2017-06-04 110 views
1

我正在寻找一个解决方案,当我使用减少添加/删除功能时,我可以删除空箱。删除空箱与减少添加删除功能不工作

我有一个的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函数,但似乎没有任何工作。

我目前的解决方法是在图表上提供“未使用”文本,以便用户知道管理器未使用该阵列,但我更愿意按预期移除空箱。

在此先感谢您的帮助。

+1

直接与问题无关,但您应该在dc中执行'd3.round((p.total/p.count),2)'计算。js'valueAccessor',而不是在Crossfilter组中。只需使用该组来跟踪“总计”和“计数”。为什么?假设您有1000条记录,并筛选出500条记录。这个计算在那个过程中会运行500次,当你只需要运行一次,当你显示平均值时。 –

+0

感谢您的提示,很高兴知道。我会做出改变 – Kevin

回答

2

是的,remove_empty_bins需要调整,如果减少产生一个对象,而不是一个数字。

我想不出任何一般的方式来做到这一点,不会使效率低下,*让我们调节功能,这种用例:

function remove_empty_bins(source_group) { 
    return { 
     all:function() { 
      return source_group.all().filter(function(d) { 
       return d.value.total != 0; 
      }); 
     } 
    }; 
} 

我们只需要拉.total出于对象,因为an object (almost) never equals zero

作为奖励,我还设置了酒吧一个固定的高度在你的提琴:

formationChart 
    .fixedBarHeight(30) 

否则,当有一个酒吧,它会成长,以适应整个区域,许多人考虑丑陋。

我也应用过滤到经理rowChart。你的小提琴叉:https://jsfiddle.net/gordonwoodhull/qw0oe8ea/6/

*也许是时候了谓词来重构为remove_bins()呢?但是,在没有箭头功能的浏览器消失之前,这并不简单。

+0

再次感谢戈登,非常感激。我尝试了各种不同的东西,删除空箱功能,但没有任何我试过! – Kevin