我想动态创建图表的数据集。现在我有这样的东西,它按预期工作,但我不知道数组的长度,所以我不能使用这个,当我尝试在数据对象内部循环失败。任何sugestions?
var month=new Array("Jan","Feb","Mar","Apr","Maj","Jun","Jul","Aug","Sep","Okt","Nov","Dec");
var day =new Array();
var mon =new Array();
var pass =new Array();
var bug =new Array();
_.each(data, function(data){
var date = new Date(data['timestamp']);
mon.push(date.getMonth());
day.push(date.getDate());
pass.push(data['pass']);
bug.push(data['bug']);
});
var datasets = {
"Pass": {
label: "Pass",
data: [[""+month[mon[0]]+"-"+day[0]+"", pass[0]], [""+month[mon[1]]+"-"+day[1]+"", pass[1]], [""+month[mon[2]]+"-"+day[2]+"", pass[2]], [""+month[mon[3]]+"-"+day[3]+"", pass[3]], [""+month[mon[4]]+"-"+day[4]+"", pass[4]]]
},
"Bug": {
label: "Bug",
data: [[""+month[mon[0]]+"-"+day[0]+"", bug[0]], [""+month[mon[1]]+"-"+day[1]+"", bug[1]], [""+month[mon[2]]+"-"+day[2]+"", bug[2]], [""+month[mon[3]]+"-"+day[3]+"", bug[3]], [""+month[mon[4]]+"-"+day[4]+"", bug[4]]]
}
};
答案 0 :(得分:2)
在构建数据集之前构建数据对象。你不能在数据集内循环,但你可以在外面循环。
或者您可以从数据集中创建一个对象,然后使用JSON.Stringify()它。
编辑以显示如何执行第二步,代码可能不是100%正确,因此适应您的方案:
var dataset= new Object();
dataset.label = "Bug";
foreach(var i in DatestoAdd){
dataset.Dates.add(DatestoAdd[i]);
}
JSON.Stringify(dataset);