Getting started with Flutter Funnel Chart (SfFunnelChart)

22 Apr 202114 minutes to read

This section explains the steps required to populate the chart with data, title, data labels, legend, and tooltips. This section covers only the minimal features needed to know to get started with the chart.

To get start quickly with our Flutter chart widget, you can check on this video.

Add Flutter Charts to an application

Create a simple project using the instructions given in the Getting Started with your first Flutter app documentation.

Add dependency

Add the Syncfusion Flutter Chart dependency to your pub spec file.

  • dart
  • dependencies:
    
        syncfusion_flutter_charts: ^xx.x.xx

    NOTE

    Here xx.x.xx denotes the current version of Syncfusion Flutter Charts package.

    Get packages

    Run the following command to get the required packages.

  • dart
  • $ flutter pub get

    Import package

    Import the following package in your Dart code.

  • dart
  • import 'package:syncfusion_flutter_charts/charts.dart';

    Initialize chart

    Once the package has been imported, initialize the chart as a child of any widget. SfFunnelChart can be used to render Funnel charts. Here, as we are rendering Funnel chart, initialize SfFunnelChart widget as a child of Container widget.

  • dart
  • @override
        Widget build(BuildContext context) {
            return Scaffold(
                body: Center(
                    child: Container(
                        //Initialize chart
                        child: SfFunnelChart()
                    )
                )
            );
        }

    NOTE

    : An empty chart will be displayed. This is the charts default behavior.

    Bind data source

    Based on your data, initialize the series type. In the series, you need to map the data source and the fields for x and y data points. Here, Funnel series is rendered that is demonstrated in the following code snippet.

  • dart
  • @override
        Widget build(BuildContext context) {
            final List<ChartData> chartData = [
                ChartData('Jan', 35),
                ChartData('Feb', 28),
                ChartData('Mar', 34),
                ChartData('Apr', 32),
                ChartData('May', 40)
            ];
            return Scaffold(
                body: Center(
                    child: Container(
                        child: SfFunnelChart(
                          series:FunnelSeries<ChartData, String>(
                                dataSource: chartData,
                                xValueMapper: (ChartData data, _) => data.x,
                                yValueMapper: (ChartData data, _) => data.y
                            )
                        )
                    )
                )
            );
        }
    
        class ChartData {
          ChartData(this.x, this.y, [this.color]);
            final String x;
            final double y;
            final Color color;
        }

    Bind data source

    Add title

    You can add a title to the chart to provide quick information to users about the data plotted in the chart. The title to chart can be set as demonstrated in the following code snippet.

  • dart
  • @override
        Widget build(BuildContext context) {
            return Scaffold(
                body: Center(
                    child: Container(
                        child: SfFunnelChart(
                          // Chart title text
                          title: ChartTitle(text: 'Half yearly sales analysis'),
                          series:FunnelSeries<ChartData, String>(
                                dataSource: chartData,
                                xValueMapper: (ChartData data, _) => data.x,
                                yValueMapper: (ChartData data, _) => data.y
                            )
                        )
                    )
                )
            );
        }

    Title to chart

    Enable data labels

    You can add data labels to improve the readability of the chart using the dataLabelSettings property.

  • dart
  • @override
        Widget build(BuildContext context) {
            return Scaffold(
                body: Center(
                    child: Container(
                        child: SfFunnelChart(
                          series:FunnelSeries<ChartData, String>(
                                dataSource: chartData,
                                xValueMapper: (ChartData data, _) => data.x,
                                yValueMapper: (ChartData data, _) => data.y,
                                // Render the data label
                                dataLabelSettings:DataLabelSettings(isVisible : true)
                            )
                        )
                    )
                )
            );
        }

    DataLabel to chart

    Enable legend

    The legend provides information about the series rendered in the chart.

    You can use legend in chart by setting the isVisible property to true in SfFunnelChart.legend.

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

    Legend in chart

    Enable tooltip

    The tooltip is used when you cannot display information using the data labels due to space constraints.

    The tooltipBehavior property in chart is used to enable and customize the tooltip for the Funnel series. The tooltip is enabled as demonstrated in the following code snippet.

  • dart
  • late TooltipBehavior _tooltipBehavior;
    
        @override
        void initState(){
            _tooltipBehavior = TooltipBehavior(enable: true);
            super.initState();
        }
    
        @override
        Widget build(BuildContext context) {
            return Scaffold(
                appBar: AppBar(
                    title: Text(widget.title),
                ),
                body: Center(
                    child: Container(
                        child: SfFunnelChart(
                          // Enables the tooltip for all the series in chart
                            tooltipBehavior: _tooltipBehavior,
                            series:FunnelSeries<ChartData, String>(
                                dataSource: chartData,
                                xValueMapper: (ChartData data, _) => data.x,
                                yValueMapper: (ChartData data, _) => data.y,
                            )
                        )
                    )
                )
            );
        }

    Tooltip in chart

    You can find the complete getting started sample from this link.