Felix
Felix

Reputation: 201

dc.js: dynamically multiple dimensions

I want to create as many dimensions as I got datasets in the array. The dataset amount is variable. In my example i have 3 but it can be more or less.

My data:

signalData: {
signal1: {
  name: "",
  data: {2,3,1,4,5,1,3},
},
signal2: {
  name: "",
  data: {2,3,1,4,5,1,3},
},
signal3: {
  name: "",
  data: {2,3,1,4,5,1,3},
},

I tried this:

var cf = crossfilter(flatData);

  var dimension = cf.dimension(dc.pluck("key"));
  var group = [];

  for (var signal in this.signalData) {
    dim = null;
    var dim = dimension.group().reduceSum(function (d) {
      return d[signal];
    });

    group.push(dim);
  }

But all 3 rendered charts only display the data of the signal3

Upvotes: 1

Views: 115

Answers (1)

Gordon
Gordon

Reputation: 20140

I added an easier solution to the entry in the FAQ.

Array.forEach is often a good solution, because the value for the current iteration of the loop will be captured in a local variable which won't change:

  Object.keys(signalData).forEach(function(signal) {
    dim = null;
    var dim = dimension.group().reduceSum(function (d) {
      return d[signal];
    });

    group.push(dim);
  });

Here we use Object.keys() to get an array of the field names, and then loop over that using forEach.

(I don't use for loops all that much, for this reason.)

Upvotes: 1

Related Questions