Reputation: 2215
I am new to Flutter and just started using fl_charts
which really looks fantastic. However I am struggling to move beyond demo LineChartSample1.
The sample code given here assigns values to spots as follows:
spots: [
FlSpot(1, 1.4),
FlSpot(2, 3.4),
FlSpot(3, 2),
FlSpot(4, 2.2),
FlSpot(5, 1.8),
],
In real life this obviously will have to be dynamic. Hence I need to assign values from an array of numbers like this:
myArray = [7578, 7547, 3578, 9875]
I can't figure out how to assign this array to spots assuming that first parameter to FlSpot is just a sequence like 0, 1, 2, 3 ... and second parameter coming from myArray.
I need something like FlSpot(x++, myArray[x])
in a loop.
Upvotes: 4
Views: 5516
Reputation: 21
First you can pass the data static to fl_chart means the point you want to show there like this:
spots: [ FlSpot(1, 1.4), FlSpot(2, 3.4), FlSpot(3, 2), FlSpot(4, 2.2), FlSpot(5, 1.8), ],
for adding the dynamic value from list you have to use the code
RxList analyticPlots = [].obs;
List.generate(widget.list.length, (i) {
analyticPlots.add(
FlSpot(i.toDouble(),
double.parse(widget.list[i].value.toString())));
});
Upvotes: 1
Reputation: 580
sorry for the late answer,
I will follow the fl-chart tag to answer earlier.
There is an operator called map
, it converts your current data to a new different data model.
you can use like this:
final List<double> yValues = [
1.3,
1,
1.8,
1.5,
2.2,
1.8,
3,
];
List<FlSpot> spots =
yValues.asMap().entries.map((e) {
return FlSpot(e.key.toDouble(), e.value);
}).toList(),
// now spots are [
// FlSpot(0, 1.3),
// FlSpot(1, 1),
// FlSpot(2, 1.8),
// FlSpot(3, 1.5),
// FlSpot(4, 2.2),
// FlSpot(5, 1.8),
// FlSpot(6, 3),
// ];
Upvotes: 6