dc.js:具有多个 CSV 的 ReduceSum

dc.js: ReduceSum with multiple CSVs

这是另一个 Whosebug problem 的后续行动,将包含多个 CSV 的图表创建到单个 dc.js 仪表板中。

我按照说明进行操作,我的图表正在运行。但是,不起作用的是 numberDisplay 元素。我怀疑的是,由于我将两个 CSV 的总数制成表格,我将不得不调整 groupAll.reduceSum() 函数,但我不确定如何调整。我的代码示例如下

//using queue.js to load data
var q = queue()
  .defer(d3.csv, "data1.csv")
  .defer(d3.csv, "data2.csv");

  q.await(function(error, data1, data2){

  //initiatizing crossfilter and ingesting data
  var ndx = crossfilter();
  ndx.add(data1.map(function(d){
    return { age: d.age,
             gender: d.gender,
             scores: +d.scores,
             total: +d.total,
             type: 'data1'};
    }));

  ndx.add(data2.map(function(d){
    return { age: d.age,
             gender: d.gender,
             scores: +d.scores,
             total: +d.total,
             type: 'data2'};
    }));

//initializing charts
totalDisplay = dc.numberDisplay("#total-display");
totalScores = dc.numberDisplay("#total-scores");

//groupAll function to sum up the values
var scoresGroup = ndx.groupAll().reduceSum(function(d) {
          d.scores;
        });
        var totalGroup = ndx.groupAll().reduceSum(function(d) { 
          d.total;
        });

//parameters for the number display. Currently it is returning NaN
totalDisplay
        .formatNumber(d3.format(","))
        .valueAccessor(function(d) {
            return d;
        })
        .group(totalGroup);

totalScores
        .formatNumber(d3.format(",f"))
        .valueAccessor(function(d) {
            return d;
        })
        .group(scoresGroup);

如有任何帮助,我们将不胜感激!

您需要使用 return 才能从函数中获得 return 值!

var scoresGroup = ndx.groupAll().reduceSum(function(d) {
      d.scores;
});
var totalGroup = ndx.groupAll().reduceSum(function(d) { 
      d.total;
});

应该是

var scoresGroup = ndx.groupAll().reduceSum(function(d) {
      return d.scores;
});
var totalGroup = ndx.groupAll().reduceSum(function(d) { 
      return d.total;
});

否则,您最终会得出 undefinedundefined 不是数字。 :-)