Codebase list node-d3-shape / e423f31 src / order / insideOut.js
e423f31

Tree @e423f31 (Download .tar.gz)

insideOut.js @e423f31raw · history · blame

import appearance from "./appearance";
import {sum} from "./ascending";

export default function(series) {
  var n = series.length,
      i,
      j,
      sums = series.map(sum),
      order = appearance(series),
      top = 0,
      bottom = 0,
      tops = [],
      bottoms = [];

  for (i = 0; i < n; ++i) {
    j = order[i];
    if (top < bottom) {
      top += sums[j];
      tops.push(j);
    } else {
      bottom += sums[j];
      bottoms.push(j);
    }
  }

  return bottoms.reverse().concat(tops);
}