d4.js
function(key, items) {
      var offsets = {};

      var stack = d3.layout.stack()
        .values(function(d) {
          return d.values;
        })
        .x(function(d) {
          return d[key];
        })
        .y(function(d) {
          return +d[opts.value.key];
        })
        .out(function(d, y0, y) {
          d.y = y;
          if (d.y >= 0) {
            d.y0 = offsets[d[key] + 'Pos'] = offsets[d[key] + 'Pos'] || 0;
            offsets[d[key] + 'Pos'] += y;
          } else {
            d.y0 = offsets[d[key] + 'Neg'] = offsets[d[key] + 'Neg'] || 0;
            offsets[d[key] + 'Neg'] -= Math.abs(y);
          }
        });
      stack(items.reverse());
    }
function(key, items) {
      var offsets = {};

      var stack = d3.layout.stack()
        .values(function(d) {
          return d.values;
        })
        .x(function(d) {
          return d[key];
        })
        .y(function(d) {
          return +d[opts.value.key];
        })
        .out(function(d, y0, y) {
          d.y = y;
          if (d.y >= 0) {
            d.y0 = offsets[d[key] + 'Pos'] = offsets[d[key] + 'Pos'] || 0;
            offsets[d[key] + 'Pos'] += y;
          } else {
            d.y0 = offsets[d[key] + 'Neg'] = offsets[d[key] + 'Neg'] || 0;
            offsets[d[key] + 'Neg'] -= Math.abs(y);
          }
        });
      stack(items.reverse());
    }
function ( key , items ) { var offsets = { } ; var stack = d3 . layout . stack ( ) . values ( function ( d ) { return d . values ; } ) . x ( function ( d ) { return d [ key ] ; } ) . y ( function ( d ) { return + d [ opts . value . key ] ; } ) . out ( function ( d , y0 , y ) { d . y = y ; if ( d . y >= 0 ) { d . y0 = offsets [ d [ key ] + 'Pos' ] = offsets [ d [ key ] + 'Pos' ] || 0 ; offsets [ d [ key ] + 'Pos' ] += y ; } else { d . y0 = offsets [ d [ key ] + 'Neg' ] = offsets [ d [ key ] + 'Neg' ] || 0 ; offsets [ d [ key ] + 'Neg' ] -= Math . abs ( y ) ; } } ) ; stack ( items . reverse ( ) ) ; }
By default D3 doesn't handle stacks with negative values very well, we need to calulate or our y and y0 values for each group.
By default D3 doesn t handle stacks with negative values very well we need to calulate or our y and y0 values for each group .