2012-05-15 12 views
16

Tôi cảm thấy rằng giải thích API thư viện crossfilter được viết cho ai đó ở trên các kỹ năng của tôi, nhưng tôi cũng biết rằng làm chủ nó sẽ giải quyết được vấn đề của tôi.Sử dụng crossfilter để tự động trả lại kết quả trong JavaScript

Để làm cho nó đơn giản, tôi sẽ tham khảo các dữ liệu ví dụ API Page's cho câu hỏi này.

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

Tôi có thể trả về các bản ghi khớp với khóa cụ thể (số lượng, tổng số, v.v.), nhưng tôi không hiểu cách trả lại kết quả khớp với cặp khóa/giá trị. Ví dụ: làm cách nào để tôi trả lại tập kết quả khớp với kết quả với số lượng lớn hơn 1, tổng bằng 90, một đầu bằng 0 và một loại tab? Đây là nơi tôi hoàn toàn bị mất.

Như mọi khi, mọi trợ giúp sẽ được đánh giá cao.

Trả lời

26

Bạn có thể tạo thứ nguyên cho từng thuộc tính và sau đó gọi phương thức lọc của từng thứ nguyên với tiêu chí lọc tương ứng mà bạn đã chỉ định, như vậy.

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) 

Hiệu ứng được tích lũy, vì vậy dòng cuối cùng thực sự là kết quả của tất cả các giá trị tôn trọng tất cả bộ lọc từ mọi thứ nguyên.

2

Tôi thấy câu trả lời ở trên chính xác nhưng không chính xác đối với tôi là người mới bắt đầu tức là tôi không nhận được kết quả không mong muốn (không có sự thiếu tôn trọng slo-jo, nhưng tôi viết từ góc độ người mới bắt đầu như tôi là một nấc chéo). Cần phải xóa bộ lọc trước khi gọi một số bộ lọc (bạn cần phải mở rộng tập dữ liệu với nhiều loại khác nhau, ví dụ: các mẹo khác nhau, tổng số, v.v. để xem ý tôi là gì). Xuất ra bàn điều khiển giúp tôi.

Dưới đây là những gì giúp tôi hiểu:

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);