score:2

Accepted answer

using a key function when binding the data is a very good practice in d3, which ensures object constancy. so, contrary to the suggestion in the accepted other answer, you should not remove it.

instead of removing it, just fix the key function. the problem right now is that since the keys are...

["team 1 cc", "team 2 cc", "team 3 cc"]

... for the selected dataset, this...

d.key.split(' ')[0]

.. will return team for every group, and the keys obviously cannot be the same.

so, just do...

d.key.split(' ')[0] + d.key.split(' ')[1]

or even just...

d.key.slice(0, -3)

... which will return: team 1, team 2 and team 3.

here is your updated plunker: https://plnkr.co/edit/xcvmdj9a3yf37dzdma6d?p=preview

score:1

it looks like your mistake in this code:

let bargroups = g.selectall("g.layer")
  .data(stacking, d=>d.key.split(' ')[0]);

you pass the second argument to .data method. according to docs:

selection.data([data[, key]])

... a key function may be specified to control which datum is assigned to which element, replacing the default join-by-index, by computing a string identifier for each datum and element.

if you remove it your visualization will work properly, checkmy fork of your plnkr.


Related Query