stackOrderInsideOut function Stack orders Stacks

List<int> stackOrderInsideOut(
  1. List<List<List<num>>> series
)

Returns a series order such that the earliest series (according to the maximum value) are on the inside and the later series are on the outside.

final stack = Stack(…)..order = stackOrderInsideOut;

This order is recommended for streamgraphs in conjunction with the stackOffsetWiggle. See Stacked Graphs — Geometry & Aesthetics by Byron & Wattenberg for more information.

Implementation

List<int> stackOrderInsideOut(List<List<List<num>>> series) {
  int n = series.length, i, j;
  var sums = series.map(sum).toList(),
      order = stackOrderAppearance(series),
      tops = <int>[],
      bottoms = <int>[];
  num top = 0, bottom = 0;

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

  return bottoms.reversed.followedBy(tops).toList();
}