Legend in Cartesian charts

The legend contains list of chart series/data points in chart. The information provided in each legend item helps to identify the corresponding data series in chart.

  • dart
  • @override
        Widget build(BuildContext context) {
          return Scaffold(
            body: Center(
              child: Container(
                  child:SfCartesianChart(
                    // Enables the legend
                    legend: Legend(isVisible: true),
                    series: <LineSeries>[
                      LineSeries<ChartData, String>(
                        dataSource: chartData,
                        xValueMapper: (ChartData data, _) => data.x,
                        yValueMapper: (ChartData data, _) => data.y
                      )
                    ]
                  )
                )
              )
            );
          }

    Legend

    Customizing legend

    The name property of CartesianSeries is used to define the label for the corresponding series legend item .The appearance of the label can be customized using the below properties.

    • borderWidth – used to change the stroke width of the legend shape.
    • borderColor – used to change the stroke color of the legend shape.
    • backgroundColor - used to change the background color of legend shape.
    • opacity - used to control the transparency of the legend icon shape.
    • padding - used to add padding between the icon shape and the text.
    • iconHeight - used to change the height of the icon shape.
    • iconWidth - used to change the width of the icon shape.
    • borderWidth – used to change the stroke width of the legend icon shape.
    • iconBorderColor – used to change the stroke color of the legend icon shape.
    • itemPadding - used to add padding between the first legend text and the second legend icon shape.
  • dart
  • @override
        Widget build(BuildContext context) {
          return Scaffold(
            body: Center(
              child: Container(
                child:SfCartesianChart(
                  legend: Legend(
                    isVisible: true,
                    name:legend,
                    // Border color and border width of legend
                    borderColor: Colors.black,
                    borderWidth: 2
                  ),
                  series: <CartesianSeries>[
                    LineSeries<ChartData, String>(
                      name:'line-series',
                      dataSource: chartData,
                      xValueMapper: (ChartData data, _) => data.x,
                      yValueMapper: (ChartData data, _) => data.y
                    )
                  ]
                )
              )
            )
          );
        }

    Customized Legend

    Legend title

    The following properties are used to define and customize the title of legend.

    • text - used to change the text of the title.
    • textStyle – used to change the text color, size, font family, fontStyle, and font weight.
    • textStyle.color – used to change the color of the text.
    • textStyle.fontFamily - used to change the font family for legend text.
    • textStyle.fontStyle – used to change the font style for the legend text.
    • textStyle.fontSize - used to change the font size for the legend text.
    • alignment – used to change the alignment of the title text; it can be near, center, or far.
  • dart
  • @override
        Widget build(BuildContext context) {
          return Scaffold(
            body: Center(
                child: Container(
                    child:SfCartesianChart(
                    legend: Legend(
                        isVisible: true,
                        // Legend title
                        title: LegendTitle(
                          text:'Country',
                          textStyle: ChartTextStyle(
                          color: Colors.red,
                          fontSize: 15,
                          fontStyle: FontStyle.italic,
                          fontWeight: FontWeight.w900
                        )
                      ),
                    series: <CartesianSeries>[
                      LineSeries<ChartData, String>(
                        dataSource: chartData,
                        xValueMapper: (ChartData data, _) => data.x,
                        yValueMapper: (ChartData data, _) => data.y
                  ),
                    series: <CartesianSeries>[
                      LineSeries<ChartData, String>(
                        dataSource: chartData,
                        xValueMapper: (ChartData data, _) => data.x,
                        yValueMapper: (ChartData data, _) => data.y
                    )
                  ]
                )
              )
            )
          );
        }

    Legend title

    Toggles the series visibility

    You can control the visibility of the series by tapping the legend item. You can enable this feature by enabling the toggleSeriesVisibility property.

  • dart
  • @override
        Widget build(BuildContext context) {
          return Scaffold(
            body: Center(
              child: Container(
                child:SfCartesianChart(
                  legend: Legend(
                    isVisible: true,
                    // Toogles the series visibility on tapping the legend item
                    toggleSeriesVisibility: true
                  ),
                  series: <CartesianSeries>[
                    LineeSeries<ChartData, String>(
                      dataSource: chartData,
                      xValueMapper: (ChartData data, _) => data.x,
                      yValueMapper: (ChartData data, _) => data.y
                    )
                  ]
                )
              )
            )
          );
        }

    Legend visibility

    The isVisible property of legend is used to toggle the visibility of legend.

  • dart
  • @override
        Widget build(BuildContext context) {
          return Scaffold(
            body: Center(
              child: Container(
                child:SfCartesianChart(
                  legend: Legend(
                    // Visibility of legend
                    isVisible: false
                  ),
                  series: <CartesianSeries>[
                    LineSeries<ChartData, String>(
                      dataSource: chartData,
                      xValueMapper: (ChartData data, _) => data.x,
                      yValueMapper: (ChartData data, _) => data.y
                    )
                  ]
                )
              )
            )
          );
        }

    Legend item visibility

    You can control the visibility of a particular series legend item using the isVisibleInLegend property of series. The default value of the isVisibleInLegend property is true. If it is set to false, then the legend item for this specific series will not be displayed in the legend.

  • dart
  • @override
        Widget build(BuildContext context) {
          return Scaffold(
            body: Center(
              child: Container(
                child: SfCartesianChart(
                  legend: Legend(
                    isVisible: true
                  ),
                  primaryXAxis: CategoryAxis(),
                  series: <ColumnSeries>[
                    ColumnSeries<ChartData, String>(                  
                      dataSource: chartData,
                      xValueMapper: (ChartData data, _) => data.x,
                      yValueMapper: (ChartData data, _) => data.y
                    ),
                    ColumnSeries<ChartData, String>(
                      // Hiding the legend item for this series
                      isVisibleInLegend: false,
                      dataSource: chartData,
                      xValueMapper: (ChartData data, _) => data.x,
                      yValueMapper: (ChartData data, _) => data.y
                    )
                  ]
                )
              )
            )
          );
        }

    Legend isVisibleInLegend

    Legend overflow

    The legend items can be placed in multiple rows or scroll can be enabled using the overflowMode property if size of the total legend items exceeds the available size. The default value of the overflowMode property is scroll.

  • dart
  • @override
        Widget build(BuildContext context) {
          return Scaffold(
            body: Center(
              child: Container(
                child: SfCartesianChart(
                  legend: Legend(
                    isVisible: true,
                    // Overflowing legend content will be wraped
                    overflowMode: LegendItemOverflowMode.wrap
                  ),
                  series: <CartesianSeries>[
                    LineSeries<ChartData, String>(
                      dataSource: chartData,
                      xValueMapper: (ChartData data, _) => data.x,
                      yValueMapper: (ChartData data, _) => data.y
                    )
                  ]
                )
              )
            )
          );
        }

    Legend)

    Positioning the legend

    You can change the position of the legend inside the chart. The following properties are used to customize the position of legend. auto position will place the legend at the right, if the chart’s width is greater than the chart’s height. Else the legend will be placed at the bottom position.

    • position – used to position the legend relatively. The available options are auto, bottom, left, right, and top. Defaults to auto.
    • orientation - used to change the orientation of the legend, the default value is auto. Also you can set horizontal or vertical.
  • dart
  • @override
        Widget build(BuildContext context) {
          return Scaffold(
            body: Center(
              child: Container(
                child: SfCartesianChart(
                  legend: Legend(
                    isVisible: true,
                    // Legend will be placed at the left
                    position: LegendPosition.left
                  ),
                  series: <CartesianSeries>[
                    AreaSeries<ChartData, String>(
                      dataSource: chartData,
                      xValueMapper: (ChartData data, _) => data.x,
                      yValueMapper: (ChartData data, _) => data.y
                    ),
                  ]
                )
              )
            )
          );
        }

    Legend)

    Legend item template

    You can customize the appearance of legend items with your template by using legendItemBuilder property of legend. Here you can specify the content that needs to be displayed in the legend text as widget.

  • dart
  • @override
        Widget build(BuildContext context) {
          return Scaffold(
            body: Center(
              child: Container(
                child: SfCartesianChart(
                  legend: Legend(
                    isVisible: true,
                    // Templating the legend item
                    legendItemBuilder: (String name, dynamic series, dynamic point, int index) {
                      return Container(
                        child: Container(
                          child: Text('template')
                      );
                    }
                  ),
                  series: <CartesianSeries>[
                    AreaSeries<ChartData, String>(
                      dataSource: chartData,
                      xValueMapper: (ChartData data, _) => data.x,
                      yValueMapper: (ChartData data, _) => data.y
                    )
                  ]
                )
              )
            )
          );
        }