这是另一个StackOverflow problem的后续版本,其中在单个dc.js仪表板中创建了具有多个CSV的图表。

我按照说明进行操作,我的图表正在运行。但是,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才能从函数中返回值!

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;
});


否则,您最终将求和undefined并且undefined不是数字。 :-)

关于javascript - dc.js:带有多个CSV的ReduceSum,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30948009/

10-09 21:08