1

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

Community
  • 1
  • 1
Simon
  • 205
  • 1
  • 3
  • 15

1 Answers1

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/

Gordon
  • 19,811
  • 4
  • 36
  • 74
  • Hrrrrmmm, I think that should be `ndx.groupAll()` as described. Endlessly confusing the subtleties of which objects observe what filters. – Gordon Dec 15 '16 at 06:35