如何将数组中的json插入highcharts?

时间:2016-08-25 06:48:22

标签: 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 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系列?

2 个答案:

答案 0 :(得分:1)

我的问题是JSON没有在视图上被jQuery解析,并且基本上将它传递给highcharts系列raw。添加jQuery.parseJSON(chartData);它能够正确解析并显示在图表上。

答案 1 :(得分:0)

使用jQuery,您可以尝试这样:

    var dataPoint =   [[1470406561000,2116],
                       [1470406561000,2116],
                       [1470406562000,2116]];  

    var chart = $('#chart2').highcharts();
    chart.series[0].setData(dataPoint);

    $('#chart2').highcharts().redraw();

请确保根据您的代码正确更改图表ID,例如#chart2