barRects method

List<(int, Rect)> barRects(
  1. int pane,
  2. int index
)

A row's bars on a pane: each drawn mark's place and its box, in the graph's own pixels.

Implementation

List<(int, Rect)> barRects(int pane, int index) {
  final marks = spec.panes[pane].marks;
  final row = rows[index];
  final out = <(int, Rect)>[];
  final center = xOfRow(index);
  final plot = plotRect(pane);
  for (var i = 0; i < marks.length; i++) {
    final m = marks[i];
    if (m.type != 'bar' || m.overlay == null) continue;
    if (row[m.y] case final num v when v.isFinite) {
      final (_, hi) = overlayDomain(pane, m);
      final h = v / hi * plot.height;
      out.add((i, Rect.fromLTRB(center - barWidth / 2, plot.bottom - h, center + barWidth / 2, plot.bottom)));
    }
  }
  final groups = barGroups(pane);
  if (groups.isEmpty) return out;
  final gap = groups.length > 1 ? math.min(2.0, barWidth / (groups.length * 4)) : 0.0;
  final all = _groupWidth(groups.length, gap);
  final w = (all - gap * (groups.length - 1)) / groups.length;
  final zero = yOf(pane, 0);
  for (var g = 0; g < groups.length; g++) {
    final left = center - all / 2 + g * (w + gap);
    var up = 0.0, down = 0.0;
    for (final i in groups[g]) {
      if (row[marks[i].y] case final num v when v.isFinite && v != 0) {
        final a = v > 0 ? up : down;
        final b = a + v;
        if (v > 0) {
          up = b;
        } else {
          down = b;
        }
        final ya = yOf(pane, a), yb = yOf(pane, b);
        final r = Rect.fromLTRB(left, math.min(ya, yb), left + w, math.max(ya, yb));
        // A bar too thin to see still shows where it is.
        out.add((i, r.height < 1 ? Rect.fromLTRB(r.left, zero - 1, r.right, zero) : r));
      }
    }
  }
  return out;
}