我在 php 中生成了一个这样的数组,json 嵌套在 chartData
下的数组中:
array:1 [▼
0 => array:18 [▼
"id" => 1
"chart_title" => "A title"
"chart_type" => "line"
"chartData" => "[[1470406561000,2116],[1470406561000,2116],[1470406562000,2116]]"
]
]
我想访问此数组中的 chartData
json 并将其插入到 HighCharts 中系列。
我试过使用:
window['chart' + chartData.id].series[0].addPoint(chartData, true, shift);
还有一个forEach
循环:
chartData.forEach(function(dataPoint){
console.log(dataPoint);
window['chart' + chartData.id].series[0].addPoint(dataPoint[0], true);
dataPoint.slice(0,30).forEach(function(point){
window['chart' + chartData.id].series[0].addPoint(point, true, shift);
});
});
两者都不会在控制台中显示任何错误,并且值不会显示在图表上。如果我 console.log(dataPoint);
我得到看起来像正确输出的内容:
[[1470406561000,2116],[1470406561000,2116],[1470406562000,2116]]
我如何将 chartData
json 插入 highchart系列?
最佳答案
我的问题是 JSON 没有被 View 上的 jQuery 解析,并且基本上将其原始传递给 highcharts 系列。通过添加 jQuery.parseJSON(chartData);
它能够正确解析并显示在图表上。
关于javascript - 如何将数组中的 json 插入 highcharts?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39138464/