Edward J. Stembler
Edward J. Stembler

Reputation: 2042

Applying a date range filter to a crossfilter dimension

I'm attempting to use the crossfilter javascript library (in conjunction with D3.js) to group and filter json data.

My json data has the following fields: week_date, category, team_name, title, subtitle

I've been able to successfully group on all records to produce YTD totals, using something like this:

var dimension = data.dimension(function(d) { return d[target]; });
var dimensionGrouped = dimension.group().all();

Where target is either category or team_name.

In addition to keeping the YTD totals, I also want to display totals for a given range. For example, a user-selected week (i.e. Oct 1st - 5th).

How do I create a filtered group which returns the totals for a given date range? Using my week_date field.

Upvotes: 4

Views: 4077

Answers (1)

Edward J. Stembler
Edward J. Stembler

Reputation: 2042

Well, after some superficial research, including skimming over crossfilter's issues list, I've concluded that crossfilter does not currently support grouping on multiple fields.

I was able to work around this by using a filtered copy of the data instead, as such:

// YTD rows
var crossfilterData = crossfilter(data);
var ytdDimension = crossfilterData.dimension(function(d) { return d[target]; });
var ytdDimensionGrouped = ytdDimension.group().all();
ytdDimensionGrouped.forEach(function (item) {
    // ...
});

// Ranged rows
var filteredData = data.filter(function (d) {
    return d.week_date >= filter[0] && d.week_date <= filter[1];
});
crossfilterData = crossfilter(filteredData);
var rangeDimension = crossfilterData.dimension(function(d) { return d[target]; });
var rangeDimensionGrouped = rangeDimension.group().all();
rangeDimensionGrouped.forEach(function (item) {
    // ...
});

Upvotes: 4

Related Questions