TreemapLegend.bar constructor Null safety

const TreemapLegend.bar(
  1. {Widget? title,
  2. TreemapLegendOverflowMode overflowMode = TreemapLegendOverflowMode.scroll,
  3. EdgeInsetsGeometry? padding = const EdgeInsets.all(10.0),
  4. TreemapLegendPosition position = TreemapLegendPosition.top,
  5. Offset? offset,
  6. double spacing = 2.0,
  7. TextStyle? textStyle,
  8. Axis? direction,
  9. Size? segmentSize,
  10. TreemapLegendLabelsPlacement? labelsPlacement,
  11. TreemapLegendEdgeLabelsPlacement edgeLabelsPlacement = TreemapLegendEdgeLabelsPlacement.inside,
  12. TreemapLabelOverflow labelOverflow = TreemapLabelOverflow.visible,
  13. TreemapLegendPaintingStyle segmentPaintingStyle = TreemapLegendPaintingStyle.solid,
  14. bool showPointerOnHover = false,
  15. TreemapLegendPointerBuilder? pointerBuilder,
  16. Color? pointerColor,
  17. Size pointerSize = const Size(16, 12)}
)

Creates a bar type legend for the tree map.

Information provided in the legend helps to identify the data rendered in the tree map.

Defaults to null.

By default, legend will not be shown.

late List<SocialMediaUsers> _source;
late List<TreemapColorMapper> _colorMappers;

@override
void initState() {
  _source = <SocialMediaUsers>[
    SocialMediaUsers('India', 'Facebook', 280),
    SocialMediaUsers('India', 'Instagram', 88),
    SocialMediaUsers('USA', 'Facebook', 190),
    SocialMediaUsers('USA', 'Instagram', 120),
    SocialMediaUsers('Japan', 'Twitter', 48),
    SocialMediaUsers('Japan', 'Instagram', 31),
  ];
  _colorMappers = <TreemapColorMapper>[
    TreemapColorMapper.range(
        from: 0, to: 10, color: Colors.red, name: '10'),
    TreemapColorMapper.range(
        from: 11, to: 20, color: Colors.green, name: '20'),
    TreemapColorMapper.range(
        from: 21, to: 30, color: Colors.blue, name: '30'),
  ];
  super.initState();
}

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: SfTreemap(
      dataCount: _source.length,
      weightValueMapper: (int index) {
        return _source[index].usersInMillions;
      },
      levels: [
        TreemapLevel(
          groupMapper: (int index) {
            return _source[index].country;
          },
        ),
      ],
      legend: TreemapLegend(),
      colorMappers: _colorMappers,
    ),
  );
}

class SocialMediaUsers {
  const SocialMediaUsers(
    this.country,
    this.socialMedia,
    this.usersInMillions,
  );
  final String country;
  final String socialMedia;
  final double usersInMillions;
}

See also:

  • TreemapLegend, for adding default legend type with different icon styles like circle, diamond, rectangle and triangle.

Implementation

const TreemapLegend.bar({
  this.title,
  this.overflowMode = TreemapLegendOverflowMode.scroll,
  this.padding = const EdgeInsets.all(10.0),
  this.position = TreemapLegendPosition.top,
  this.offset,
  this.spacing = 2.0,
  this.textStyle,
  this.direction,
  this.segmentSize,
  this.labelsPlacement,
  this.edgeLabelsPlacement = TreemapLegendEdgeLabelsPlacement.inside,
  this.labelOverflow = TreemapLabelOverflow.visible,
  this.segmentPaintingStyle = TreemapLegendPaintingStyle.solid,
  this.showPointerOnHover = false,
  this.pointerBuilder,
  this.pointerColor,
  this.pointerSize = const Size(16, 12),
})  : _type = _LegendType.bar,
      iconType = TreemapIconType.circle,
      iconSize = const Size(8.0, 8.0),
      assert(spacing >= 0);