简体   繁体   English

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

[英]How to insert json within an array into highcharts?

I generate an array like so in php with json nested in the array under chartData : 我在php中生成了一个像这样的数组,json嵌套在chartData下的数组中:

array:1 [▼
  0 => array:18 [▼
    "id" => 1
    "chart_title" => "A title"
    "chart_type" => "line"
    "chartData" => "[[1470406561000,2116],[1470406561000,2116],[1470406562000,2116]]"
  ]
]

I want access the chartData json within this array and insert it into a HighCharts series. 我想访问此数组中的chartData json并将其插入HighCharts系列。

I've tried using: window['chart' + chartData.id].series[0].addPoint(chartData, true, shift); 我尝试过使用: window['chart' + chartData.id].series[0].addPoint(chartData, true, shift);

and also a forEach loop: 还有一个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);
                         });
                    });

Both don't show any errors in the console and the values don't show up on the chart. 两者都不会在控制台中显示任何错误,并且值不会显示在图表上。 If I console.log(dataPoint); 如果我是console.log(dataPoint); I get what looks like the correct output: [[1470406561000,2116],[1470406561000,2116],[1470406562000,2116]] 我看到正确的输出: [[1470406561000,2116],[1470406561000,2116],[1470406562000,2116]]

How would I insert the chartData json into a highchart series? 如何将chartData json插入高图系列?

My issue was the JSON was not being parsed by jQuery on the view and essentially passing it to the highcharts series raw. 我的问题是JSON没有在视图上被jQuery解析并且基本上将它传递给highcharts系列raw。 By adding jQuery.parseJSON(chartData); 通过添加jQuery.parseJSON(chartData); It was able to parse correctly and display on the chart. 它能够正确解析并显示在图表上。

With jQuery you can try like this: 使用jQuery,您可以尝试这样:

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

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

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

Please make sure you change chart id ie #chart2 correctly as per your code. 请确保根据您的代码正确更改图表ID,即#chart2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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