从 JSON 添加系列到 highcharts
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6905531/
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
adding series to highcharts from JSON
提问by dvanderb
I'm trying to add a series to a highcharts chart from JSON data. The json has dates and y values:
我正在尝试从 JSON 数据向 highcharts 图表添加一个系列。json 有日期和 y 值:
{Date.UTC(2011,8,1): 47, Date.UTC(2011,8,2): 78}
and the javascript function I currently have, which adds the series but the dates don't seem to work, is:
和我目前拥有的 javascript 函数,它添加了系列但日期似乎不起作用,是:
function requestData() {
$.ajax({
url: 'chartData.php',
success: function(items) {
var series = {
id: 'series',
name: 'JSON Data',
data: []
}
$.each(items, function(itemNo, item) {
series.data.push(item);
});
chart.addSeries(series);
},
cache: false
});
}
Can anyone help me finish off this query to get the chart working? Thanks in advance for the help!
任何人都可以帮助我完成此查询以使图表正常工作吗?在此先感谢您的帮助!
EDIT: I SOLVED THIS - SEE ANSWER BELOW FOR HOW I DID IT
编辑:我解决了这个问题 - 请参阅下面的答案,了解我是如何做到的
回答by dvanderb
I figured this out. Here's how I did it in case anyone else has the same question:
我想通了。如果其他人有同样的问题,我是这样做的:
In my script that generates the JSON data, I did the following:
在生成 JSON 数据的脚本中,我执行了以下操作:
header('Content-type: text/json');
//Placeholder - random data for now
$x1 = "2011-8-1";
$y1 = rand(0, 100);
$x2 = "2011-8-2";
$y2 = rand(0, 100);
//Generate this array from database data
$arr = array($x1 => $y1, $x2 => $y2);
echo json_encode($arr);
Then, in the ajax script that adds the series to the chart, I did the following:
然后,在将系列添加到图表的 ajax 脚本中,我执行了以下操作:
function requestData() {
$.ajax({
url: 'chartData.php',
success: function(json) {
var series = {
id: 'series',
name: 'JSON Data',
data: []
}
$.each(json, function(date,value) {
xval = date.split("-");
x = Date.UTC(xval[0], xval[1] - 1, xval[2]);
series.data.push([
x,
value
]);
});
chart.addSeries(series);
},
cache: false
});
}
回答by Tomas Tornevall
In an example from a project I'm working on right now, I evaluate the series data from an array like this. Maybe that is cheating, but I at least got it to work :)
在我现在正在处理的一个项目的示例中,我评估了这样一个数组中的系列数据。也许那是作弊,但至少我让它起作用了:)
var xAxisCat = new Array();
var hSeries = new Array();
for (var datevote in contestantObject.contestants[cObj].votes)
{
xAxisCat.push(datevote);
hSeries.push(contestantObject.contestants[cObj].votes[datevote]);
}
var setSeries = "["+hSeries+"]";
And then, at the series creation I evaluate the created array instead:
然后,在系列创建时,我评估创建的数组:
series: [{
name: contestantObject.contestants[cObj].name,
data: eval(setSeries)
}]
回答by Manik Thakur
Check the response(JSON DATA) coming through ajax having datatype as object or not. Because highcharts will work it the response is of datatype as object.
检查通过 ajax 传入的响应(JSON DATA)是否具有数据类型作为对象。因为 highcharts 会起作用,所以响应的数据类型为对象。
If it is returning as string then, convert it into object using eval('(' + response + ')')
如果它作为字符串返回,则使用 eval('(' + response + ')') 将其转换为对象

