selvagsz
selvagsz

Reputation: 3872

Working With Filters in Crossfilter

I have just started working with crossfilter and d3.js ... I'm trying some snippets given in the API reference... I Have the following data

var payments = crossfilter([
  {date: "2011-11-14T16:17:54Z", quantity: 2, total: 190, tip: 100, type: "tab"},
  {date: "2011-11-14T16:20:19Z", quantity: 2, total: 190, tip: 100, type: "tab"},
  {date: "2011-11-14T16:28:54Z", quantity: 1, total: 300, tip: 200, type: "visa"},
  {date: "2011-11-14T16:30:43Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-11-14T16:48:46Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-11-14T16:53:41Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-11-14T16:54:06Z", quantity: 1, total: 100, tip: 0, type: "cash"},
  {date: "2011-11-14T16:58:03Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-11-14T17:07:21Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-11-14T17:22:59Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-11-14T17:25:45Z", quantity: 2, total: 200, tip: 0, type: "cash"},
  {date: "2011-11-14T17:29:52Z", quantity: 1, total: 200, tip: 100, type: "visa"}
]);

I can create dimension via type as

var paymentsByTotal = payments.dimension(function(d) { return d.type; });

My question is that how can i filter an array of strings. I tried:

paymentsByTotal.filterRange(["cash","visa"]);

But I didn't get the expected result!

Any suggestions?

Upvotes: 6

Views: 2420

Answers (3)

ninjaPixel
ninjaPixel

Reputation: 6382

If you have an array of values, and you don't want to specify the explicit logic (i.e. d === "visa" ) for each item, then you can extend sai's filterFunction solution to check if the value is included in your array. Doing it this way just makes things easier if your array of items to filter is large or likely to change.

var items = ['Visa', 'Cash'];
paymentsByTotal.filterFunction(function(d) { return items.indexOf(d) > -1;});

Upvotes: 1

sai
sai

Reputation: 145

Appears that a filterFunction(function) has been added since the previous response, so that you can now do this with:

paymentsByTotal.filterFunction(function(d) { return d === "visa" || d === "cash" });

Upvotes: 2

Hyder
Hyder

Reputation: 1463

With the sourcecode in the master branch of Crossfilter.js, there is no provision for union of filters, you have to get the code from Jason Davies' union branch.

Then, you should be able to do paymentsByTotal.filter("cash","visa"); and get the desired output.

Upvotes: 3

Related Questions