我通过引用json数据在highcharts中绘制多个系列。 json数据配置如下:
{"data": {"words with friends": [[1365034819.212261, 107], [1366071619.212278, 224]], "snacksss": [[1364775619.212285, 786], [1366849219.212291, 445]], "ticktackloot": [[1365207619.212297, 85], [1367799619.212303, 803]], "bike race": [[1364343619.212309, 293], [1367886019.212315, 572]]}}
其中每个字典中的第一个也是唯一一个元素是列表列表。每个列表列表都包含一个带有时间戳和任意值的列表。每个字典的关键代表一个系列。问题是该系列仅绘制每个列表列表中的最后一个列表。时间戳在x轴上,y上的任意值。我最终获得了分别为224,445,803和572的y分。我希望它们全部显示出来。代码如下:
var chart; // global
function requestData()
{
$.ajax(
{
url: 'test.json',
success: function(response)
{
// SINCE response IS TURNED INTO AN OBJECT LITERAL
// ASSOCIATIVE ARRAY, WE CAN ACCESS data
for (var key in response.data)
{
//extract every timestamp and multiply it by 1000
//because JS processes timestamps in microseconds.
//Reinsert in array
for(var counter=0;counter<response.data[key].length;counter++)
{
insert_array=[]
//insert_array.push([response.data[key][counter][0]*1000,response.data[key][counter][1]])
insert_array.push([response.data[key][counter][0]*1000,response.data[key][counter][1]])
if(counter==response.data[key].length-1)
{
var seriesData=insert_array;
var seriesName = key;
console.log(" processing " + seriesName + "=" + seriesData);
var series =
{
name: seriesName,
data: seriesData,
lineWidth: 15
};
chart.addSeries(series);
}
}
}
},
cache: false
});
}
$(document).ready(function()
{
console.log("preparing document...");
chart = new Highcharts.Chart(
{
chart:
{
renderTo: 'container1',
type: 'bar',
events:
{
load: requestData
}
},
title:
{
text: 'Top Offers'
},
xAxis:
{
type: 'datetime',
title:
{
text: 'Offers'
}
},
yAxis:
{
title:
{
text: 'Rewards'
}
},
series: []
});
});
答案 0 :(得分:1)
尝试像这样改变cody内循环:
//extract every timestamp and multiply it by 1000 because JS processes timestamps in microseconds. Reinsert in array
insert_array=[]
for(var counter=0;counter<response.data[key].length;counter++)
{
//insert_array.push([response.data[key][counter][0]*1000,response.data[key][counter][1]])
insert_array.push([response.data[key][counter][0]*1000,response.data[key][counter][1]])
...
这样,insert_array
变量将在每个列表中定义一次并正确填充;使用实际代码,变量在每个循环中都被清除,因此它只携带最后一个值。