Articles in this section
Category / Section

How to apply color in treemap widget based on positive and negative values?

3 mins read

In this article, we explain how to apply color in the Flutter Treemap widget based on positive and negative values.

To show the colors based on the positive and negative values, the colorValueMapper property can be used in the SfTreemap widget.

Step 1: Initialize the treemapData variable which holds the data source for the treemap. Here the data consists of car model name, sales count of the cars in a year, and increased or decreased percentage of sales.

late List<TreemapData> treemapData;
 
  @override
  void initState() {
    treemapData = const <TreemapData>[
      TreemapData(model: 'Ertiga+XL6', sales: 104190, percentage: -10),
      TreemapData(model: 'Eeco', sales: 99480, percentage: 10),
      TreemapData(model: 'Fortuner', sales: 9204, percentage: 18),
      TreemapData(model: 'Glanza', sales: 20676, percentage: 27),
      TreemapData(model: 'Scorpio', sales: 31240, percentage: -40),
      TreemapData(model: 'XUV 500', sales: 7053, percentage: -70),
    ];
    super.initState();
  }
  
class TreemapData {
  const TreemapData(
      {required this.model, required this.sales, required this.percentage });
 
  final String model;
  final double sales;
  final double percentage;
}

Step 2: Create the SfTreemap widget, assign the treemapData list’s length to dataCount, map the sales value to weightValueMapper. Then initialize the required properties. Then assign the color based on the positive and negative values using the colorValueMapper property as in below code.

 @override
  Widget build(BuildContext context) {
    return SfTreemap(
      dataCount: treemapData.length,
      weightValueMapper: (int index) => treemapData[index].sales,
      levels: <TreemapLevel>[
        TreemapLevel(
          groupMapper: (int index) => treemapData[index].model,
          colorValueMapper: (TreemapTile tile) {
            final double percentage = treemapData[tile.indices[0]].percentage;
            if (percentage > 0) {
              return Colors.red;
            } else {
              return Colors.green;
            }
          },
          labelBuilder: (BuildContext context, TreemapTile tile) {
            return Padding(
              padding: const EdgeInsets.all(4.0),
              child: Text(tile.group, overflow: TextOverflow.ellipsis),
            );
          },
        ),
      ],
    );
  }

Now the treemap will render with red color for the negative values and green color for the positive values.

Flutter treemap - Treemap with colors based on positive and negative values


Conclusion

I hope you enjoyed learning about how to apply color in treemap widget based on positive and negative values.

You can refer to our  Flutter Treemap feature tour page to know about its other groundbreaking feature representations. You can also explore our Flutter Treemap documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied