Javascript 如何将数组中的json插入highcharts?
我在php中生成了这样一个数组,其中json嵌套在Javascript 如何将数组中的json插入highcharts?,javascript,php,jquery,json,highcharts,Javascript,Php,Jquery,Json,Highcharts,我在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
下的数组中:
array:1 [▼
0 => array:18 [▼
"id" => 1
"chart_title" => "A title"
"chart_type" => "line"
"chartData" => "[[1470406561000,2116],[1470406561000,2116],[1470406562000,2116]]"
]
]
我想访问这个数组中的chartData
json,并将其插入到一个系列中
我试过使用:
window['chart'+chartData.id]。系列[0]。添加点(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);
});
});
两者都不会在控制台中显示任何错误,并且值也不会显示在图表上。如果Iconsole.log(数据点)代码>我得到了正确的输出:
[[1470406561002116]、[1470406561002116]、[14704065620002116]
如何将chartData
json插入到一个系列中 使用jQuery,您可以这样尝试:
var dataPoint = [[1470406561000,2116],
[1470406561000,2116],
[1470406562000,2116]];
var chart = $('#chart2').highcharts();
chart.series[0].setData(dataPoint);
$('#chart2').highcharts().redraw();
请确保根据您的代码正确更改图表id,即#chart2
。我的问题是jQuery没有在视图上解析JSON,实际上是将其传递给highcharts系列raw。通过添加jQuery.parseJSON(chartData)代码>它能够正确解析并显示在图表上 感谢@vijayP,如果我在jquery中设置了dataPoint变量,但是如果我尝试使用vardataPoint=chartData这样的数组来设置它,那么我尝试了这个方法,没有任何问题代码>它像这样绘制图表,所以我现在无法访问您发布的图像。您可以console.log(chartData)
before语句dataPoint=chartData代码>并检查它。我控制台记录了数据,输出是[[1470406561002116],[1470406561002116],[14704065620002116]。
它和另一个一样对吗。。。?控制台有错误吗?从逻辑上讲,它应该可以工作代码>这就解决了它。谢谢你的帮助,维杰