It can be done like this. Giving the class to g, which contains the full stack:
//Now each g which holds a stack has a class for selection. var state = svg.selectAll(".state").data(data).enter().append("g").attr("class", "g").attr("transform", function (d) { return "translate(" + x(d.State) + ",0)"; }).attr("id", function (d) { return d.State; }).attr("class", "stack");
And then in the change code that runs when the selection changes, perform the transition as follows:
//translate the stack post sorting. transition.selectAll(".stack") .delay(delay) .attr("transform", function (d) { return "translate(" + x0(d.State) + ",0)"; });
I added comments to the code so you can understand the demo
Full working fiddle here .
Cyril source share