0.3.0 • Published 8 years ago

chiasm-data-reduction v0.3.0

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

chiasm-data-reduction

A Chiasm component for aggregation and filtering data.

This module is a thin wrapper around the data-reduction module that exposes its functionality within the Chiasm runtime environment.

The properties "aggregate" and "filter" are made available as public properties. These properties correspond to the options passed into data-reduction. The component expects the property "datasetIn" to be set to a dataset object (such as one generated by chiasm-dsv-dataset). The data reduction will be performed on this dataset, and the output will be assigned to the "datasetOut" property.

Example Use

The following code snippet is from the tests for this module, and shows how the component can be instantiated and used within a Chiasm instance.

var chiasm = Chiasm();
chiasm.plugins.dataReduction = ChiasmDataReduction;

chiasm.setConfig({
  reduction: {
    plugin: "dataReduction",
    state: {
      aggregate: {
        dimensions: [{
          column: "foo"
        }],
        measures: [{
          outColumn: "total", 
          operator: "count"
        }]
      }
    }
  }
});

chiasm.getComponent("reduction").then(function (reduction){
  reduction.datasetIn = {
    data: [
      { foo: "A", bar: 1 },
      { foo: "A", bar: 8 },
      { foo: "A", bar: 6 }, // A sum = 15, count = 3
      { foo: "B", bar: 4 },
      { foo: "B", bar: 3 }, // B sum = 7, count = 2
      { foo: "C", bar: 6 },
      { foo: "C", bar: 1 },
      { foo: "C", bar: 3 },
      { foo: "C", bar: 6 },
      { foo: "C", bar: 4 } // C sum = 20, count = 5
    ]
  };
  reduction.when("datasetOut", function (result){
    assert.equal(result.data.length, 3);
    assert.equal(where(result, "foo", "A")[0].total, 3);
    assert.equal(where(result, "foo", "B")[0].total, 2);
    assert.equal(where(result, "foo", "C")[0].total, 5);
    assert.equal(where(result, "foo", "A")[0].total, 3);
    done();
  });
});

function where(result, column, value){
  return result.data.filter(function (d) {
    return d[column] === value;
  });
}