如何在Highcharts中检索/显示标题、单位、版权以及JSON数据
我已经成功地实现了JSONP请求的代码,检索了多个国家的数据,并将它们显示为图表中的线条 但是,我还需要从JSON中获取标题、单位、版权等,以便能够在图形上显示这些元素 现在,我想知道如何才能做到这一点 JSON响应可能如下所示:如何在Highcharts中检索/显示标题、单位、版权以及JSON数据,json,highcharts,jsonp,series,Json,Highcharts,Jsonp,Series,我已经成功地实现了JSONP请求的代码,检索了多个国家的数据,并将它们显示为图表中的线条 但是,我还需要从JSON中获取标题、单位、版权等,以便能够在图形上显示这些元素 现在,我想知道如何才能做到这一点 JSON响应可能如下所示: [ [ "series", [ { "data": [
[
[
"series",
[
{
"data": [
[
2007,
2239300
],
[
2008,
2237490
],
[
2009,
2167070
],
[
2010,
2204450
]
],
"name": "France"
},
{
"data": [
[
2007,
2324234
],
[
2008,
3456352
],
[
2009,
1241422
],
[
2010,
4543231
]
],
"name": "Germany"
}
]
],
[
"title",
{
"text": "Title here"
}
],
[
"yAxis",
{
"text": "The units here"
}
]
]
$.getJSON(url, {selectedCountries: "France,Germany,Switzerland", type: "jsonp"})
.done(function(data)
{
options.series = data;
var chart = new Highcharts.Chart(options);
})
.fail(function(jqxhr, textStatus, error)
{
var err = textStatus + ", " + error;
console.log( "Request Failed: " + err );
})
options.series = data['series']['data'];
options.title = data['title'];
我客户的代码需要更改。目前看起来是这样的:
[
[
"series",
[
{
"data": [
[
2007,
2239300
],
[
2008,
2237490
],
[
2009,
2167070
],
[
2010,
2204450
]
],
"name": "France"
},
{
"data": [
[
2007,
2324234
],
[
2008,
3456352
],
[
2009,
1241422
],
[
2010,
4543231
]
],
"name": "Germany"
}
]
],
[
"title",
{
"text": "Title here"
}
],
[
"yAxis",
{
"text": "The units here"
}
]
]
$.getJSON(url, {selectedCountries: "France,Germany,Switzerland", type: "jsonp"})
.done(function(data)
{
options.series = data;
var chart = new Highcharts.Chart(options);
})
.fail(function(jqxhr, textStatus, error)
{
var err = textStatus + ", " + error;
console.log( "Request Failed: " + err );
})
options.series = data['series']['data'];
options.title = data['title'];
我想它一定会变成这样:
[
[
"series",
[
{
"data": [
[
2007,
2239300
],
[
2008,
2237490
],
[
2009,
2167070
],
[
2010,
2204450
]
],
"name": "France"
},
{
"data": [
[
2007,
2324234
],
[
2008,
3456352
],
[
2009,
1241422
],
[
2010,
4543231
]
],
"name": "Germany"
}
]
],
[
"title",
{
"text": "Title here"
}
],
[
"yAxis",
{
"text": "The units here"
}
]
]
$.getJSON(url, {selectedCountries: "France,Germany,Switzerland", type: "jsonp"})
.done(function(data)
{
options.series = data;
var chart = new Highcharts.Chart(options);
})
.fail(function(jqxhr, textStatus, error)
{
var err = textStatus + ", " + error;
console.log( "Request Failed: " + err );
})
options.series = data['series']['data'];
options.title = data['title'];
但这不起作用。谁能给我一个提示我该怎么做?非常感谢 好了,终于开始了。必须将JSON作为一个对象(而不是数组,也不是字符串)传递(因此,在对象周围没有像“或”这样的引号!)。这就像一个符咒 代码如下:
$(function () {
var options = {
chart: {
renderTo: 'container',
type: 'spline',
marginBottom: 50
},
series: [{}]
};
data = {
"title": {
"text": "Here goes the title"
},
"yAxis": {
"title": {
"text": "Here go the units"
}
},
"series": [{
"name": "France",
"data": [[2006,2189260],[2007,2239300],[2008,2237490],[2009,2167070],[2010,2204450]]
}]
};
options.series = data["series"];
options.title = data["title"];
options.yAxis = data["yAxis"];
var chart = new Highcharts.Chart(options);
});
非常感谢Sebastian Bochan的大力支持!因此您需要在json中准备正确的表单(与图表配置相同,包括数据),加载它并运行图表。@Sebastian:谢谢。我以为它会这样工作。但是,为了让Highcharts能够运行它,正确的json是什么呢?上面的json代码是正确的。我想在客户端,我需要一个不同于“options.series=data”的命令你能更具体一点吗?那真的会有帮助。Thx很多!我说的JSON是指带有配置的返回对象,比如{“xAxis”:{“title”:{“text”:“aaa”},“series”:[{“data”:[1,2,3]}]},然后你可以加载它并在回调中初始化图表(如你所提到的).Thx。但我仍然不明白。您的JSON括号看起来与我从JSON调用中收到的括号不同…我提出了,这可能有助于我理解这个问题。非常感谢您的帮助!