JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 746
  • Score
    100M100P100Q121895F
  • License Apache 2.0

Reductio: Crossfilter groupings

Package Exports

  • reductio

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (reductio) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Reductio: Crossfilter grouping

Reductio is a library for generating Crossfilter reduce functions and applying them to Crossfilter groups. Aggregations are additive (so you can track more than one aggregation on a given group) and can depend on each other (the 'avg' aggregation requires that 'count' and 'sum' be specified).

Current aggregations supported are:

var data = crossfilter([...]);
var dim = data.dimension(...);
var group = dim.group();
var reducer;

reducer = reductio().count(true);
// Same as group.reduceCount()
reducer(group);

// accessorFunction must return a number
reducer = reductio().sum(accessorFunction);
// Same as group.reduceSum(accessor)
reducer(group);

// There is no need to use the intermediate 'reducer' variable if you are not going to re-use the reducer.

// .count(true) and .sum(...) must both be specified
reductio().avg(true)(group);

// Median value returned by accessor function within each group 
reductio().median(accessorFunction)(group);

// Histogram of values within grouping. Acts like d3.layout.histogram defined using bins(thresholds).
// https://github.com/mbostock/d3/wiki/Histogram-Layout
//
// This grouping should be usable anywhere d3.layout.histogram can be used. May be useful for small-
// multiples charts, or for use with the dc.js stack mixin.
//
// group.histogram is an array. Each element of the array is a sorted array of values returned by
// histogramValue that fall into that bin. Each element of the array also has properties, x, dx,
// and y, as defined in the d3.layout.histogram documentation.
reductio().histogramBins([0,2,6,10])                            // Bin thresholds
        .histogramValue(function(d) { return d.bar; })(group)   // Value to bin

Aggregations can be chained on a given instance of reductio. For example:

reductio().count(true)
    .sum(function(d) { return d.bar; })
    .avg(true)(group);

Basic use:

var data = crossfilter([
  { foo: 'one', bar: 1 },
  { foo: 'two', bar: 2 },
  { foo: 'three', bar: 3 },
  { foo: 'one', bar: 4 },
  { foo: 'one', bar: 5 },
  { foo: 'two', bar: 6 },
]);

var dim = data.dimension(function(d) { return d.foo; });
var group = dim.group();

// Equivalent to reductio().avg(function(d) { return d.bar; }), which sets the .sum() and .count() values.
var reducer = reductio()
    .count(true)
    .sum(function(d) { return d.bar; })
    .avg(true);

// Now it should track count, sum, and avg.
reducer(group);

group.top(Infinity);
// [ { key: 'one', value: { count: 3, sum: 10, avg: 3.3333333 },
//   { key: 'two', value: { count: 2, sum: 8, avg: 4 },
//   { key: 'three', value: { count: 1, sum: 3, avg: 3 } ]

We also support exception aggregation. For our purposes, this means only aggregating once for each unique value that the exception accessor returns. So:

var data = crossfilter([
  { foo: 'one', bar: 'A', num: 1 },
  { foo: 'two', bar: 'B', num: 2 },
  { foo: 'three', bar: 'A', num: 3 },
  { foo: 'one', bar: 'B', num: 2 },
  { foo: 'one', bar: 'A', num: 1 },
  { foo: 'two', bar: 'B', num: 2 },
]);

var dim = data.dimension(function(d) { return d.foo; });
var group = dim.group();

var reducer = reductio()
    .exception(function(d) { return d.bar; })
    .exceptionCount(true)
    .exceptionSum(function(d) { return d.num; });

reducer(group);

group.top(Infinity);
// [ { key: 'one', value: { exceptionCount: 2, exceptionSum: 3 },    // 'bar' dimension has 2 values: 'A' and 'B'.
//   { key: 'two', value: { exceptionCount: 1, exceptionSum: 2 },    // 'bar' dimension has 1 value: 'B'.
//   { key: 'three', value: { exceptionCount: 1 , exceptionSum: 3} ] // 'bar' dimension has 1 value: 'A'.

Right now we support exceptionCount and exceptionSum, but it might also make sense to support other types of dependent aggregation. These types of aggregations are meant to help with a situation where you want to use Crossfilter on a flattened one-to-many or many-to-many relational model, which can result in redundant values. When using exceptionSum, make sure that for each value within a group that the exception accessor returns, the exceptionSum accessor returns an identical value, or results will be unpredictable.