I'm developing a Flutter app where I need to visually display data using charts. Specifically, I want to implement:
A Pie Chart to represent proportions or percentages.
A Line Chart to display trends over time or continuous data.
I'm looking for guidance on how to implement these charts in Flutter. I'm open to any of the following approaches:
Using a reliable Flutter charting library (lightweight and customizable).
Drawing charts manually using CustomPaint, if necessary.
Any other practical and performance-friendly solution.
See if the following example helps. It makes use of fl_chart
package:
Get it with:
flutter pub add fl_chart
Code:
import 'package:flutter/material.dart';
import 'package:fl_chart/fl_chart.dart';
void main() {
runApp(
const MaterialApp(home: FlChartsPage(), debugShowCheckedModeBanner: false),
);
}
class FlChartsPage extends StatelessWidget {
const FlChartsPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Pie and Line Charts with fl_chart')),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text('Pie Chart', style: Theme.of(context).textTheme.bodyMedium),
const SizedBox(height: 16),
SizedBox(
height: 250,
child: PieChart(
PieChartData(
sectionsSpace: 2,
centerSpaceRadius: 0, // Full pie (no donut hole)
sections: [
PieChartSectionData(
value: 40,
color: Colors.blue,
title: 'A 40%',
radius: 100,
titleStyle: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
PieChartSectionData(
value: 30,
color: Colors.red,
title: 'B 30%',
radius: 100,
titleStyle: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
PieChartSectionData(
value: 20,
color: Colors.green,
title: 'C 20%',
radius: 100,
titleStyle: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
PieChartSectionData(
value: 10,
color: Colors.orange,
title: 'D 10%',
radius: 100,
titleStyle: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
],
),
),
),
const SizedBox(height: 32),
Text('Line Chart', style: Theme.of(context).textTheme.bodyMedium),
const SizedBox(height: 16),
AspectRatio(
aspectRatio: 1.5,
child: LineChart(
LineChartData(
lineBarsData: [
LineChartBarData(
isCurved: true,
spots: const [
FlSpot(0, 1),
FlSpot(1, 3),
FlSpot(2, 2),
FlSpot(3, 5),
FlSpot(4, 3),
FlSpot(5, 4),
],
barWidth: 3,
color: Colors.blue,
dotData: FlDotData(show: true),
),
],
titlesData: FlTitlesData(
bottomTitles: AxisTitles(
sideTitles: SideTitles(
showTitles: true,
reservedSize: 32,
),
),
leftTitles: AxisTitles(
sideTitles: SideTitles(showTitles: true),
),
),
gridData: FlGridData(show: true),
borderData: FlBorderData(show: true),
),
),
),
const SizedBox(height: 32),
],
),
),
);
}
}