nearestIndexAtX static method

int? nearestIndexAtX(
  1. ChartScene scene,
  2. double px
)

The column index nearest pixel x px — the trackball column. Uses the scene's ChartScene.scale when present, else the nearest mark by center x.

Implementation

static int? nearestIndexAtX(ChartScene scene, double px) {
  if (scene.isEmpty || !px.isFinite) return null;
  final scale = scene.scale;
  if (scale != null) return scale.nearestIndex(px);

  int? best;
  var bestDist = double.infinity;
  for (final m in scene.marks) {
    final d = (m.center.dx - px).abs();
    if (d < bestDist) {
      bestDist = d;
      best = m.index;
    }
  }
  return best;
}