Slava32
Slava32

Reputation: 171

new array with additional element does not visualize

I have the following diagram https://jsfiddle.net/r96upg74/5/

I create a new array also called data with one more element 'Total' at the end using the following code:

var value0Sum = 0,
    value1Sum = 0;

value0Sum = d3.sum(data, function(d){return d[values[0]];});
value1Sum = d3.sum(data, function(d){return d[values[1]];});

data = function (array) {
        r = array.map(function (d) {
            return { id: d.id, value1: d[values[0]], value2: d[values[1]] }} 
        );
    return r.concat([{id:"Total", value1: value0Sum, value2: value1Sum}]);
}(data);

It results in: https://jsfiddle.net/r96upg74/7/

However, diagram does not visualize the new data properly.

Any help will be appreciated, Thanks

Upvotes: 1

Views: 40

Answers (1)

floribon
floribon

Reputation: 19193

There are some confusions in your code.

This loop is very weird:

data.forEach(function(d) {
  for (var i = 0; i < values.length; i++) {
    d.value = +d[values[i]];
    return d;
  }
});

You assign something in d.value and then stop the loop, so you will never go further than i=0

I removed it because you don't need it.

Then your problem is that in your second fiddle, you transform the data which used to have the keys id, M, N in a data with keys id, value1, value2.

I have changed that with M and N and it is working: https://jsfiddle.net/3860zcc3/

Upvotes: 1

Related Questions