markAt static method

PlotMark? markAt(
  1. ChartScene scene,
  2. Offset point, {
  3. double radius = tapRadius,
})

The mark a tap at point selects: first any mark whose PlotMark.region contains the point (bars), else the nearest PlotMark.center within radius. Returns null when nothing is close enough.

Implementation

static PlotMark? markAt(
  ChartScene scene,
  Offset point, {
  double radius = tapRadius,
}) {
  if (scene.isEmpty || !point.dx.isFinite || !point.dy.isFinite) return null;

  for (final m in scene.marks) {
    final r = m.region;
    if (r != null && r.contains(point)) return m;
    final p = m.hitPath;
    if (p != null && p.contains(point)) return m;
  }

  // Compare squared distances to avoid a sqrt per mark (called on every hover
  // move in per-mark mode).
  PlotMark? best;
  var bestSq = double.infinity;
  for (final m in scene.marks) {
    final dx = m.center.dx - point.dx;
    final dy = m.center.dy - point.dy;
    final sq = dx * dx + dy * dy;
    if (sq < bestSq) {
      bestSq = sq;
      best = m;
    }
  }
  return (best != null && bestSq <= radius * radius) ? best : null;
}