【问题标题】:Crossfilter average group交叉过滤平均组
【发布时间】:2013-03-11 03:51:56
【问题描述】:

我试图通过 sum(querytimes) 计算平均查询时间,然后将它们除以计数。我怎样才能得到计数?

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

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

【问题讨论】:

    标签: javascript d3.js crossfilter dc.js


    【解决方案1】:

    我认为更好的(也是预期的)方法是定义自己的 reduce 函数(add、remove、initial)。然后,您可以将运行总和、计数等存储在 reduce 函数中,并在过滤器从组中添加和删除数据时适当地调整它们。

    在这个类似的问题中给出了使用平均值和最小值和最大值进行此操作的示例:Using Crossfilter, is it possible to track max/min when grouping?

    【讨论】:

      【解决方案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);
      

      【讨论】:

      • 下面的评论是实现这一目标的预期方式。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-06
      • 2019-12-21
      • 1970-01-01
      • 1970-01-01
      • 2021-04-23
      相关资源
      最近更新 更多