Flutter Charts

 

Flutter Charts

A chart is a graphical representation of data where data is represented by a symbol such as a line, bar, pie, etc. In Flutter, the chart behaves the same as a normal chart. We use a chart in Flutter to represent the data in a graphical way that allows the user to understand them in a simple manner. We can also plot a graph to represents the rise and fall of our values. The chart can easily read the data and helps us to know the performance on a monthly or yearly basis whenever we need it.

Supported Chart Types in Flutter

Flutter supports mainly three types of charts, and each chart comes with several configuration options. The following are the chart used in Flutter application:

  1. Line Chart
  2. Bar Chart
  3. Pie and Donut Chart

Line Chart

A line chart is a graph that uses lines for connecting individual data points. It displays the information in a series of data points. It is mainly used to track changes over a short and long period of time.

We can use it as below:

  1. LineChart(  
  2.   LineChartData(  
  3.     // write your logic  
  4.   ),  
  5. );  

Bar Chart

It is a graph that represents the categorical data with rectangular bars. It can be horizontal or vertical.

We can use it as below:

  1. BarChart(  
  2.   BarChartData(  
  3.     // write your logic  
  4.   ),  
  5. );  

Pie or Donut Chart

It is a graph that displays the information in a circular graph. In this graph, the circle is divided into sectors, and each shows the percentage or proportional data.

We can use it as below:

  1. PieChart(  
  2.   PieChartData(  
  3.     // write your logic  
  4.   ),  
  5. );  

Let us understand it with the help of an example.

Example

Open the IDE and create the new Flutter project. Next, open the project, navigate to the lib folder, and open the pubspec.yaml file. In this file, we need to add the chart dependency. Flutter provides several chart dependency, and here we are going to use fl_chart dependency. So add it as below:

  1. dependencies:  
  2.   flutter:  
  3.     sdk: flutter  
  4.   fl_chart: ^0.10.1   

After adding the dependency, click on the get packages link shown on the screen's top left corner. Now, open the main.dart file and replace it with the below code:

  1. import 'package:flutter/material.dart';  
  2. import 'package:fl_chart/fl_chart.dart';  
  3.   
  4. void main() => runApp(MyApp());  
  5.   
  6. /// This Widget is the main application widget.  
  7. class MyApp extends StatelessWidget {  
  8.   @override  
  9.   Widget build(BuildContext context) {  
  10.     return MaterialApp(  
  11.       home: HomePage(),  
  12.     );  
  13.   }  
  14. }  
  15.   
  16. class HomePage extends StatelessWidget {  
  17.   @override  
  18.   Widget build(BuildContext context) {  
  19.     return Scaffold(  
  20.       appBar: AppBar(  
  21.         title: const Text('Flutter Chart Example'),  
  22.           backgroundColor: Colors.green  
  23.       ),  
  24.       body: Center(  
  25.         child: Column(  
  26.           mainAxisAlignment: MainAxisAlignment.center,  
  27.           children: <Widget>[  
  28.             LineCharts(),  
  29.             Padding(  
  30.               padding: const EdgeInsets.all(16.0),  
  31.               child: Text(  
  32.                 "Traffic Source Chart",  
  33.                   style: TextStyle(  
  34.                       fontSize: 20,  
  35.                       color: Colors.purple,  
  36.                       fontWeight: FontWeight.w700,  
  37.                       fontStyle: FontStyle.italic  
  38.                   )  
  39.               )  
  40.             ),  
  41.           ],  
  42.         ),  
  43.       ),  
  44.     );  
  45.   }  
  46. }  
  47.   
  48. class LineCharts extends StatelessWidget {  
  49.   @override  
  50.   Widget build(BuildContext context) {  
  51.     const cutOffYValue = 0.0;  
  52.     const yearTextStyle =  
  53.     TextStyle(fontSize: 12, color: Colors.black);  
  54.   
  55.     return SizedBox(  
  56.       width: 360,  
  57.       height: 250,  
  58.       child: LineChart(  
  59.         LineChartData(  
  60.           lineTouchData: LineTouchData(enabled: false),  
  61.           lineBarsData: [  
  62.             LineChartBarData(  
  63.               spots: [  
  64.                 FlSpot(01),  
  65.                 FlSpot(11),  
  66.                 FlSpot(23),  
  67.                 FlSpot(34),  
  68.                 FlSpot(35),  
  69.                 FlSpot(44)  
  70.               ],  
  71.               isCurved: true,  
  72.               barWidth: 2,  
  73.               colors: [  
  74.                 Colors.black,  
  75.               ],  
  76.               belowBarData: BarAreaData(  
  77.                 show: true,  
  78.                 colors: [Colors.lightBlue.withOpacity(0.5)],  
  79.                 cutOffY: cutOffYValue,  
  80.                 applyCutOffY: true,  
  81.               ),  
  82.               aboveBarData: BarAreaData(  
  83.                 show: true,  
  84.                 colors: [Colors.lightGreen.withOpacity(0.5)],  
  85.                 cutOffY: cutOffYValue,  
  86.                 applyCutOffY: true,  
  87.               ),  
  88.               dotData: FlDotData(  
  89.                 show: false,  
  90.               ),  
  91.             ),  
  92.           ],  
  93.           minY: 0,  
  94.           titlesData: FlTitlesData(  
  95.             bottomTitles: SideTitles(  
  96.                 showTitles: true,  
  97.                 reservedSize: 5,  
  98.                 textStyle: yearTextStyle,  
  99.                 getTitles: (value) {  
  100.                   switch (value.toInt()) {  
  101.                     case 0:  
  102.                       return '2016';  
  103.                     case 1:  
  104.                       return '2017';  
  105.                     case 2:  
  106.                       return '2018';  
  107.                     case 3:  
  108.                       return '2019';  
  109.                     case 4:  
  110.                       return '2020';  
  111.                     default:  
  112.                       return '';  
  113.                   }  
  114.                 }),  
  115.             leftTitles: SideTitles(  
  116.               showTitles: true,  
  117.               getTitles: (value) {  
  118.                 return '\$ ${value + 100}';  
  119.               },  
  120.             ),  
  121.           ),  
  122.           axisTitleData: FlAxisTitleData(  
  123.               leftTitle: AxisTitle(showTitle: true, titleText: 'Value', margin: 10),  
  124.               bottomTitle: AxisTitle(  
  125.                   showTitle: true,  
  126.                   margin: 10,  
  127.                   titleText: 'Year',  
  128.                   textStyle: yearTextStyle,  
  129.                   textAlign: TextAlign.right)),  
  130.           gridData: FlGridData(  
  131.             show: true,  
  132.             checkToShowHorizontalLine: (double value) {  
  133.               return value == 1 || value == 2 || value == 3 || value == 4;  
  134.             },  
  135.           ),  
  136.         ),  
  137.       ),  
  138.     );  
  139.   }  
  140. }  

Output:

When we run the app in the device or emulator, we will get the UI of the screen similar to the below screenshot:

Flutter Charts


Post a Comment

Previous Post Next Post