2012-05-15 19 views
16

我覺得crossfilter庫API解釋的人我上面的技能編寫的,但我也知道,掌握它會解決我的問題動態返回結果。使用crossfilter在JavaScript

爲簡單起見,我將引用API Page's例如數據這個問題。

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"} 
]); 

我能夠返回匹配特定鍵(數量,總等)的記錄,但我不知道如何返回匹配鍵/值對的組合的結果。例如,如何返回匹配結果的結果集的數量大於1,總數等於90,小費是否等於0和選項卡類型?這是我完全失去的地方。

一如既往,任何幫助將不勝感激。

回答

26

您可以爲每個屬性維度,然後調用每個維度的過濾方法與你所指出的,像這樣相應的過濾標準。

var payments_by_quantity = payments.dimension(function(d){return d.quantity}), 
    payments_by_total = payments.dimension(function(d){return d.total}), 
    payments_by_tip = payments.dimension(function(d){return d.tip}), 
    payments_by_type = payments.dimension(function(d){return d.type}); 

payments_by_quantity.filter([1, Infinity]); 
payments_by_total.filter(90); 
payments_by_tip.filter(0); 
payments_by_type.filter("tab"); 

payments_by_type.top(Infinity) 

效果是累積性的,所以最後一行實際上是所有值都來自所有維度的所有過濾器的結果。

2

我發現上述答案准確但不準確,對我來說,作爲一個初學者,即我沒有或意外的結果(沒有不尊重的懶惰,但我從初學者的角度寫作,因爲我是一個crossfilter nube)。 陷阱是在調用某些過濾器之前清除過濾器的必要(您需要擴展數據集以及更多種類,例如不同的提示,總計等以瞭解我的意思)。輸出到控制檯可以幫助我。

這裏就是幫助我的理解:

var data = [ 
    {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: 222, total: 990, 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: 5, 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: 990, 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"} 
]; 



<script type="text/javascript"> 

// questions: For instance, how would I return the result set that matched results with a quantity more than 1, a total equal 90, a tip equal 0 and a type of tab? 
// create dimensions for each attribute 
var payments_by_quantity = payments.dimension(function(d){return d.quantity}); 
    payments_by_total = payments.dimension(function(d){return d.total}), 
    payments_by_tip = payments.dimension(function(d){return d.tip}), 
    payments_by_type = payments.dimension(function(d){return d.type}); 

//need top(Infinity) to print out contents of filtered items 
var morethan1 = payments_by_quantity.filter([1, Infinity]).top(Infinity); 
console.log("morethan1",morethan1); 

var tot_eq_90 = payments_by_total.filter(90).top(Infinity); 
console.log("tot_eq_90",tot_eq_90); 

// clear filters. If not, the result below will still be filtered by totals = 90 
payments_by_total.filterAll(); 

console.log("top1= biggest paymt qty:", payments_by_quantity.top(1)); 
payments_by_total.filterAll(); 
console.log("top2= biggest paymt qty:", payments_by_quantity.top(2)); 
payments_by_total.filterAll(); 

console.log("bottom paymt tip:", payments_by_tip.bottom(1)); 

var tip_eq_0 = payments_by_tip.filter(0).top(Infinity); 
console.log("tip_eq_0",tip_eq_0); 
payments_by_total.filterAll(); 

var typetab = payments_by_type.filter("tab").top(Infinity); 
console.log("typetab",typetab); 
payments_by_total.filterAll(); 

var typetab_i = payments_by_type.top(Infinity); 
console.log("typetab+i",typetab_i);