square / crossfilter

Fast n-dimensional filtering and grouping of records.
https://square.github.com/crossfilter/
Other
6.22k stars 1.3k forks source link

filter is not working #144

Closed abuscom-leonhard closed 9 years ago

abuscom-leonhard commented 9 years ago

I uses crossfilter version 1.3.11. I my understanding a grouping on a dimension should uses only those records that fulfill filters on ohter dimensions Therefor I expect the following test to be valid:

'use strict';

describe('Lib: crossfilter', function () {

var livingThings;

//
beforeEach(function () {
    livingThings = crossfilter([
        // Fact data.
        {name: "Rusty", type: "human", legs: 2},
        {name: "Alex", type: "human", legs: 2},
        {name: "Lassie", type: "dog", legs: 4},
        {name: "Spot", type: "dog", legs: 4},
        {name: "Polly", type: "bird", legs: 2},
        {name: "Fiona", type: "plant", legs: 0}
    ]);
}
);

it('should do something', function () {
    var dimType, dimLegs, dimName, cnt;
    dimType = livingThings.dimension(function (d) {
        return d.type;
    });

    dimLegs = livingThings.dimension(function (d) {
        return d.legs;
    });

    dimLegs.filter(2);
    cnt = dimType.group().reduceCount().top(Infinity);

    console.log(cnt);
    expect(cnt.length).toBe(3);

});

});

jasondavies commented 9 years ago

cnt.length is 4, because there are 4 groups when grouped by type. However, two of the groups have value 0, indicating that two of the types have zero counts for the current filters. Groups with zero counts are not excluded, which seems to be your expectation.

abuscom-leonhard commented 9 years ago

Yes thats my expectation. In my real world example I calculate the sum of sales by country and I want to include only a certain sales representative. I try to exclude all groupings that do not match the filter. Can I do this?

jasondavies commented 9 years ago

If you only want non-zero groups, you can simply filter the array of groups:

countGroup.top(Infinity).filter(function(d) { return d.value > 0; });