1
votes

I'm pretty new to Crossfilter, so my apologies.

I am working on a project where I am bringing data into the page and then permitting users to filter the data using select elements. Each select filter corresponds to a Crossfilter dimension. Based on the selected filter(s), the appropriate data is displayed in the page. I have that all working correctly, including the use of multiple filters at the same time (and each filter can contain multiple values).

What I would like to do is display the data organized by a selected group. For example, if my data has 3 columns: Office, Department and Gender, the user may filter on Office = [New York, Atlanta] and Gender = [Female]. The user can then group the results by Department (or another column). For ex, if there are 20 out of 100 matches based on the filters listed above, those results can be grouped by Department as follows: 8 Marketing, 7 Sales and 5 in IT.

(The order of the grouping is not relevant right now.)

There are other Departments in the data that are not included in the filtered results (like HR, Security, Operations, etc.)

At the time that the code has applied the filters and displayed the results, I don't seem to have a way to show the unique Department values that are associated with just those 20 items. When I do something like this:

deptDimension.group().all().forEach(function(d) {

It lists ALL of the Departments from all 100 records, even ones that are not in the current filtered results.

How do I get a list of unique values from a given dimension that will respect the existing filters?

Also, if anyone knows how to iterate over the results in the order of the groups/unique values for the Department dimension, that would be helpful too.

1
I will add that there is another use case for this, making the filters more dynamic and act like facets. For ex, if I apply the filter Office = New York, then that might exclude everyone in IT. In that case, I want the Department filter to be refreshed to show only the Departments for people in the New York office. It would also be useful if I could show how many matches there are associated with each filter value.Mark Salamon
Can you please post example code? What you describe regarding all records displaying shouldn't be happening, but please do note that groupings intersect current filters except for their own dimension, so the group you are generating won't respect the filter on the deptDimension dimension: github.com/square/crossfilter/wiki/…Ethan Jewett
Thanks. I'm aware of that limitation on groupings. In the example above, the grouping was on a field that did not have a filter applied, so that would not be an issue. Btw, it's not that all records are displaying, it's that all possible values for a given dimension are displaying even after filters have been applied. I will add some code later.Mark Salamon
That's to be expected, but the values on those groups should be 0, correct?Ethan Jewett
Correct. The value is zero. My preference would be not to show those groups at all instead of having to check for a zero value and ignoring those.Mark Salamon

1 Answers

2
votes

I recommend filtering out the zero-value groups if you don't want them:

deptDimension.group().all()
  .filter(function(d) { return d.value; })
  .forEach(function(d) { ...

Updated version of your example (fixing a few other things as well): https://jsfiddle.net/8g04rzuf/