dc.js 用多列行图计算平均值

dc.js calculate averages with multi column rowchart

我正在使用来自 How to create a row chart from multiple columns 的代码。

但是我需要得到数据的平均值。我通常通过值访问器执行此操作,但不确定如何使用此功能执行此操作。

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();
        }
    };
}

我只需要能够获取每列的当前总和,并根据当前筛选器状态将其除以行数。

代码与本fiddlemulti column fiddle

类似

看来获取记录数的最简单方法是创建另一个 crossfilter.groupAll() 默认减少:

var _recordCount = dim.groupAll();

然后你可以在旋转结果时将每个总和除以当前计数:

    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};
        });
    }

这是更新后的 fiddle:http://jsfiddle.net/37uET/32/