How to position the legend on Flutter Treemap?
In Flutter Treemap, you can position the legend on the Treemap by following these steps:
Step 1: Add Syncfusion® Flutter Treemap package to your dependencies in the pubspec.yaml file, and then initialize the data source and ColorMapper in the initState() method.
late List<SocialMediaUsers> _socialMediaUsers; late List<TreemapColorMapper> _colorMappers; @override void initState() { _socialMediaUsers = <SocialMediaUsers>[ SocialMediaUsers('India', 'Facebook', 25.4), SocialMediaUsers('USA', 'Instagram', 19.11), SocialMediaUsers('Japan', 'Facebook', 13.3), SocialMediaUsers('Germany', 'Instagram', 10.65), SocialMediaUsers('France', 'Twitter', 7.54), ]; _colorMappers = <TreemapColorMapper>[ TreemapColorMapper.range( from: 0, to: 10, minSaturation: 0.5, maxSaturation: 1, color: Colors.blue[200]!), const TreemapColorMapper.range( from: 10, to: 20, minSaturation: 0.5, maxSaturation: 1, color: Colors.deepOrange), TreemapColorMapper.range( from: 20, to: 30, minSaturation: 0.5, maxSaturation: 1, color: Colors.blue[800]!), ]; super.initState(); }
Step 2: Initialize the Treemap with the necessary properties. Please refer to this documentation for initializing the Treemap and populating the data source.
Step 3: Set the legend property to the Treemap and position the legend by using the offset property.
@override Widget build(BuildContext context) { return Scaffold( body: Center( child: SizedBox( height: 400, width: 400, child: SfTreemap( dataCount: _socialMediaUsers.length, legend: const TreemapLegend(direction: Axis.vertical, offset: Offset(-180, 310)), weightValueMapper: (int index) { return _socialMediaUsers[index].usersInMillions; }, levels: [ TreemapLevel( groupMapper: (int index) { return _socialMediaUsers[index].country; }, colorValueMapper: (TreemapTile tile) { return tile.weight; }, ), ], colorMappers: _colorMappers, ), ), ), ); } class SocialMediaUsers { SocialMediaUsers(this.country, this.socialMedia, this.usersInMillions); final String country; final String socialMedia; final double usersInMillions; }
Output
Check the following links for more features in Syncfusion® Flutter Treemap:
Live samples
Blogs related to Treemap
Conclusion
I hope you enjoyed learning about how to position the legend on Flutter Treemap.
You can refer to our Flutter Treemap feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Flutter Treemap example 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 forums, Direct-Trac, or feedback portal. We are always happy to assist you!