[英]Cannot find proper JSON array syntax for Highcharts
希望一切都很好。 我在通過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}
您應該像這樣生成源數組:
$stack[] = array($commname, $countit);
或像這樣
$stack[] = array('name' => $commname, 'y' => $countit);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.