2012-07-20 28 views
5

有没有办法在具有一个或多个值的属性上创建维度?例如用于D3 Crossfilter的离散过滤器尺寸

{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"]} 

我的目标是创建一个交叉过滤器,可以过滤具有序数值的维的条目。有没有一种方法可以让我写出一个过滤器/维度,让我说“我想要所有具有该项目”苹果“的条目?

我能想到的唯一解决方法是为每个项目创建一个维度。像这样:

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 

主要问题是我不想枚举和硬编码所有不同的对象。此外,我可能会有很多可能的不同项目。有没有更聪明的方法来做到这一点?

在此先感谢!

回答

2

如何更改您的数据模型?我觉得用:

[{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"}] 

也许你可以使用reduceSum

3

这里面临着同样的问题,通过计算ID的总数并没有看到一个简单的解决方案与目前的lib功能,请参阅this

根据Pablo Navaro的建议,将数据模型更改为适合单个值维度的问题是您需要确保为其他维度计算的统计信息不会失真(重复计算,纠正方法......)

希望看到一个过滤器工作在多个值的维度,或有更多的时间挖掘到代码库提出一个...