2012-05-15 3 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이고, 팁이 같고 탭 유형 인 결과와 일치하는 결과 세트를 어떻게 반환하겠습니까? 이것은 내가 완전히 잃어버린 곳입니다.

언제나처럼 도움을 주시면 감사하겠습니다.

답변

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

위의 답변은 초보자로서 정확하게 정확하지만 부정확 한 것으로 나타났습니다. 즉, 예기치 못한 결과가 나타납니다. (슬픈 조롱을 무시할 수는 없지만 초보자 관점에서 필자는 크로스 필터 터브로서 글을 쓰고 있습니다.) 일부 필터를 호출하기 전에 필터를 지울 필요가 있습니다 (데이터 세트를 확장하여 예를 들어 팁, 총계 등을 확장해야 의미를 알 수 있음). 콘솔로 출력하면 도움이됩니다.

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

여기 내 이해하는 데 도움이 무엇