简体   繁体   English

dc.js:带有多个CSV的ReduceSum

[英]dc.js: ReduceSum with multiple CSVs

This is a follow-up to another StackOverflow problem with creating charts with multiple CSVs into a single dc.js dashboard. 这是另一个StackOverflow 问题的后续工作,该问题是在单个dc.js仪表板中创建具有多个CSV的图表。

I followed the instructions and my charts are working. 我按照说明进行操作,我的图表正在运行。 However, what is not working is the numberDisplay elements. 但是, numberDisplay元素不起作用。 What I'm suspecting is that since I'm tabulating the totals of two CSVs, I would have to adjust the groupAll.reduceSum() function, but I'm unsure how. 我怀疑的是,由于我要对两个CSV的总数进行制表,因此我必须调整groupAll.reduceSum()函数,但是我不确定如何调整。 An example of my code is below 我的代码示例如下

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

Any help would be greatly appreciated! 任何帮助将不胜感激!

You need to use return in order to return values from functions! 您需要使用return才能从函数中返回值!

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

should be 应该

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

Otherwise, you will end up summing undefined and undefined is Not a Number. 否则,您最终将求和undefined undefined不是数字。 :-) :-)

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

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