如果有人试图这样做,我创建了一个维度来保存数据中的所有记录:
allDim = ndx.dimension(function (d) {
return typeof Modality === 'string';
})
dayCountGroup = allDim.group().reduce(
function (p, v) { // add
var day = d3.time.day(v.EventDate).getTime();
p.map.set(day, p.map.has(day) ? p.map.get(day) + 1 : 1);
return p;
},
function (p, v) { // remove
var day = d3.time.day(v.EventDate).getTime();
p.map.set(day, p.map.has(day) ? p.map.get(day) - 1 : 0);
if(p.map.has(day) && p.map.get(day) == 0) p.map.remove(day);
return p;
},
function () { // init
return { map: d3.map() };
}
)
小时维度和组为:
hourDim = ndx.dimension(function (d) {
return d.EventHour;
})
hourAvgGroup = hourDim.group().reduce(
function (p, v) { // add
var day = d3.time.day(v.EventDate).getTime();
p.map.set(day, p.map.has(day) ? p.map.get(day) + 1 : 1);
return p;
},
function (p, v) { // remove
var day = d3.time.day(v.EventDate).getTime();
p.map.set(day, p.map.has(day) ? p.map.get(day) - 1 : 0);
if(p.map.has(day) && p.map.get(day) == 0) p.map.remove(day);
return p;
},
function () { // init
return { map: d3.map() };
}
)
.valueAccessor(function(d){ return sum_map(d.value.map)/size_array_of_maps(dayCountGroup.top(Infinity)) ? sum_map(d.value.map)/size_array_of_maps(dayCountGroup.top(Infinity)) : 0})
其中使用的两个功能是:
function sum_map(m) {
var sum = 0;
m.forEach(function(k, v) {
sum += v;
});
return m.size() ? sum : 0;
}
function size_array_of_maps(myObject) {
var count = 0;
myObject.forEach(function(key,value) {
count += key.value.map.size();
})
return count;
}
我肯定这里有很多冗余代码,但小提琴似乎在工作,我稍后会整理:)
https://jsfiddle.net/dolomite/6eeahs6z/126/