Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discrete filter for D3 Crossfilter Dimensions

Is there a way to create a dimension on a attribute that has one or more values? For example

{quantity: 2, total: 190, tip: 100, items: ["apple","sandwich"],
{quantity: 2, total: 190, tip: 100, items: ["ice-cream"]},
{quantity: 1, total: 300, tip: 200, items: ["apple", "coffee"]}

My goal is to create a cross filter that can filter out entries along a dimension that has ordinal values. Is there a way I write a filter/dimension that will allow me to say "I want all entries that have the item 'apple'"?

The only workaround i can think of is to create a dimension for each item. Like so:

var paymentsByApple = payments.dimension(function(d) { return $.inArray("apple", d.items); });
var paymentsByCoffee = payments.dimension(function(d) { return $.inArray("coffee", d.items); });
// and one for every possible item

The main problem is that I don't want to enumerate and hard code all the different objects. Moreover, I may end up having lots of possible different items. Is there a smarter way to do this?

Thanks in advance!

like image 610
kumikoda Avatar asked Jul 20 '12 02:07

kumikoda


2 Answers

Facing the same problem here and don't see an easy workaround with the current lib features, see this.

The problem with changing the datamodel to fit a single value dimension as proposed by Pablo Navaro is that you need to make sure that the stats calculated for other dimensions are not distorted (double counting, correcting means, ....)

Hope to see a filter working on multiple value dimension, or to have some more time to dig into the codebase to propose one ...

like image 142
krikrou Avatar answered Oct 20 '22 14:10

krikrou


What about changing your data model? I think that using:

[{id: 1, quantity: 1, total: 100, tip:  50, item: "apple"},
 {id: 1, quantity: 1, total:  90, tip:  50, item: "sandwich"},
 {id: 2, quantity: 1, total: 190, tip: 100, item: "ice-cream"},
 {id: 3, quantity: 1, total: 300, tip: 100, item: "apple"}, 
 {id: 3, quantity: 1, total: 300, tip: 100, item: "coffee"}]

Maybe you can compute the totals by id using reduceSum

like image 27
Pablo Navarro Avatar answered Oct 20 '22 13:10

Pablo Navarro