我正在尝试使用来自多个本地保存的json文件的数据来构建一个高图表条形图,这有点麻烦。为简单起见,我要遍历所有文件,搜索特定的字符串,然后将计数用作图形的数据。我已经这样处理了:
options.series[0].name = 'Test';
options.series[0].data = [];
//Loop over the different local files and do a search count in each
var localfiles = new Array('localfile1.json', 'localfile2.json');
for (var i=0; i<locfiles.length; i++) {
//do a count here
$.getJSON(localfiles[i], function(data) {
var count = 0;
var index = 0;
var entry;
for (index = 0; index < data.length; ++index) {
entry = data[index];
if (entry.searchkey == "searchstring") {
count ++;
}
options.series[0].data.push(count);
});
});
var chart = new Highcharts.Chart(options);
我意识到我没有正确传递选项数组。但是我不确定应该用什么方式编写代码。有什么建议吗?
提前致谢。
要获得2个小节,您需要将options.series[0].data.push(count);
第二个小节放在外面,否则最终会长出很多小节
options.series[0].name = 'Test';
options.series[0].data = [];
//Loop over the different local files and do a search count in each
var localfiles = new Array('localfile1.json', 'localfile2.json');
for (var i=0; i<locfiles.length; i++) {
//do a count here
$.getJSON(localfiles[i], function(data) {
var count = 0;
var index = 0;
var entry;
for (index = 0; index < data.length; ++index) {
entry = data[index];
if (entry.searchkey == "searchstring") {
count ++;
}
});
options.series[0].data.push(count);
});
var chart = new Highcharts.Chart(options);
这样,您将为每个json文件获得1条
回答你的评论
您可以使用 addSeries
var series1 = {
data: [],
name: ""
}
chart.addSeries(series1);
如果要删除所有以前的系列,可以执行此操作
while(chart.series.length > 0){
chart.series[0].remove(true);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句