Visualize Election Results Using the Flutter Treemap Widget | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (173).NET Core  (29).NET MAUI  (207)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (40)Black Friday Deal  (1)Blazor  (215)BoldSign  (14)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (66)Flutter  (132)JavaScript  (220)Microsoft  (118)PDF  (81)Python  (1)React  (100)Streamlit  (1)Succinctly series  (131)Syncfusion  (912)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (158)Xamarin  (161)XlsIO  (36)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (147)Chart  (130)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (625)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (40)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (505)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (386)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (589)What's new  (331)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Visualize Election Results Using the Flutter Treemap Widget

Visualize Election Results Using the Flutter Treemap Widget

The Syncfusion Flutter Treemap is a data visualization widget. It visualizes flat and hierarchical data as tiles (rectangles) that are sized and colored based on the quantitative values. Its features include data binding, legends, data labels, tooltips, color mapping, layout types, drill-down, and more. This blog post explores the step-by-step process of how to visualize election results using the Flutter Treemap widget.

We are going to render a Treemap representing the presidential election of the United States with tiles, legends, a tooltip, and two different colors to represent the candidates. The following GIF image depicts the Treemap we will build in this blog.

Visualizing Election Results Using Flutter Treemap
Visualizing Election Results Using Flutter Treemap

Note: The Treemap information is sourced from the 2020 United States presidential election.

In the above GIF image, the tile size is calculated based on the population (voter count) of the state, and the color is applied based on the winner of that state. The tooltip displays the candidate’s name, state, and vote percentage details.

Installation and configuration

Follow these steps to install and configure the Flutter Treemap widget in your application:

  1. First, create a simple project using the instructions given in the Getting Started with your first Flutter app documentation.
  2. Then, add the Syncfusion Flutter Treemap dependency to your pubspec.yaml file with the current version of the Syncfusion Flutter Treemap package.
    Refer to the following code.

    dependencies:
    syncfusion_flutter_treemap: ^xx.x.xx
  1. Now, import the library using the following code.
    import 'package:syncfusion_flutter_treemap/treemap.dart';

Populate data source and initialize the Treemap

Follow these steps to populate the election results in the Flutter Treemap:

  1. We are going to display the results for each state in the U.S.A. Thus, the Treemap should have a tile (rectangle) for each state. So, we have to set the number of states in the U.S.A as the value for the dataCount property.
  2. The next thing the Treemap needs to know is the size of each tile. In an election result, the tile size represents the population (voters) of each state. So, we have to return the voters count in each state from the weightValueMapper callback of the Treemap.
  3. Now, group the election results in order of the state in the Flutter Treemap. Return the state name from the groupMapper callback. You can have more than one TreemapLevel in the levels collection to form a hierarchical Treemap. As this election result is a flat structure, we have only one TreemapLevel.Refer to the following code snippet. Here, we have taken the details of only five states from the data source to provide better readability.
    late List<_StateElectionDetails> _stateWiseElectionResult;
    
      @override
      void initState() {
        // Define the data source.
        _stateWiseElectionResult = <_StateElectionDetails>[
          const _StateElectionDetails(
              state: 'Washington',
              candidate: 'Joe Biden',
              party: 'Democratic',
              totalVoters: 4087631,
              votes: 2369612,
              percentage: 57.97),
          const _StateElectionDetails(
              state: 'Oregon',
              candidate: 'Joe Biden',
              party: 'Democratic',
              totalVoters: 2374321,
              votes: 1340383,
              percentage: 56.45),
          const _StateElectionDetails(
              state: 'Alabama',
              candidate: 'Donald Trump',
              party: 'Republican',
              totalVoters: 2323282,
              votes: 1441170,
              percentage: 62.03),
          const _StateElectionDetails(
              state: 'Arizona',
              candidate: 'Joe Biden',
              party: 'Democratic',
              totalVoters: 3387326,
              votes: 1672143,
              percentage: 49.36),
          const _StateElectionDetails(
              state: 'Arkansas',
              candidate: 'Donald Trump',
              party: 'Republican',
              totalVoters: 1219069,
              votes: 760647,
              percentage: 62.40),
        ];
    
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        return Padding(
          padding: const EdgeInsets.all(10.0),
          child: SfTreemap(
            // Set the data source length.
            dataCount: _stateWiseElectionResult.length,
            // Return the population of the state
            weightValueMapper: (int index) {
              return _stateWiseElectionResult[index].totalVoters;
            },
            levels: [
              TreemapLevel(
                // Update the group mapper
                groupMapper: (int index) => _stateWiseElectionResult[index].state,
              ),
            ],
          ),
        );
      }

After executing this code, we will get output like in the following screenshot.

Rendering a Simple Flutter Treemap
Rendering a Simple Flutter Treemap

Enable the labels to identify the states in tiles

In the previous Treemap, it is unclear that which tile belongs to which state. The rescue comes through the labels feature, through which we are going to put the state names in the tiles. To do so, return the text widget from the labelBuilder callback.

Refer to the following code example.

@override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(10.0),
      child: SfTreemap(
        dataCount: _stateWiseElectionResult.length,
        weightValueMapper: (int index) {
          return _stateWiseElectionResult[index].totalVoters;
        },
        levels: [
          TreemapLevel(
            groupMapper: (int index) => _stateWiseElectionResult[index].state,
            
            // Write the state name in each tile.
            labelBuilder: (BuildContext context, TreemapTile tile) {
              return Padding(
                padding: const EdgeInsets.all(8.0),
                child: Text(
                  tile.group,
                  style: TextStyle(color: Colors.white),
                ),
              );
            },
          ),
        ],
      ),
    );
  }

After executing this code, we will get the output like in the following screenshot.

Enabling Labels in Flutter Treemap
Enabling Labels in Flutter Treemap

Update the tile color based on the winning party

So, everything works as expected. Next, we are going to change the tile color based on the winning candidate in that state (tile). To do this, update the TreemapColorMapper collection to the Treemap’s colorMappers property. Then, update the Treemaplevel’s colorValueMapper property.

Refer to the following code example.

late List _colorMappers;

@override
  void initState() {

    //Update the data from previous code example for data source.
    _stateWiseElectionResult = <_StateElectionDetails>[];

    //Define the color values for both parties.
    _colorMappers = [
      const TreemapColorMapper.value(value: 'Democratic', color: Colors.blue),
      const TreemapColorMapper.value(
          value: 'Republican', color: Colors.orangeAccent),
    ];

    super.initState();
}

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(10.0),
      child: SfTreemap(
        dataCount: _stateWiseElectionResult.length,
        weightValueMapper: (int index) {
          return _stateWiseElectionResult[index].totalVoters;
        },
        colorMappers: _colorMappers,
        levels: [
          TreemapLevel(
            groupMapper: (int index) => _stateWiseElectionResult[index].state,
            labelBuilder: (BuildContext context, TreemapTile tile) {
              return Padding(
                padding: const EdgeInsets.all(8.0),
                child: Text(
                  tile.group,
                  style: TextStyle(color: Colors.white),
                ),
              );
            },

            //Update the title color to represent the winning party.
            colorValueMapper: (TreemapTile tile) {
              return _stateWiseElectionResult[tile.indices[0]].party;
            },
          ),
        ],
      ),
    );
  }

After executing this code, we will get output like in the following screenshot.

Applying Colors to the Tiles Based on the Winning Party Details
Applying Colors to the Tiles Based on the Winning Party Details

Adding a legend to the Treemap

By default, the legend items’ text is rendered based on the value of the TreemapLevel.groupMapper property, which we have already defined. So, just setting the position and size of the legend is enough.

Refer to the following code example to add a legend to identify the parties based on color.

@override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(10.0),
      child: SfTreemap(
        dataCount: _stateWiseElectionResult.length,
        weightValueMapper: (int index) {
          return _stateWiseElectionResult[index].totalVoters;
        },
        colorMappers: _colorMappers,
        levels: [
          TreemapLevel(
            groupMapper: (int index) => _stateWiseElectionResult[index].state,
            labelBuilder: (BuildContext context, TreemapTile tile) {
              return Padding(
                padding: const EdgeInsets.all(8.0),
                child: Text(
                  tile.group,
                  style: TextStyle(color: Colors.white),
                ),
              );
            },
            colorValueMapper: (TreemapTile tile) {
              return _stateWiseElectionResult[tile.indices[0]].party;
            },
          ),
        ],

        // Enable the legend
        legend: TreemapLegend.bar(
          position: TreemapLegendPosition.bottom,
          segmentSize: const Size(80.0, 12.0),
        ),
      ),
    );
  }

After executing this code, we will get output like in the following screenshot.

Adding Legends to the Flutter Treemap
Adding Legends to the Flutter Treemap

Enable the tooltip

You can enable the tooltips to display additional information about the tiles when the pointer hovers over them or on tap events. For this, return a widget in the TreemapLevel.tooltipBuilder method. This widget will then be wrapped in the built-in shape that comes with the pointer at the bottom.

Refer to the following code.

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(10.0),
      child: SfTreemap(
        dataCount: _stateWiseElectionResult.length,
        weightValueMapper: (int index) {
          return _stateWiseElectionResult[index].totalVoters;
        },
        colorMappers: _colorMappers,
        levels: [
          TreemapLevel(
              groupMapper: (int index) => _stateWiseElectionResult[index].state,
              labelBuilder: (BuildContext context, TreemapTile tile) {
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text(
                    tile.group,
                    style: TextStyle(color: Colors.white),
                  ),
                );
              },

              colorValueMapper: (TreemapTile tile) {
                return _stateWiseElectionResult[tile.indices[0]].party;
              },

              // Enable the tooltip.
              tooltipBuilder: (BuildContext context, TreemapTile tile) {
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: RichText(
                      text: TextSpan(
                          text: _stateWiseElectionResult[tile.indices[0]]
                              .candidate,
                          children: [
                        TextSpan(
                              text: '\n${tile.group}',
                            ),

                        TextSpan(
                          text: '\nWon percentage : ' +
                              _stateWiseElectionResult[tile.indices[0]]
                                  .percentage
                                  .toString() +
                              '%',
                        ),
                      ])),
                );
              }),
        ],

        legend: TreemapLegend.bar(
          position: TreemapLegendPosition.bottom,
          segmentSize: const Size(80.0, 12.0),
        ),
      ),
    );
  }

After executing this code, we will get the output like in the following screenshot.

Enabling Tooltips in Flutter Treemap
Enabling Tooltips in Flutter Treemap

Resource

For more information, refer to the Visualize Election Results with Flutter Treemap demo.

Conclusion

Thanks for reading! In this blog, we have seen how to visualize election results using the Syncfusion Flutter Treemap widget. With this widget, you can also visualize literacy rates, weather reports, and more with the versatile and colorful features in it. To learn more, you can also check out our Flutter Treemap widget’s user guide and example projects. Try out the steps given in this blog post and leave your feedback in the comments section of this blog post!

Don’t miss our demo app in Google Play, the App Store, the Windows Store, the App CenterSnapcraft, and on our website.

You can contact us through our support forumDirect-Trac, or feedback portal. As always, we are happy to assist you!

Related blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed