numberDisplay - 按最大键过滤

时间:2017-07-07 13:40:41

标签: dc.js crossfilter

我有一个dc.js仪表板,其中包含按月/期间分组的历史人数,疾病等信息。仪表板的用户希望在顶部的数字显示中看到最近月份的数据(例如,人数,疾病率等)。

我的数据:

var data= [
{ "Month": "01/02/2014","Period": 201402,"Heads": 15123,"SchedHours":146000,"Sickness": 0,},
{ "Month": "01/03/2014","Period": 201403,"Heads": 15100,"SchedHours": 146000,"Sickness": 0,},
{ "Month": "01/04/2014","Period": 201404,"Heads": 14900,"SchedHours": 146000,"Sickness": 0,},
{ "Month": "01/05/2014","Period": 201405,"Heads": 14800,"SchedHours": 146000,"Sickness": 0,},
{ "Month": "01/06/2014","Period": 201406,"Heads": 14750,"SchedHours": 146000,"Sickness": 0,},
{ "Month": "01/07/2014","Period": 201407,"Heads": 14720,"SchedHours": 146000,"Sickness": 0,},
{ "Month": "01/08/2014","Period": 201408,"Heads": 14000,"SchedHours": 146000,"Sickness": 0,},
{ "Month": "01/09/2014","Period": 201409,"Heads": 13750,"SchedHours": 146000,"Sickness": 0,},
{ "Month": "01/10/2014","Period": 201410,"Heads": 13500,"SchedHours": 146000,"Sickness": 0,},
{ "Month": "01/11/2014","Period": 201411,"Heads": 13200,"SchedHours": 146000,"Sickness": 0,},
....]

我可以在仪表板首次使用变量加载时默认情况下如何执行此操作:

maxPeriod = dimPeriod.top(1)[0].Period;

但我正在努力研究如何更新上面的变量以更新numberDisplay以始终过滤它以按最大时段或月显示头部。

我目前正在展示最新数量的" Heads"使用戈登在另一个问题中提供的函数:

function choose_bin(group, key) {
return {
value: function() {
  return group.all().filter(kv => kv.key === key)[0].value;
    }
  }
}

我的小组是:

var latestMonth = choose_bin(groupHeads3, maxPeriod);

和numberDisplay:

numberChart
.group(latestMonth)
.valueAccessor(x => (x))
.formatNumber(d3.format(".0f"));

我有一个基本的jsfiddle来显示问题(一旦应用过滤器,值为0):https://jsfiddle.net/kevinelphick/mzpoe555/2/

我想要做的是,Heads值根据折线图中过滤的最高周期进行更新。我还希望将最高期间值之前的月份包括在内,并提供两者的比较以显示趋势。

提前谢谢。

1 个答案:

答案 0 :(得分:2)

你只是在运行时计算不够的东西。每次查询组时,您都需要计算maxPeriod以及之后的所有内容。你可能想要做这样的事情(如果你把它放到JSFiddle中就可以了:

var latestMonth = {
  all: function() { 
    maxPeriod = dimPeriod.top(1)[0].Period;
    return [choose_bin(groupHeads3, maxPeriod).value()]
  }
}