detectTooltipIntersection method

void detectTooltipIntersection(
  1. Offset screenCoordinates
)

Implementation

void detectTooltipIntersection(Offset screenCoordinates) {
  const intersectionThreshold = 20.0;
  for (final item in widget.items) {
    if (item is SeriesItem) {
      for (final dataPoint in item.points) {
        final mappedScreenPoint = transformWorldToScreen(
          dataPoint.x,
          dataPoint.y,
        );
        if ((mappedScreenPoint - screenCoordinates).distance <
            intersectionThreshold) {
          setState(() {
            activeTooltip = TooltipData(
              screenPosition: screenCoordinates,
              x: dataPoint.x,
              y: dataPoint.y,
              label: dataPoint.label,
              color: dataPoint.color ?? item.color,
            );
          });
          return;
        }
      }
    } else if (item is PointItem) {
      final mappedScreenPoint = transformWorldToScreen(item.x, item.y);
      if ((mappedScreenPoint - screenCoordinates).distance <
          intersectionThreshold) {
        setState(() {
          activeTooltip = TooltipData(
            screenPosition: screenCoordinates,
            x: item.x,
            y: item.y,
            label: item.label,
            color: item.color,
          );
        });
        return;
      }
    }
  }
  if (activeTooltip != null) setState(() => activeTooltip = null);
}