Wag*_*sUK 1 javascript json getjson highcharts
基本上我有一个对象,它定义了一个Highchart图表的一些属性:
var options = {
chart: {
renderTo: 'container',
type: 'line',
marginRight: 130,
marginBottom: 25
},
title: {
text: 'Monthly Average Temperature',
x: -20 //center
},
subtitle: {
text: 'Source: WorldClimate.com',
x: -20
},
xAxis: {
categories: []
},
yAxis: {
title: {
text: 'Temperature (°C)'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function () {
return '<b>' + this.series.name + '</b><br/>' + this.x + ': ' + this.y + '°C';
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -10,
y: 100,
borderWidth: 0
},
series: [{
name: 'Tokyo',
data: aCosts
}]
};
Run Code Online (Sandbox Code Playgroud)
我想从文件(ajax调用)更新此对象,如下所示:
$.getJSON(
"handler.php",
function (oJSON) {
var sName,
sCost,
aRow;
for (sName in oJSON) {
aRow = [];
//categories = [];
if (oJSON.hasOwnProperty(sName)) {
aRow.push(sName);
categories.push(sName);
}
// Create the chart
var chart = new Highcharts.Chart(options);
}
}
);
Run Code Online (Sandbox Code Playgroud)
问题是,我无法将新值传递给选项,因为它声明未定义类别.我尝试过使用:
options.categories.push(sName)
options[categories].push(sName)
Run Code Online (Sandbox Code Playgroud)
没有人工作.
如何更新选项对象中的类别值?
假设options从$.JSON调用中可以看到,这应该工作:
options.xAxis.categories.push( sName );
Run Code Online (Sandbox Code Playgroud)
如果您查看options对象的结构,您会看到,它categories位于属性下方xAxis.因此你应该解决这个问题.