Hello Folks, In this tutorial we are going to impliment an Animated Pie Chart or Donut Chart on our Flutter Project. To do this, create a new project or start from our pie chart project. You can show data from API as well as local. So let's start,
Add Dependency
flutter pub add pie_chart
Import Pie Chart
import 'package:pie_chart/pie_chart.dart';
Creating Pie Chart
final pieColor = <Color>[
Color(0XFFde5246),
Color(0XFF4285F4),
Color(0XFF1aa260),
];Now we are chaging pie chart Color.
Change Pie Chart Colors
Parse Double Value into Pie Chart
"Died": double.parse(snapshot.data!.deaths.toString()),
Hope you understand this concept. Let's Animate Our Pie Chart
Animate Pie Chart
If you want to deep drive into Animation Controlling then you can read our Animtion Builder tutorial here. To animate Pie chart you dont need to know the Animation Builder or controler, Simply add this line after colorList into Pie chart.
animationDuration: Duration(seconds: 1),
Discover More on Pie Chart
Show data in percent.
chartValuesOptions: const ChartValuesOptions(showChartValuesInPercentage: true),
Show titles left side of Pie Charts
legendOptions: const LegendOptions(legendPosition: LegendPosition.left),
Convert Pie Chart into Ring/Donut Chart
chartType: ChartType.ring,
Full Code
import 'package:flutter/material.dart';
import 'package:pie_chart/pie_chart.dart';
class MyChart extends StatefulWidget {
const MyChart({super.key});
@override
State<MyChart> createState() => _MyChartState();
}
final pieColor = <Color>[
const Color(0XFFde5246),
const Color(0XFF4285F4),
const Color(0XFF1aa260),
];
class _MyChartState extends State<MyChart> {
@override
Widget build(BuildContext context) {
return PieChart(
colorList: pieColor,
chartType: ChartType.ring,
animationDuration: Duration(seconds: 1),
chartValuesOptions: const ChartValuesOptions(showChartValuesInPercentage: true),
legendOptions: const LegendOptions(legendPosition: LegendPosition.left),
dataMap: const {
"Died": 30,
"Affected": 45,
"Secure": 25,
},
);
}
}




0 Comments
No Link Share ! No Spaming