Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 610
selvagsz Avatar asked Oct 29 '12 08:10

selvagsz


People also ask

How does cross-filtering work?

Cross-filtering lets you interact with one chart and apply that interaction as a filter to other charts in the report. When cross-filtering is turned on for a chart, you can filter the report by interacting with that chart in two ways: Clicking one or more dimension values in the chart.

What is the function of filters?

A filter is a circuit whose transfer function, that is the ratio of its output to its input, depends upon frequency. There are three broad categories of filter which are widely used: Low-pass filters allow any input at a frequency below a characteristic frequency to pass to its output unattenuated or even amplified.

How do I use the cross filter in DAX?

CROSSFILTER can only be used in functions that take a filter as an argument, for example: CALCULATE, CALCULATETABLE, CLOSINGBALANCEMONTH, CLOSINGBALANCEQUARTER, CLOSINGBALANCEYEAR, OPENINGBALANCEMONTH, OPENINGBALANCEQUARTER, OPENINGBALANCEYEAR, TOTALMTD, TOTALQTD and TOTALYTD functions.


3 Answers

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.

like image 58
Hyder Avatar answered Sep 22 '22 16:09

Hyder


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" });
like image 31
sai Avatar answered Sep 23 '22 16:09

sai


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;});
like image 45
ninjaPixel Avatar answered Sep 23 '22 16:09

ninjaPixel