getNearestDatumDetailPerSeries method

  1. @override
List<DatumDetails<D>> getNearestDatumDetailPerSeries(
  1. Point<double> chartPoint,
  2. bool byDomain,
  3. Rectangle<int>? boundsOverride, {
  4. bool selectOverlappingPoints = false,
  5. bool selectExactEventLocation = false,
})
override

Gets a list of the data from each series that is closest to a given point.

chartPoint represents a point in the chart, such as a point that was clicked/tapped on by a user.

selectOverlappingPoints specifies whether to include all points that overlap the tapped position in the result. If specified, the method will return either the closest point or all the overlapping points with the tapped position.

byDomain specifies whether the nearest data should be defined by domain distance, or relative Cartesian distance.

boundsOverride optionally specifies a bounding box for the selection event. If specified, then no data should be returned if chartPoint lies outside the box. If not specified, then each series renderer on the chart will use its own component bounds for filtering out selection events (usually the chart draw area).

Implementation

@override
List<DatumDetails<D>> getNearestDatumDetailPerSeries(
  Point<double> chartPoint,
  bool byDomain,
  Rectangle<int>? boundsOverride, {
  bool selectOverlappingPoints = false,
  bool selectExactEventLocation = false,
}) {
  final nearest = <DatumDetails<D>>[];
  final inside = <DatumDetails<D>>[];

  // Was it even in the component bounds?
  if (!isPointWithinBounds(chartPoint, boundsOverride)) {
    return nearest;
  }

  for (final points in seriesPointMap.values) {
    PointRendererElement<D>? nearestPoint;

    var nearestDistances = _Distances(
      domainDistance: _maxInitialDistance,
      measureDistance: _maxInitialDistance,
      relativeDistance: _maxInitialDistance,
    );

    for (final point in points) {
      if (point.overlaySeries) {
        continue;
      }

      final p = point._currentPoint!.point!;

      // Don't look at points not in the drawArea.
      if (p.x! < componentBounds!.left || p.x! > componentBounds!.right) {
        continue;
      }

      final distances = _getDatumDistance(point, chartPoint);

      if (selectOverlappingPoints) {
        if (distances.insidePoint!) {
          inside.add(_createDatumDetails(point._currentPoint!, distances));
        }
      }

      // If any point was added to the inside list on previous iterations,
      // we don't need to go through calculating nearest points because we
      // only return inside list as a result in that case.
      if (inside.isEmpty) {
        // Do not consider the points outside event location when
        // selectExactEventLocation flag is set.
        if (!selectExactEventLocation || distances.insidePoint!) {
          if (byDomain) {
            if ((distances.domainDistance <
                    nearestDistances.domainDistance) ||
                (distances.domainDistance ==
                        nearestDistances.domainDistance &&
                    distances.measureDistance <
                        nearestDistances.measureDistance)) {
              nearestPoint = point._currentPoint;
              nearestDistances = distances;
            }
          } else {
            if (distances.relativeDistance <
                nearestDistances.relativeDistance) {
              nearestPoint = point._currentPoint;
              nearestDistances = distances;
            }
          }
        }
      }
    }

    // Found a point, add it to the list.
    if (nearestPoint != null) {
      nearest.add(_createDatumDetails(nearestPoint, nearestDistances));
    }
  }

  // Note: the details are already sorted by domain & measure distance in
  // base chart. If asking for all overlapping points, return the list of
  // inside points - only if there was overlap.
  return (selectOverlappingPoints && inside.isNotEmpty) ? inside : nearest;
}