2013-03-21 130 views
9

我試圖通過求和(querytimes)來計算平均查詢時間,然後將它們除以一個計數。我如何得到計數?Crossfilter平均組

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

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

回答

2

我不熟悉crossfilter,只是剛開始玩它。可能有更好的方法,但是這提供了一種方法來計算用於分組的維數(我不是100%清楚地知道d.count指的是用於分組的維數,使用如果需要另一個分組)。

來自實例提供的代碼在衍生: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

註釋下面是實現這一目標的預期方式。 – 2015-01-12 00:35:40

11

我想一個更好的(和預期)的方式來做到這一點是通過定義自己減少功能(添加,刪除,初始)。然後,您可以將運行總和,計數等存儲在減少功能中,並在篩選器添加&刪除組中的數據時適當調整它們。

與平均值,並與分&最大這樣的例子中給出了這樣類似的問題:Using Crossfilter, is it possible to track max/min when grouping?