dc.js使用多列行图计算平均值 [英] dc.js calculate averages with multi column rowchart
问题描述
我正在使用如何从多列创建行图。
但是我需要获取数据的平均值。我通常通过值访问器执行此操作但不确定如何使用此功能执行此操作。
However I need to get the averages of the data. I normally do this via value accessor but am unsure of how to do this with this functionality.
function regroup(dim, cols) {
var _groupAll = dim.groupAll().reduce(
function(p, v) { // add
cols.forEach(function(c) {
p[c] += v[c];
});
return p;
},
function(p, v) { // remove
cols.forEach(function(c) {
p[c] -= v[c];
});
return p;
},
function() { // init
var p = {};
cols.forEach(function(c) {
p[c] = 0;
});
return p;
});
return {
all: function() {
// or _.pairs, anything to turn the object into an array
return d3.map(_groupAll.value()).entries();
}
};
}
我只需要能够得到每列的当前总和并除以它取决于基于当前过滤器状态的行数。
I just need to be able to get the current sum of each column and divide it by the count of the rows based on the current filter state.
代码类似于这个小提琴中的代码多列小提琴
The code is similar to the one in this fiddle multi column fiddle
推荐答案
似乎获取记录数的最简单方法是使用默认减少创建另一个 crossfilter.groupAll()
:
It seems that the easiest way to get the count of records is to create another crossfilter.groupAll()
with the default reduction:
var _recordCount = dim.groupAll();
然后您可以在旋转结果时将每个总和除以当前计数:
Then you can divide each sum by the current count when rotating the results:
all: function() {
var count = _recordCount.value();
// or _.pairs, anything to turn the object into an array
return d3.map(_groupAll.value())
.entries().map(function(kv) {
return {key: kv.key, value: kv.value / count};
});
}
这是更新的小提琴: http://jsfiddle.net/37uET/32/
这篇关于dc.js使用多列行图计算平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!