【发布时间】:2014-12-09 10:12:35
【问题描述】:
希望一切都好。我在通过 PHP 设置 JSON 数组并将其推送到 Highcharts 时遇到了一些麻烦。
目前我生成这样的数组:
$stack[] = array($commname => $countit);
$stack = json_encode($stack);
当我 print_r 数组时,我得到以下信息:
[{"原油":69},{"天然气":554},{"液化天然气 Gas":152},{"Power":40},{"Coal":10},{"Weather":21},{"Macroeconomics":67},{"Miscellaneous":45},{"Prices" :50},{"Freight":14},{"Forecasts":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 希望它采用以下格式:[["Crude oil", 35],["Natural Gas", 45] etc...]
关于我做错了什么的任何指针?谢谢!
G.
【问题讨论】:
-
在js部分将你的JSON转换成数组。
-
你得到什么错误?
标签: javascript php arrays json highcharts