Skip to main content

Command Palette

Search for a command to run...

Drawing Graphs in Flutter using fl_chart

Hello, I'm John Kim, mobile & web application developer with 10+ years of expertise.

Updated
•3 min read•View as Markdown
J
Hello, I'm John Kim, mobile & web application developer with 10+ years of expertise.

Data visualization is crucial for turning raw numbers into an understandable narrative. In modern application development, introducing responsive and interactive charts helps users interpret trends quickly, whether tracking daily steps or managing investment portfolios. While Flutter does not include a highly advanced plotting mechanism out of the box, its layout flexibility makes it perfectly equipped to host robust external data libraries.

This tutorial explores the dominant external packages used for rendering charts in Flutter and provides a practical implementation guide.

Review of External Chart Libraries

Choosing the right package impacts application bundle size, rendering performance, and user experience.

  • fl_chart: The community favorite. It is a highly customizable, hardware-accelerated library supporting line, bar, pie, scatter, and radar graphs. It relies entirely on native Canvas operations, avoiding high memory overhead.

  • syncfusion_flutter_charts: A premium, commercial-grade package built for complex enterprise needs. It is ideal if you need heavy financial candle visualizations or exceptionally fast rendering for thousands of live data points.

  • material_charts: A newer entry providing clean, strict adherence to Material Design 3 guidelines with minimal setup overhead.

For this tutorial, we will use fl_chart due to its open-source nature, modern layout paradigms, and flexibility across iOS, Android, and Web platforms.

Core Concept: Tooltips Across Multiple Chart Types

When dealing with data, a feature users expect is interactivity via tooltips (displaying precise data labels upon touching or hovering over data sections).

In fl_chart, interactivity is uniformly driven by a dedicated touch architecture across different graph configurations:

  • In line charts, this is controlled through LineTouchData.

  • In bar charts, it is handled via BarTouchData.

Let's look at how to implement custom touch tooltips natively across both graph environments using a single clean structure.

1. Implementation in a Line Chart

import 'package:flutter/material.dart';
import 'package:fl_chart/fl_chart.dart';

class AnalyticalLineChart extends StatelessWidget {
  const AnalyticalLineChart({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 300,
      child: LineChart(
        LineChartData(
          // --- Feature Implementation: Interactivity & Tooltips ---
          lineTouchData: LineTouchData(
            touchTooltipData: LineTouchTooltipData(
              getTooltipColor: (touchedSpot) => Colors.blueGrey.withOpacity(0.8),
              getTooltipItems: (List<LineBarSpot> touchedSpots) {
                return touchedSpots.map((barSpot) {
                  return LineTooltipItem(
                    'Value: ${barSpot.y}',
                    const TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
                  );
                }).toList();
              },
            ),
          ),
          gridData: const FlGridData(show: false),
          titlesData: const FlTitlesData(show: true),
          borderData: FlBorderData(show: true, border: Border.all(color: Colors.grey, width: 1)),
          lineBarsData: [
            LineChartBarData(
              spots: const [
                FlSpot(0, 3),
                FlSpot(1, 1),
                FlSpot(2, 4),
                FlSpot(3, 2),
              ],
              isCurved: true,
              barWidth: 4,
              color: Colors.blue,
            ),
          ],
        ),
      ),
    );
  }
}

2. Implementation in a Bar Chart

The bar chart implementation follows a similar pattern to the line chart, utilizing BarChartData and BarTouchData. Below is the implementation example:

import 'package:flutter/material.dart';
import 'package:fl_chart/fl_chart.dart';

class AnalyticalBarChart extends StatelessWidget {
  const AnalyticalBarChart({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 300,
      child: BarChart(
        BarChartData(
          barTouchData: BarTouchData(
            touchTooltipData: BarTouchTooltipData(
              getTooltipItem: (group, groupIndex, rod, rodIndex) {
                return BarTooltipItem(
                  'Value: ${rod.toY}',
                  const TextStyle(color: Colors.white),
                );
              },
            ),
          ),
          barGroups: [
            BarChartGroupData(x: 0, barRods: [BarChartRodData(toY: 5)]),
            BarChartGroupData(x: 1, barRods: [BarChartRodData(toY: 8)]),
          ],
        ),
      ),
    );
  }
}

Explanation of the Code

The fl_chart library provides a unified approach to interactivity:

  • Data Structure: Line charts use FlSpot(x, y), while bar charts utilize BarChartGroupData.

  • Tooltip Mechanism: Both use touchTooltipData within their respective *TouchData classes.

  • Customization: Callbacks (getTooltipItems, getTooltipItem) dynamically generate labels upon user interaction, allowing for highly customized display.

Conclusion

fl_chart allows for responsive, interactive, and highly styled charts in Flutter. By utilizing consistent touch data structures for different graph types, developers can create polished visual representations with efficient, clean code.

More from this blog

D

Developer's Mind

28 posts