给出此数据集here
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来做这个但不确定是否有一种简单的方法来做这个只是编写一个javascript函数。 例如,我可以获得低于2692的总记录/行
cf.size()
//2692
注意
打开控制台模式(Ctrl-shift-I)并键入cf.size()
交叉过滤器已在源代码(Ctrl-U)var cf = crossfilter(data);
使用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
使用crossfilter,但这只是在交叉滤波器延迟维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
适应这个: Custom calculated aggregated field in 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}
另一个好人在这里:
What are the reduceAdd, reduceSum , reduceRemove functions in crossfilter? How should they be used?但仍然不是我想要的,这是每个类别的总和,我想要总和,例如期望所有延迟值的答案都为30632
。
从控制台下面的docs打印输出中获取:
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
答案 0 :(得分:1)
您可以使用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
})