javascript - 如何将数组中的 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 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/

相关文章:

javascript - 下拉菜单位于页面中间,应根据位置打开列表

javascript - 基于关键代码的事件监听器无法使用边距正确移动框

javascript - 通过替换来修改字符串

javascript - 如何避免在每个测试文件中导入组件所需的所有内容?

javascript - 将 AJAX 调用添加到函数触发的弹出窗口阻止程序

javascript - JSON、数组和对象的调用

javascript - jQuery 动画不触发回调函数

php - mysqli 创建多个表

javascript - AJAX 和 JavaScript : suggestions about an interface

php - Laravel:如何通过键 ID 获取单个数据透视行