[英]Plotting averages with dc.js / crossfilter
I have some data like this 我有一些这样的数据
[
{type:"a", series:[0.1, 0.2, 0.2, 0.1, -0.1]},
{type:"b", series:[0.3, 0.4, 0.5, 0.6, 0.7]},
]
There is a dimension for type, then I'm doing a group and averaging out each series per type. 有一个类型的维,然后我进行分组并平均每种类型的每个序列。
Is there a nice way to plot the averaged series per type? 有没有一种很好的方法来绘制每种类型的平均序列? Obviously I want it to update with any new filters.
显然,我希望它使用任何新的过滤器进行更新。
I tried flattening out my data as 我尝试将数据扁平化为
[
{type:"a", index:0, value:0.1},
{type:"a", index:1, value:0.2},
...
]
and making index a dimension as well. 并同时将索引作为维度。 But it's getting a bit slow when I add more data;
但是,当我添加更多数据时,它会变得有点慢。 I never filter the index dimension, so adding it to crossfilter is just an extra cost;
我从不过滤索引维度,因此将其添加到交叉过滤器只是一项额外的费用; and finally it messes up the counts for each type.
最后,它弄乱了每种类型的计数。
Try this: 尝试这个:
var group = typeDimension.group().reduce(reduceAdd, reduceRemove, reduceInitial);
function reduceAdd(p, v) {
++p.count
p.sum += calculateAverage(v['series']);
p.avg = p.sum / p.count;
return p;
}
function reduceRemove(p, v) {
--p.count
p.sum -= calculateAverage(v['series']);
p.avg = p.sum / p.count;
return p;
}
function reduceInitial() {
return {count: 0, sum: 0, avg: 0};
}
function calculateAverage(numbers) {
var sum = 0;
number.forEach(function(num) {
sum += num;
});
return sum / numbers.length;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.