用交叉过滤器取消场地的所有值?
在这里给出这个数据集
Date Origin Destination Delay (mins)
03/01/2001 13:05:00 MCI MDW 8
03/01/2001 13:45:00 LAS PHX 95
03/01/2001 18:30:00 LAX PHX 10
03/01/2001 18:30:00 ONT PHX 0
03/01/2001 18:30:00 LAS LAX -9
03/01/2001 18:30:00 LAX OAK 12
03/01/2001 18:40:00 ONT SMF -8
03/01/2001 19:00:00 MDW BNA -1
03/01/2001 19:00:00 OAK LAX -10
03/01/2001 19:00:00 LAS LAX -11
...
我如何总结Delay
字段中的所有值? 我知道有负值,我只是以此为例。
我正在考虑使用crossfilter来做到这一点,但不知道是否有一个simplar的方式来做这个只是写一个JavaScript函数。 例如我可以得到下面是2692的总记录/行
cf.size()
//2692
注意
调出控制台模式(Ctrl-shift-I)并键入cf.size()
已经在源代码中定义了交叉过滤器(Ctrl-U) var cf = crossfilter(data);
EDIT1
使用d3.sum
d3.sum(data,function(d){return d.delay;})
30632
使用array.reduce - 请参阅JavaScript Array.prototype.reduce - 什么是0?
data.reduce(function(total,d){ return total+ d.delay }, 0)
30632
使用var delayDimension = cf.dimension(function(d) {return d.delay;});
,但这只是在var delayDimension = cf.dimension(function(d) {return d.delay;});
延迟维上使用上面的减少量var delayDimension = cf.dimension(function(d) {return d.delay;});
delayDimension.top(Infinity).reduce(function(total,d){ return total+ d.delay }, 0)
30632
注意:
这会在最后一个元素中返回延迟值:
data.reduce(function(total,num){ return num.delay })
165
EDIT2
适应此:在Crossfilter中自定义计算的聚合字段
创建一个维度:
origin=cf.dimension(function(d) { return d.origin; })
创建我的reduceAdd,reduceMove和reduceInitial函数:
var reduceAdd = function(p, v) {
p.delay += v.delay;
return p;
}
var reduceRemove = function(p, v) {
p.delay -= v.delay;
return p;
}
var reduceInitial = function() {
return {
delay : 0
}
}
控制台输出示例:
var json = origin.group().reduce(reduceAdd,reduceRemove,reduceInitial).orderNatural().top(Infinity);
undefined
json
[Objectkey: "ALB"value: Objectdelay: -1__proto__: Object__proto__: Object, Objectkey: "AMA"value: Objectdelay: 171__proto__: Object__proto__: Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
json[0]
Object {key: "ALB", value: Object}
json[0].value
Object {delay: -1}
另一个很好的例子:crossfilter中的reduceAdd,reduceSum,reduceRemove函数是什么? 他们应该如何使用? 但仍然不是我想要的,这是每个类别的总和,我想要一个总和,例如,期望所有延迟值加在一起的答案为30632
。
EDIT3
从控制台的打印输出文档中获得它:
cf.size()
//2692
var reduceAdd = function(p, v) {
p.delay += v.delay;
return p;
}
var reduceRemove = function(p, v) {
p.delay -= v.delay;
return p;
}
var reduceInitial = function() {
return {
delay : 0
}
}
cf_groupAll=cf.groupAll().reduce(reduceAdd,reduceRemove,reduceInitial)
//Object {}
cf_groupAll.value()
//Object {delay: 30632}
cf_groupAll.value().delay
//30632
您可以使用jQuery Selector来获取最后一列的值。 然后你可以使用parseInt(STRING,10)来解析Integer值。 那么你可以很容易地将它们统计在一
var $elements = $('#data tr.item td:last-child')
var sum = 0
$elements.each(function() {
var currentValue = parseInt($(this).html(), 10)
sum += currentValue
})
链接地址: http://www.djcxy.com/p/32693.html
上一篇: summ all the values of a field using crossfilter?
下一篇: Crossfilter divide one dimension by the range selected in another