Back to Articles

Intermediate

Data Visualization in Flutter with fl_chart

· 7 min read

Data Visualization in Flutter with fl_chart

What Is fl_chart?

When you want to show data like monthly spending, weekly step counts or a sales trend in an app, writing a CustomPainter from scratch takes time and forces you to manage touch interaction, animation and axis labels by hand. fl_chart is a pure-Dart, dependency-free charting package built exactly for this job. It offers ready-made chart widgets like BarChart, LineChart, PieChart and ScatterChart, all supporting animated transitions and touch tooltips out of the box.

A bar chart animating between two data sets
BarChart automatically animates its bars to new values whenever the data changes.

Basic Usage: BarChart

A BarChart takes a BarChartData; this data class holds a list of BarChartGroupData, each representing one bar group. Each group can contain one or more bars (BarChartRodData) at its own x position.

BarChart(
  BarChartData(
    barGroups: [
      for (var i = 0; i < haftalikSatislar.length; i++)
        BarChartGroupData(
          x: i,
          barRods: [
            BarChartRodData(
              toY: haftalikSatislar[i],
              color: Colors.blueAccent,
              width: 18,
              borderRadius: BorderRadius.circular(4),
            ),
          ],
        ),
    ],
    titlesData: FlTitlesData(
      bottomTitles: AxisTitles(
        sideTitles: SideTitles(
          showTitles: true,
          getTitlesWidget: (value, meta) => Text(gunler[value.toInt()]),
        ),
      ),
    ),
    gridData: const FlGridData(show: false),
    borderData: FlBorderData(show: false),
  ),
)

titlesData controls which titles are shown on each axis, while gridData and borderData let you toggle the background grid lines and the border. For a clean look, it's usually better to turn both off and keep only the axis you actually need.

Showing a Trend: LineChart

To show a change over time (for example, daily active users), LineChart is a better fit. Data points are given as FlSpot(x, y) and turned into a line with LineChartBarData.

LineChart(
  LineChartData(
    lineBarsData: [
      LineChartBarData(
        spots: [
          const FlSpot(0, 120),
          const FlSpot(1, 150),
          const FlSpot(2, 140),
          const FlSpot(3, 190),
          const FlSpot(4, 210),
        ],
        isCurved: true,
        color: Colors.teal,
        barWidth: 3,
        dotData: const FlDotData(show: false),
        belowBarData: BarAreaData(
          show: true,
          color: Colors.teal.withValues(alpha: 0.15),
        ),
      ),
    ],
  ),
)

isCurved: true smooths the line; filling the area below it with belowBarData gives the chart a modern area-chart feel. dotData: show: false hides the dot at every data point, leaving a cleaner line.

Animated Transitions

When the data changes (for example, the user picks a different week), you want the chart to glide smoothly to the new values instead of jumping. BarChart and LineChart handle this automatically through the swapAnimationDuration parameter; when a new BarChartData is passed inside setState, the old and new values are interpolated automatically.

BarChart(
  yeniVeri,
  swapAnimationDuration: const Duration(milliseconds: 400),
  swapAnimationCurve: Curves.easeOutCubic,
)

Touch Interaction: Showing Tooltips

To let the user see the value when they tap a bar or a point on a line, use the touchData parameter. With BarTouchData or LineTouchData you can customize the tooltip's background color, text style and which value is displayed.

BarChartData(
  barGroups: barGruplari,
  barTouchData: BarTouchData(
    touchTooltipData: BarTouchTooltipData(
      getTooltipItem: (group, groupIndex, rod, rodIndex) {
        return BarTooltipItem(
          '${rod.toY.toStringAsFixed(0)} adet',
          const TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
        );
      },
    ),
  ),
)

Which Chart Type, When?

• BarChart: for comparing categories (e.g. sales by day of the week). Reads best when the number of categories is small and fixed. • LineChart: for showing a continuous change or trend over time; stays more readable than a BarChart as the number of data points grows. • PieChart: for showing how parts relate to a whole (e.g. a budget breakdown); more than 5-6 slices hurts readability. • ScatterChart: for showing the relationship or clustering between two variables, suited to continuous rather than categorical data.

Things to Watch Out For

1. Don't Forget a Fixed Height

BarChart and LineChart throw an error inside a parent that gives them unbounded height (for example, a Column without an Expanded). Always place the chart inside a fixed-height SizedBox or an area bounded by Expanded.

2. Too Many Data Points Hurt Performance

A LineChart with thousands of FlSpot values being redrawn every frame can cause jank. For very large datasets, downsample the points to a reasonable count before showing the chart.

Conclusion

fl_chart is the most practical way to produce professional-looking, animated and interactive charts without drawing on a Canvas by hand. Pick BarChart for category comparisons, LineChart for trends, PieChart for proportions; fine-tune transitions with swapAnimationDuration and the touch experience with touchData. Remember to give the chart a fixed height and to downsample large datasets.