希望一切都好。我在通过PHP设置JSON数组并将其推送到Highcharts时遇到了一些麻烦。
目前我像这样生成数组:
$stack[] = array($commname => $countit);
$stack = json_encode($stack);
当我print_r数组时,我得到以下内容:
[{"原油":69},{"天然气":554},{"液体自然 天然气":152
},{"电力":40},{"煤炭":10},{"天气":21},{"宏观经济学":67},{"杂项":45},{"价格":50},{"运费":14},{"预测":16}]
然后我像这样将数组传递给 javascript:
var stack = <?php echo json_encode( $stack ) ?>;
.. 然后将其传递到以下 highcharts 数组中,如下所示:
var text = {
chart: {
plotBackgroundColor: null,
plotBorderWidth: 1,//null,
plotShadow: false
},
title: {
text: 'Browser market shares at a specific website, 2014'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
format: '<b>{point.name}</b>: {point.percentage:.1f} %',
style: {
color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
}
}
}
},
series: [{
type: 'pie',
name: 'Browser share',
data: [
]
}]
};
text.series[0].data.push(stack);
。但这行不通。我认为我的数组"堆栈"准备不当,因为 highcharts 希望它采用以下格式:[["原油",35],["天然气",45] 等...]
关于我做错了什么的任何指示?谢谢!
G.
试试这个jQuery.parseJSON
jQuery.parseJSON()
var stack = <?php echo json_encode( $stack ) ?>;
stack = jQuery.parseJSON(stack);
你有两种方式- 将 JSON 表单转换为此形式:
{name:"Crude Oil", y:69}
- 获取 JSON,然后使用循环并推送到新的系列数据数组,然后在 HighCharts 选项中引用它。
你应该像这样生成源数组:
$stack[] = array($commname, $countit);
或者像这样
$stack[] = array('name' => $commname, 'y' => $countit);