Skip to content

Bug: BarChart touchedStackItemIndex always -1 for downward (negative) rod stack items #2104

Description

@ultramcu

Bug

When a BarChartRodData is downward (fromY > toY, e.g. fromY: 0, toY: -10 — common for plotting losses or values below a baseline) and has rodStackItems, the painter detects the outer rod but the inner stack item is silently missed: BarTouchResponse.touchedStackItemIndex is always -1 and touchedStackItem is always null, no matter where the touch lands inside the rod.

This breaks tooltips, getTooltipItem, and any callback that keys off touchedStackItemIndex for negative bars (e.g. financial loss bars).

Cause

BarChartPainter.handleTouch checks:

if (touchedPoint.dy <= fromPixel && touchedPoint.dy >= toPixel) { ... }

at bar_chart_painter.dart:946. For an upward stack item (toY > fromY), toPixel < fromPixel in screen space (Flutter Y grows downward) and the inequality describes the rod's pixel range correctly. For a downward stack item (fromY > toY), fromPixel < toPixel and the predicate dy <= small && dy >= big is mathematically empty — no dy can ever satisfy it.

Reproduction (no real device needed)

final data = BarChartData(
  minY: -10,
  maxY: 0,
  barGroups: [
    BarChartGroupData(x: 0, barRods: [
      BarChartRodData(
        fromY: 0,
        toY: -10,
        width: 20,
        rodStackItems: [BarChartRodStackItem(0, -10, Colors.red)],
      ),
    ]),
  ],
);
final painter = BarChartPainter();
final holder = PaintHolder<BarChartData>(data, data, TextScaler.noScaling);
final r = painter.handleTouch(const Offset(100, 50), const Size(200, 100), holder);
// r.touchedStackItemIndex is -1 (expected 0)
// r.touchedStackItem is null (expected not null)

Fix

Make the check orientation-agnostic with min/max. PR #2103 has the one-line fix + a regression test inside the existing handleTouch() group.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions