[Flutter/dart] Create a bar chart
Overview
You may want to display user data as a bar chart in your app. This time, I will introduce how to create a bar chart with flutter.
Method
We use a library called charts_flutter. (official)
Install
Add the following to pubsec.yaml.
charts_flutter: ^0.9.0
Add the following at the top of dart file.
import 'package:charts_flutter/flutter.dart' as charts;
Since the class name that defines the color of the graph called Color in charts_flutter overlaps with the existing Color, it is recommended to import it with a name.
Creating data for graphs
Graph the following sample class.
class SampleData{
String name;
int value;
SampleData({this.name, this.value});
}
Converts a list of SampleData to data for a graph.
Widget makeChart(List<SampleData> datas){
List<charts.Color> _colors=
[charts.ColorUtil.fromDartColor(Colors.red),
charts.Color.fromHex(code: "#1976D2"),
charts.MaterialPalette.green.shadeDefault,
charts.Color.black];
List<charts.Series<SampleData, String>> _series=[
charts.Series<SampleData, String>(
id: "sample",
data: datas,
domainFn: (_sample, _)=> _sample.name,
measureFn: (_sample, _)=> _sample.value,
colorFn: (_sample, i)
=> i<_colors.length? _colors[i] : _colors[_colors.length-1]
)
];
domainFn is the method to determine the value of the x-axis and measureFn is the method to determine the value of the y-axis. The first argument is SampleData and the second argument is the index of the data.
colorFn is a method that determines the color of the graph. You can specify the color by converting from the Color class, specifying by code, or selecting from standard colors.
make chart
//continuation
return Container(
height: 400,
child: charts.BarChart(
_series,
domainAxis: new charts.OrdinalAxisSpec(
renderSpec: new charts.SmallTickRendererSpec(
labelRotation: 45, //1 rotate label
labelStyle: new charts.TextStyleSpec( //2 style of label
fontSize: 16
),
)
),
),
);
}
Pass the _series created above as the first argument of charts.BarChart.
After that, the axis label is set.
Part 1 is rotated 45 ° so that the x-axis labels do not overlap.
(Probably a bug, but if you set the rotation angle to 90 °, the height will be fixed)
Part 2 sets the format of the x-axis label.
There are many other settings you can make, so check the official site.
example
class MyHomePageState extends State<MyHomePage> {
List<SampleData> datas;
@override
void initState() {
datas=[
SampleData(name: "Alice", value: 90),
SampleData(name: "Bob", value: 85),
SampleData(name: "Charlie", value: 50),
SampleData(name: "Daniel", value: 100)
];
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('demo'),
),
body: Container(
padding: const EdgeInsets.all(16),
child: makeChart(datas),
)
);
}
Widget makeChart(List<SampleData> datas){
//略
}
}
You can make the following graph!
Recent Posts
See AllWhat want to do I want to create an input form using TextField. For example, if the input content is a monetary amount, I would like to...
What want to do There is a variable that remain unchanged once the initial value is determined. However, it cannot be determined yet when...
What want to do As the title suggests. Place two widgets in one line on the screen One in the center of the screen and the other on the...
Comments