Search code examples
javascriptcrossfilter

crossfilter2 dimension filter not working as expected


const crossfilter = require( 'crossfilter2');
const _ = require('lodash')
function randomGaussian() {
  return (
    Math.sqrt(-2 * Math.log(Math.random())) *
    Math.cos(2 * Math.PI * Math.random())
  );
}

const data = _.range(256).map(i => {
    // console.log(i, " -- ", randomGaussian() + 8);
    return [i, randomGaussian() + 8];
  });
    const filter = crossfilter(data);
    const value1 = filter.dimension(d => d[1]);
  const valueGroup = value1.group().reduceSum(d => d[1]);

value1.filter([7.2,8.1])
console.log(value1.top(Infinity).length)
console.log(valueGroup.all().filter(d => d.value ).length)

I expect the group length to be same as the dimension length... Any idea what I am doing wrong?


Solution

  • Figured out that the dimension filter will not apply to the group as per the documentation.

    " Note: a grouping intersects the crossfilter's current filters, except for the associated dimension's filter. Thus, group methods consider only records that satisfy every filter except this dimension's filter. So, if the crossfilter of payments is filtered by type and total, then groupAll by total only observes the filter by type."