我正在寻找一个解决方案,当我使用减少添加/删除功能时,我可以删除空箱。删除空箱与减少添加删除功能不工作
我有一个的jsfiddle here
空箱被删除时,我想提供“点”的简单相加,而不是当我想用一个平均计算,并在图中使用的valueAccessor。
我的数据被设置如下:
{Season:"2016/17",
Manager:"Alan Curtis",
Points:1,
Formation:"4231",
date:"01 February 2017"},
{Season:"2016/17",
Manager:"Paul Clement",
Points:1,
Formation:"442",
date:"01 February 2018"},
{Season:"2015/16",
Manager:"Paul Clement",
Points:3,
Formation:"433",
date:"01 May 2017"},
而我的目标是提供一个“点每场比赛的平均,由“经理”,也可以通过“组”。
我使用了减少添加/删除功能:
function reduceAdd(p, v) {
p.total += v.Points;
++p.count;
p.ppg = d3.round((p.total/p.count), 2);
return p;
}
function reduceRemove(p, v) {
p.total -= v.Points;
--p.count;
p.ppg = d3.round((p.total/p.count), 2);
return p;
}
function reduceInitial() {
return {
total: 0,
count: 0,
ppg: 0,
};
}
和删除空箱码:
function remove_empty_bins(source_group) {
return {
all:function() {
return source_group.all().filter(function(d) {
return d.value !=0;
});
}
};
}
我的图表代码:
managerChart
.dimension(dimManager)
.group(ManagerPPGGroup)
.ordering(function(p) { return -p.value.ppg })
.renderLabel(false)
.othersGrouper(null)
.renderTitle(false)
.renderTitleLabel(true)
.margins({top: 10, left: 10, right: 20, bottom: 80})
.valueAccessor(function(p)
{ if (p.value.ppg >0) {
return p.value.ppg } else { return "n/a"}; });
formationChart
.dimension(dimFormation)
.group(filteredFormationPPGGroup)
.ordering(function(p) { return -p.value.ppg })
.renderLabel(false)
.cap(10)
.elasticX(true)
.renderTitle(false)
.renderTitleLabel(true)
.margins({top: 10, left: 10, right: 20, bottom: 80})
.valueAccessor(function(p) { return p.value.count > 0 ? p.value.ppg : "not used"; });
一切正常,除了应用过滤器时不会清空空箱。
我尝试了各种各样的事情来尝试解决问题,更改图表的valueAccessor和remove_empty_bins函数,但似乎没有任何工作。
我目前的解决方法是在图表上提供“未使用”文本,以便用户知道管理器未使用该阵列,但我更愿意按预期移除空箱。
在此先感谢您的帮助。
直接与问题无关,但您应该在dc中执行'd3.round((p.total/p.count),2)'计算。js'valueAccessor',而不是在Crossfilter组中。只需使用该组来跟踪“总计”和“计数”。为什么?假设您有1000条记录,并筛选出500条记录。这个计算在那个过程中会运行500次,当你只需要运行一次,当你显示平均值时。 –
感谢您的提示,很高兴知道。我会做出改变 – Kevin