简体   繁体   English

dc.js / Crossfilter-显示所选尺寸的分数?

[英]dc.js/Crossfilter - show fraction of dimension selected?

I have two dc.barChart s and two dc.NumberDisplay s. 我有两个dc.barChart和两个dc.NumberDisplay

JS Fiddle is here: https://jsfiddle.net/wgbruqk8/6/ . JS小提琴在这里: https : //jsfiddle.net/wgbruqk8/6/

One of the number displays shows the total of the overall population that is selected. 数字显示之一显示所选总人口的总数。 The other currently shows the fraction of all other dimensions that are selected -- I'd like for it to show the fraction of the dimension at hand that is selected. 另一个当前显示所选的所有其他尺寸的比例-我希望它显示所选的现有尺寸的比例。 For example, since its dimension is Achievement, if the brush is at [10,90], I'd like for it to show 100%. 例如,由于其尺寸为“成就”,因此如果笔刷为[10,90],我希望它显示100%。 However, if the brush is at [31, 90], I'd like for it to show 90% because there are 11 total cases and the brush is over 10 of them. 但是,如果笔刷位于[31,90],我希望它显示90%,因为总共有11个案例,并且笔刷超过10个。 How do I have it show the proportion of cases that the brush has selected? 如何显示画笔已选择的案例比例?

Also including the JS below: 还包括以下JS:

data = [{'achievement': 30,'balance': 35}, 
                {'achievement': 46, 'balance': 35},
        {'achievement': 72, 'balance': 33},
        {'achievement': 50, 'balance': 29},
        {'achievement': 55, 'balance': 38},
        {'achievement': 70, 'balance': 40},
        {'achievement': 85, 'balance': 42},
        {'achievement': 80, 'balance': 41},
        {'achievement': 46, 'balance': 35},
        {'achievement': 46, 'balance': 35},
        {'achievement': 46, 'balance': 35},];

console.log(data);

var chartMargins = {top: 10, right: 30, bottom: 20, left: 40};

var ndx = crossfilter(data),
    all = ndx.groupAll(),
    countAll = all.reduceCount(),

    achievement = ndx.dimension(function(d) {
          return d.achievement;
        }),
    achievementGroup = achievement.group(Math.floor),
    balance = ndx.dimension(function(d) {
          return d.balance;
        }),
    balanceGroup = balance.group(Math.floor);


var achievementChart = dc.barChart('#dc-achievement-chart')
            .width(600)
            .height(80)
            .margins(chartMargins)
            .dimension(achievement)
            .group(achievementGroup)
          .x(d3.scale.linear().domain([0,100]));

var balanceChart = dc.barChart('#dc-balance-chart')
            .width(700)
            .height(80)
            .margins(chartMargins)
            .dimension(balance)
            .group(balanceGroup)
            .centerBar(true)
          .x(d3.scale.linear().domain([0,100]).rangeRound([0, 10*100]))
            .filter([40,100]);

dc.numberDisplay('#dc-overall-total')
      .width(100)
      .valueAccessor(function(d) { return d / 11})
      .formatNumber(d3.format('%'))
      .group(ndx.groupAll());

dc.numberDisplay('#dc-achievement-selection')
     .valueAccessor(function(d) { return d / 11})
     .formatNumber(d3.format('%'))
     .group(achievement.groupAll());

dc.renderAll();

As mentioned above, you currently probably have to do this calculation against the raw data, based on filters pulled from your dc.js chart. 如上所述,您当前可能必须根据从dc.js图表​​中提取的过滤器对原始数据进行此计算。 Here is an example of how to do it: https://jsfiddle.net/jy8zh18a/1/ 这是如何执行此操作的示例: https : //jsfiddle.net/jy8zh18a/1/

The key is basically to create a fake group and calculate the number of data elements that match the current filter on whatever chart you are interested in: 关键基本上是创建一个假组并计算与您感兴趣的任何图表上的当前过滤器匹配的数据元素的数量:

var acheivementSelectGroup = {
  value: function() {
    var acheivementFilter = achievementChart.filter();
    var selectedValues = data.filter(function(d) {
        if(acheivementFilter) {
        return acheivementFilter[0] <= d['achievement'] &&
            d['achievement'] <= acheivementFilter[1];
      } else {
        return true;
      }
    }).length;
    return selectedValues;
  }
}

Then use that fake group in your number display: 然后在您的号码显示中使用该假组:

dc.numberDisplay('#dc-achievement-selection')
     .valueAccessor(function(d) { return d / 11})
     .formatNumber(d3.format('%'))
     .group(acheivementselectyroup);

Note in the code above, 'achievement' is spelled in 2 different ways based on the different spellings in the chart variable name and the data attribute. 注意,在上面的代码中,“成就”是根据图表变量名称和数据属性中的不同拼写以两种不同的方式拼写的。 Not critiquing spelling here, but just pointing it out as it might be confusing to others coming at this problem :-) 这里不是在批评拼写,而只是指出它是因为它可能会使其他人混淆这个问题:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM