2013-03-21 4 views
9

평균 sum (querytimes)으로 평균 쿼리 시간을 계산 한 다음 count로 나누는 중입니다. 어떻게 카운트를 얻을 수 있습니까?크로스 필터 평균 그룹

var querytimeByMonthGroup = moveMonths.group().reduceSum(function (d) { 
    return d.querytime; 
}); 

var querytimeByMonthGroup = moveMonths.group().reduceSum(function (d) { 
    return d.querytime/d.count; ??? 
}); 

답변

2

크로스 필터에 익숙하지 않아 막 재생하기 시작했습니다. 더 좋은 방법이있을 수 있지만 그룹화에 사용 된 차원의 수를 계산할 수있는 방법을 제공합니다 (나는 d.count가 그룹화에 사용 된 차원의 수를 참조한다는 것을 100 % 명확히 밝히지 않았습니다. 필요한 경우 다른 그룹화). 에서 사용할 수있는 코드에서 파생 된

예 : https://github.com/square/crossfilter/wiki/API-Reference

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

var paymentsByType = payments.dimension(function(d) { return d.type; }), 
     paymentVolumeByType = paymentsByType.group(), 
     counts = paymentVolumeByType.reduceCount().all(), 
     countByType = {}; 

// what is returned by all is a pseudo-array. An object that behaves like an array. 
// Trick to make it a proper array 
Array.prototype.slice.call(counts).forEach(function(d) { countByType[d.key] = d.value; }) 
var paymentVolumeByType = paymentVolumeByType.reduceSum(function(d, i) { 
    console.log(d.total, d.type, countByType[d.type]) 
    return d.total/countByType[d.type]; 
}); 
// accessing parentVolumeByType to cause the reduceSum function to be called 
var topTypes = paymentVolumeByType.top(1); 
+0

코멘트 아래이를 달성하기 위해 의도 된 방법입니다. –

11

난이 할 수있는 더 좋은 (그리고 의도) 방법은 자신의 감소 기능 (제거, 초기 추가) 정의하여 생각합니다. 그런 다음 합계, 개수 등을 감소 함수에 저장하고 필터가 &을 그룹에 추가 할 때 적절하게 조정할 수 있습니다.

평균과 및 분 & 최대로이 일의 예

이 비슷한 질문에 제시되어있다 : Using Crossfilter, is it possible to track max/min when grouping?