繁体   English   中英

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

[英]How to insert json within an array into 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插入高图系列?

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

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

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

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

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

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

暂无
暂无

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

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