2012-07-20 29 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的建議,將數據模型更改爲適合單個值維度的問題是您需要確保爲其他維度計算的統計信息不會失真(重複計算,糾正方法......)

希望看到一個過濾器工作在多個值的維度,或有更多的時間挖掘到代碼庫提出一個...