Bar Chart in Flutter Cartesian Charts (SfCartesianChart)

9 Jun 202111 minutes to read

To create a Flutter bar chart quickly, you can check this video.

To render a bar chart, create an instance of BarSeries, and add it to the series collection property of SfCartesianChart. The following properties can be used to customize the appearance:

  • color - changes the color of the series.
  • opacity - controls the transparency of the chart series.
  • borderWidth - changes the stroke width of the series.
  • borderColor - changes the stroke color of the series.
  • dart
  • @override
        Widget build(BuildContext context) {
            return Scaffold(
                body: Center(
                    child: Container(
                        child: SfCartesianChart(
                            series: <ChartSeries>[
                                // Renders bar chart
                                BarSeries<SalesData, double>(
                                    dataSource: chartData,
                                    xValueMapper: (SalesData sales, _) => sales.year,
                                    yValueMapper: (SalesData sales, _) => sales.sales
                                )
                            ]
                        )
                    )   
                )
            );
        }

    Bar chart

    Bar width and spacing

    The spacing property is used to change the spacing between two segments. The default value of spacing is 0, and the value ranges from 0 to 1. Here, 1 and 0 correspond to 100% and 0% of the available space, respectively.

    The width property is used to change the width of the rectangle. The default value of the width is 0.7, and the value ranges from 0 to 1. Here, 1 and 0 correspond to 100% and 0% of the available width, respectively.

  • dart
  • @override
        Widget build(BuildContext context) {
            return Scaffold(
                body: Center(
                    child: Container(
                        child: SfCartesianChart(
                            series: <ChartSeries>[
                                BarSeries<SalesData, double>(
                                    dataSource: chartData,
                                    xValueMapper: (SalesData sales, _) => sales.year,
                                    yValueMapper: (SalesData sales, _) => sales.sales,
                                    width: 0.6, // Width of the bars
                                    spacing: 0.3 // Spacing between the bars
                                )
                            ]
                        )
                    )   
                )
            );
        }

    Bar width and spacing

    Rounded corners

    The borderRadius property is used to add the rounded corners to the rectangle.

  • dart
  • @override
        Widget build(BuildContext context) {
            return Scaffold(
                body: Center(
                    child: Container(
                        child: SfCartesianChart(
                            series: <ChartSeries>[
                                BarSeries<SalesData, double>(
                                    dataSource: chartData,
                                    xValueMapper: (SalesData sales, _) => sales.year,
                                    yValueMapper: (SalesData sales, _) => sales.sales,
                                    borderRadius: BorderRadius.all(Radius.circular(15))
                                )
                            ]
                        )
                    )   
                )
            );
        }

    Bar rounded corners

    Track customization

    You can render the bar chart with track. Track is a rectangular bar rendered from the start to the end of the axis. Bar series will be rendered above the track. The isTrackVisible property is used to enable the track. The following properties can be used to customize the appearance:

  • dart
  • @override
        Widget build(BuildContext context) {
            return Scaffold(
                body: Center(
                    child: Container(
                        child: SfCartesianChart(
                            series: <ChartSeries>[
                                BarSeries<SalesData, double>(
                                    dataSource: chartData,
                                    // Renders the track
                                    isTrackVisible: true,
                                    xValueMapper: (SalesData sales, _) => sales.year,
                                    yValueMapper: (SalesData sales, _) => sales.sales
                                )
                            ]
                        )
                    )   
                )
            );
        }

    Bar track

    Also refer, color palette, color mapping, animation, gradient and empty points for customizing the bar series further.

    See Also

    *Create vertical bar chart.

    *Create horizontal bar chart