dimension filter is not working with multiple values

700 views
Skip to first unread message

Niteen Munjani

unread,
Jul 28, 2016, 1:47:13 AM7/28/16
to dc-js user group
Hello,

I have textbox on page and user can enter multiple values in comma separated to filter the chart. As per the API documentations we have to pass array of values to multiple value filter but its not working for me. Here is my code snippet

var selectedActivityId = "A12345,B12454" // example values from textbox

var activities = selectedActivityId.split(',');  //getting array element=  ["A12345" , "B12454"]

            if(activities.length > 1){              
                console.log('inside multiple filter');
                activityIdsFilter.filter(activities); // activityIdsFilter is dimension
                
            }else{
                console.log('inside single filter');
                activityIdsFilter.filter(selectedActivityId);
            }         

I have tried by creating filterfunction as well but nothing works for me. Can anyone please suggest how to resolve this issue?

Regards
Nitin Munjani

Gordon Woodhull

unread,
Jul 28, 2016, 10:48:03 AM7/28/16
to dc.js user group
Hi Nitin,

You're on the right track, but the behavior of .filter() is a little bit strange.

I've recently updated the documentation for this function, because it confused me too. Please  give it a try:


In particular, it takes either a single value or an array containing one array of values. And it toggles each one, so you may want .replaceFilter() instead.


Cheers,
Gordon

--
You received this message because you are subscribed to the Google Groups "dc-js user group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dc-js-user-gro...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dc-js-user-group/d3ea1bb1-9f9a-44a5-98a4-645d9f795126%40googlegroups.com.

Niteen Munjani

unread,
Jul 29, 2016, 6:33:37 AM7/29/16
to dc-js user group
Hi Gordon,

Thanks for your response. 

The solution you suggested I tried it but its not working for me. Could you please provide some example?

Regards
Nitin Munjani

Gordon Woodhull

unread,
Jul 29, 2016, 7:02:38 AM7/29/16
to dc-js-us...@googlegroups.com
From the documentation I linked earlier:
// filter by a set of states
chart.filter([['MA', 'TX', 'ND', 'WA']]);
Note the extra brackets: it's an array containing one array of values.
Message has been deleted

Niteen Munjani

unread,
Jul 29, 2016, 8:15:37 AM7/29/16
to dc-js user group
Hi Gordon

I have tried the way you mentioned as well, but its not working.

I have tried below

mydimension.filter([['MA''TX''ND''WA']]);
and
mydimension.filter(['MA''TX''ND''WA']);

Regards
Nitin Munjani

Gordon Woodhull

unread,
Jul 29, 2016, 8:22:38 AM7/29/16
to dc.js user group
Filter the chart, not the dimension. In general, crossfilter doesn't support any getters, so there is no way for dc.js to know what the current filters are except by setting them through the chart.

And yes, the interface is inconsistent between crossfilter and dc.js. I don't know where dc.js picked up that extra pair of brackets.


Niteen Munjani

unread,
Jul 29, 2016, 8:34:19 AM7/29/16
to dc-js user group
Hi Gordon

My other filter is working fine with dimension. 

example below

createdDateFilter.filter(null);
createdDateFilter.filterRange([value1, value2]);
dc.redrawAll();

Also when I am passing single value as below its working fine for me with dimension.
mydimension.filter('MA'); or mydimension.filter("MA");

Regards
Nitin Munjani

Gordon Woodhull

unread,
Jul 29, 2016, 8:44:12 AM7/29/16
to dc.js user group
Hi Nitin,

There's also a difference between categorical dimensions, where you're filtering a bunch of single values, and quantitative dimensions, where you're filtering a continuous interval of values. 

Crossfilter will not filter more than one categorical value. If you want to do this through crossfilter, please read the crossfilter documentation:

Note that each of the different forms is a shortcut to one of three filter functions available: filterExact (one value), filterRange (takes an interval as an array of two values), filterFunction (takes a boolean function). It is more precise and probably less confusing to use those functions instead. filterExact and filterRange are more efficient than filterFunction, but this won't matter until your data gets huge.

When you specify multiple categorical values to dc.js, using the syntax I described, it produces a filterFunction for crossfilter.

Cheers,
Gordon


Reply all
Reply to author
Forward
0 new messages