1
votes

This feels like it should be easy :/

The crossfilter API says I can run a reduce on groupAll: https://github.com/square/crossfilter/wiki/API-Reference#groupAll_reduce

But I cannot get it to work. I've tried facts.groupAll() where var facts = crossfilter(data); And I've tried all.reduce() where var all = facts.groupAll(). I've tried with and without brackets and googled for examples. Does anyone know of a working example? I want a single output across all my rows.

I realise my reduce function isn't complete and looks complicated. It works fine reducing a dimension, but gives undefined for groupAll on facts.

Thanks

  var accumGrp = facts.groupAll().reduce(
    function(p,v) {
      for (var i=0; i<supplierFields[0].length; i++) {
        if (!p.population[supplierFields[0][i]]) { p.population[supplierFields[0][i]] = []; }
        p.population[supplierFields[0][i]].push(v[supplierFields[0][i]+'_l']);
      }
      return p;
    },
    function(p,v) { return p; },
    function() {
      var obj = {};
      obj.population = {};
      obj.highlight = {};
      return obj;
    }
  );

  print_filter('accumGrp');
1

1 Answers

1
votes

Your basic problem here is probably that you need to call groupAll.value() in order to execute the group aggregation, whereas regular groups calculate aggregations at the time of definition or data load, not when you query them with group.top or group.all.

It looks like your basic approach is otherwise correct, and I can't see what print_filter does, so it's just a guess, but try calling console.log(accumGrp.value()) at the end of your script and see if it works.

If not, here is a short working example to reference:

var data = [1,2,3,4]

var cf = crossfilter(data)
var grp = cf.groupAll().reduce(
  function(p, d) { return p + d },
  function(p, d) { return p - d },
  function() { return 0 }
)

console.log(grp.value())

This prints 10. And here is a working JSFiddle where you can try things out (with a few more console statements that might help look into what is going on): https://jsfiddle.net/esjewett/39xgn5ah/1/