角度nvD3堆积面积图数据系列格式



我正在尝试在nvD3堆叠面积图中使用我自己的数据。Angular nvD3站点的样本数据格式如下:

[{
"key":"Series 1",
"values":[[1025409600000,0],[1028088000000,-6.3382185140371]]
},
{
"key":"Series 2",
"values":[[1025409600000,0],[1028088000000,0]]
}]

我的数据库中有以下格式的数据:

[{
"Loc_Cnt":6,"Num_Cars":552,"Num_Employees":34,"active_month":"2017-10-01T00:00:00"
},
{
"Loc_Cnt":4,"Num_Cars":252,"Num_Employees":14,"active_month":"2017-11-01T00:00:00"
}]

我试图从我的数据中绘制出三个系列(系列1:Flt_Cnt,系列2:Num_Cars,系列3:Num_Eemployees)。对于每个系列,X轴值为active_month日期,Y轴值为系列值。

如何A)轻松地将数据转换为样本数据,或者B)按AngularJs nvd3图表中的原样使用数据?我觉得数组上的.forEach对于更大的数据集来说效率不高,也不那么容易读取。我试图以某种方式使用d3.nest,但一直无法获得正确的格式。谢谢你的帮助!

它并不优雅,但我强行找到了解决方案。如果有更好的解决方案,请告诉我。

var Loc_Cnt = [];
var Num_Cars = [];
var Num_Employees = [];
var obj = {};
//arr is the array of values in my format
arr.forEach(function (element) {
//need the date in milisecond format
var date = new Date(element.active_month);
var time = date.getTime();

//load corresponding arrays
Loc_Cnt.push([time, element.Loc_Cnt]);
Num_Cars.push([time, element.Num_Cars]);
Num_Employees.push([time, element.Num_Employees]);
});
//load each key/values pair into new object
obj["Loc_Cnt"] = Loc_Cnt;
obj["Num_Cars"] = Num_Cars;
obj["Num_Employees"] = Num_Employees;
//d3.entries creates an object of key/VALUEs
arrRollup = d3.entries(obj);
//change the key word values to value
var i;
for (i = 0; i < arrRollup.length; i++) {
arrRollup[i].values = arrRollup[i]['value'];
delete arrRollup[i].value;
}

最新更新