2

I've been trying to understand crossfilter filtering and I was finally able to get it right in my code but I can't seem to understand why CrossFilter does what it does.

So here's what I get from here Crossfilter filters not filtering (dc.js):

As Jeffrey Biles says in it "So it turns out crossfilter doesn't apply the filter to the dimension you put the filter on. It applies it to everything else."

Okay, I don't understand why it's done like that but okay, filter applies to everything else, got it.

But then I found this Custom Text filter for DC.js dataTable:

And in here, rbristow is using a text filter box to filter his code. BUT filters on his companyDimension dimension. And that seems to be the dimension that is filtered as well.

So... I have two questions:

  1. Why is it that CrossFilter made it so that a filter will be applied to all dimensions except the one you call it on?
  2. Why does rbristow's code work if that's the case?

Thanks a ton in advance!

Community
  • 1
  • 1
archeezee
  • 411
  • 1
  • 4
  • 17

1 Answers1

4

In Crossfilter you can query dimensions (e.g. dimension.top(10)) or groups (e.g. group.all() or group.top(10)). Queries on a dimension return your original data records in the ordering of the dimension and they respect all filters in place on the Crossfilter including the filter on that dimension, if any. Queries on groups return your group aggregations and respect filters in place on the Crossfilter except the filter on the dimension the group is defined on.

The reason for this design is that you will use the result of group queries to build interactive types of charts (that is, charts the user will filter on) because we want to chart aggregated values. When the user is filtering on a chart, you really don't want that chart to change. The data should hold still for the user. All the other charts (based on groups defined on other dimensions) should change. So that's the reason that groups ignore filters on their own dimension.

The dataTable example you reference works for the following reason: dc.js generally uses the group it is passed to build its chart using group.top or group.all methods to get data, which ignores the filter on the dimension of that group. The dimension passed to a chart is usually only used for applying filters from that chart. It is usually (though doesn't have to be) the dimension the group in the same chart was defined on. The dc.js dataTable is different. It uses the dimension to get the data it requires. Because of this, as noted above, the dataTable will show you data that respects all filters on the Crossfilter, including any filters in place on the dimension used in the dataTable.

Ethan Jewett
  • 6,002
  • 16
  • 25