dc.js calculate averages with multi column rowchart

三世轮回 提交于 2019-12-08 05:53:58

问题


I am using working with the code from How to create a row chart from multiple columns.

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


回答1:


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

Here's the updated fiddle: http://jsfiddle.net/37uET/32/



来源:https://stackoverflow.com/questions/40627538/dc-js-calculate-averages-with-multi-column-rowchart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!