简体   繁体   中英

Highcharts: Looping JSON into Series Data

I am trying to loop a JSON string into my series data. However, I don't seem to be getting the data. What am I doing wrong?

for (var i = 0; i < jsonStrng.lowUsage.length; i++) {
    options.series[0].data = jsonStrng.healthy[i];
    chart = new Highcharts.Chart(options);
};

for (var j = 0; j < jsonStrng.healthy.length; j++) {
    options.series[1].data = jsonStrng.lowUsage[j];
    chart = new Highcharts.Chart(options);
};

for (var k = 0; k < jsonStrng.terrible.length; k++) {
    options.series[2].data = jsonStrng.terrible[k];
    chart = new Highcharts.Chart(options);
};

for (var ii = 0; ii < jsonStrng.buggy.length; ii++) {
    options.series[3].data = jsonStrng.buggy[ii];
    chart = new Highcharts.Chart(options);
};

Here is the JSFiddle

Do like below code :

Create an array and fill it with the data while looping into json array.Finally assign series.data to that array

var healthyData = [];
for (var i = 0; i < jsonStrng.lowUsage.length; i++) {
 healthyData.push(jsonStrng.healthy[i]); 
}
options.series[0].data = healthyData; 

Do this for all 3 series data and don't call chart function until all series are filled with data .After last series call following function

 chart = new Highcharts.Chart(options);

If you share your json data,. I will update your fiddle to make it working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM