dc.js: ReduceSum with multiple CSVs

444 views Asked by At

This is a follow-up to another StackOverflow problem with creating charts with multiple CSVs into a single dc.js dashboard.

I followed the instructions and my charts are working. However, what is not working is the numberDisplay elements. 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. 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!

1

There are 1 answers

1
Gordon On BEST ANSWER

You need to use return in order to return values from functions!

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. :-)