Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

complement sets in crossfilter.js?

crossfilter.js is great for identifying data points that match particular contiguous segments of data dimensions. This is usually the exact behavior that you want, but I have a peculiar use case where it would be convenient to know the set of data points that do not match any of the selected filters. Is there a way to obtain the complement of the selected dimensions in crossfilter.js?

As a concrete example, when you load the crossfilter.js page, I'd like to know the list of all flights that are not in February (the pre-specified date range).

like image 937
dino Avatar asked Aug 13 '26 19:08

dino


1 Answers

At the time of writing, there's no straightforward way to complement the current set of filters for a given Crossfilter instance. Such an operation would recompute all groups so that they match the complemented filters.

If you simply want a list of records that do not match the current set of filters, it's not exactly straightforward but you might consider iterating over an array of all records (in a particular order), and comparing with an array of matching records (in the same order).

For example:

var db = crossfilter(data),
    date = db.dimension(function(d) { return d.date; }),
    recordsByDate = date.top(Infinity),
    n = db.size();

// Add some filters here on various dimensions…
date.filterRange([0, 31536e6]);

// Retrieve matching records in date order.
var matchesByDate = date.top(Infinity),
    m = matchesByDate.length;

// Iterate over all records in date order, and compare with matching records.
for (var i = 0, j = 0; i < n && j < m; ++i) {
  if (recordsByDate[i] === matchesByDate[j]) {
    // Ignore matches.
    ++j;
    continue;
  }
  // Otherwise, non-matching record: process immediately or add to an array.
}
like image 114
Jason Davies Avatar answered Aug 16 '26 10:08

Jason Davies