{}"chart":{}"result":[]0:{}"meta":{...} <-- I did not expand meta because I do not need this information"timestamp":[0 - 100][100 - 200][200 - 300][300 - 400][400 - 500][500 - 529]"indicators":{}"quote":[]0:{}"low":[0 - 100][100 - 200][200 - 300][300 - 400][400 - 500][500 - 529]"close":[0 - 100][100 - 200][200 - 300][300 - 400][400 - 500][500 - 529]"volume":[0 - 100][100 - 200][200 - 300][300 - 400][400 - 500][500 - 529]"open":[0 - 100][100 - 200][200 - 300][300 - 400][400 - 500][500 - 529]"high":[0 - 100][100 - 200][200 - 300][300 - 400][400 - 500][500 - 529]"error":NULL
My class for the JSON data is in the attached zip inside chart_data.dart
How do I format my array and datasource so I can display the values in my chart? |
// Changed the return type of the function to chart data list type
Future<List<SampleData>> getChartData() async {
//your configurations
if (sampleChartData.isNotEmpty) sampleChartData.clear();
for (int i = 0; i < chartData.chart.result.length; i++) {
for (int j = 0; j < chartData.chart.result[i].timestamp.length; j++) {
sampleChartData.add(SampleData(
timeStamp: DateTime.fromMicrosecondsSinceEpoch(
chartData.chart.result[i].timestamp[j])));
}
}
for (int i = 0; i < chartData.chart.result.length; i++) {
for (int j = 0;
j < chartData.chart.result[i].indicators.quote.length;
j++) {
for (int k = 0;
k < chartData.chart.result[i].indicators.quote[j].low.length;
k++) {
sampleChartData[k].open =
chartData.chart.result[i].indicators.quote[j].open[k];
sampleChartData[k].high =
chartData.chart.result[i].indicators.quote[j].high[k];
sampleChartData[k].low =
chartData.chart.result[i].indicators.quote[j].low[k];
sampleChartData[k].close =
chartData.chart.result[i].indicators.quote[j].close[k];
sampleChartData[k].volume =
chartData.chart.result[i].indicators.quote[j].volume[k];
}
}
}
// Returned the chart data list instead of the chartData Json array
return sampleChartData;
} |
|
// Changed the return type in the futurebuilder
FutureBuilder<List<SampleData>>(
future: getChartData(),
builder:
(BuildContext context, AsyncSnapshot<List<SampleData>> snapshot) {
if (snapshot.data != null) {
return SfCartesianChart(
// your configurations
series: <ChartSeries<SampleData, DateTime>>[
AreaSeries<SampleData, DateTime>(
gradient: gradients,
enableTooltip: true,
// Provided the snapshot.data which consists of the returned chart data list from
// the getChartData() method to the data source of the chart.
dataSource: snapshot.data,
xValueMapper: (SampleData time, _) => time.timeStamp,
yValueMapper: (SampleData price, _) => price.open,
borderWidth: 2,
opacity: .3,
borderColor: Colors.green,
borderDrawMode: BorderDrawMode.top,
animationDuration: 0),
],
);
}
return Container(
height: 0,
width: 0,
);
},
) |