2

I have something similar to: Normalized Stacked Bar Chart

Is it possible / appropriate to instead of calculating the upper Y and lower Y of each layer to use the d3.layout.stack() feature instead? I'm unsure so would like to know when and when not to use it.

Jijo
  • 611
  • 5
  • 18
Alex KeySmith
  • 16,657
  • 11
  • 74
  • 152
  • 2
    Yes, see e.g. [this example](http://bl.ocks.org/mbostock/3885211). Also see the comment regarding the use of `d3.layout.stack()` in [this example](http://bl.ocks.org/mbostock/3886208). – Lars Kotthoff Mar 07 '14 at 13:30

1 Answers1

6

This question is old, but it doesn't have the answer I was looking for. The example that Lars Kotthoff doesn't actually normalize the data as the data is provided as normalized already. To normalize your stack, add .offset('expand') to the stack initialization. Building off the stacked area chart example, the stack initialization would just be the following instead:

var stack = d3.layout.stack()
              .offset('expand')
              .values(function(d) { return d.values; });

Simple, but it took me hours before realizing it was in the documentation. Since this is one of the top results on Google now, hopefully this helps save some time for someone in the future.

kevmo314
  • 4,223
  • 4
  • 32
  • 43
  • Interesting, thanks Kevmo314 I'm in a different world now, so won't have a chance to test it out. But it sounds good, so I'll mark it as the answer. – Alex KeySmith Aug 07 '14 at 08:51