javascript 如何将json解析成highcharts
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10499431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to parse json into highcharts
提问by nelsonvarela
The following:
下列:
$.get('/api/ajax_prices/' + product +'/', afterEndexPricesLoaded)
Returns a json object
返回一个 json 对象
{"aaData": [
[1, "70.1700", "2008-12-29 11:23:00"],
[2, "70.2600", "2008-12-29 16:22:00"],
[3, "70.6500", "2008-12-30 11:30:00"],
[4, "70.8700", "2008-12-30 16:10:00"],
[5, "70.5500", "2009-01-02 11:09:00"],
[6, "70.6400", "2009-01-02 16:15:00"]
]}
I am trying to parse the json string into highchart
我正在尝试将 json 字符串解析为 highchart
This is the code I am trying it with (data = above json string):
这是我正在尝试使用的代码(数据 = json 字符串上方):
function afterPricesLoaded(data, textStatus, xhr) {
var options = {
chart: {
renderTo: 'graph'
},
title: {
text: 'Some text',
style: {
color: '#888888'
}
},
xAxis: {
type: 'datetime'
}
/*
series: [{
data: [
[Date.UTC(2011, 0, 0), 29.9],
[Date.UTC(2012, 0, 0), 71.5],
[Date.UTC(2013, 0, 0), 106.4]
]}
]*/
};
new Highcharts.Chart(options)
}
How can I get the data into highchart series?
如何将数据放入 highchart 系列?
Edit
编辑
Graphtype= http://www.highcharts.com/demo/line-basic
Example= http://jsfiddle.net/kevink/9q4AT/
Graphtype= http://www.highcharts.com/demo/line-basic
示例= http://jsfiddle.net/kevink/9q4AT/
回答by LukeGT
Is this what you're looking for?
这是你要找的吗?
http://jsfiddle.net/TWF6N/349/
http://jsfiddle.net/TWF6N/349/
$(function () {
data = {"aaData": [
[1, "70.1700", "2008-12-29 11:23:00"],
[2, "70.2600", "2008-12-29 16:22:00"],
[3, "70.6500", "2008-12-30 11:30:00"],
[4, "70.8700", "2008-12-30 16:10:00"],
[5, "70.5500", "2009-01-02 11:09:00"],
[6, "70.6400", "2009-01-02 16:15:00"]
]};
newData = data.aaData.map( function(row) {
return [ new Date(row[2]).getTime(), parseInt(row[1]) ];
});
console.log(newData);
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container'
},
xAxis: {
type: 'datetime'
},
series: [{
data: newData
}]
});
});