showByIndex method

void showByIndex(
  1. int seriesIndex,
  2. int pointIndex
)

Displays the tooltip at the specified series and point index.

  • seriesIndex - index of the series for which the pointIndex is specified

  • pointIndex - index of the point for which the tooltip should be shown

Implementation

void showByIndex(int seriesIndex, int pointIndex) {
  final RenderBehaviorArea? parent = parentBox as RenderBehaviorArea?;
  if (parent != null) {
    if (shared) {
      String? text;
      String? header;
      Offset? position;
      final List<Color?> markerColors = <Color?>[];
      ChartTooltipInfo? tooltipInfo;
      final List<ChartTooltipInfo> tooltipInfoList = <ChartTooltipInfo>[];
      RenderBox? child = parent.plotArea?.firstChild;
      while (child != null && child.parentData != null) {
        final ContainerParentDataMixin<RenderBox> childParentData =
            child.parentData! as ContainerParentDataMixin<RenderBox>;
        if (child is ChartSeriesRenderer) {
          final ChartTooltipInfo? info = child
              .tooltipInfoFromPointIndex(pointIndex) as ChartTooltipInfo?;
          if (info != null && info.text != null) {
            if (child.index == seriesIndex) {
              tooltipInfo ??= info;
              position ??= info.primaryPosition;
            }
            tooltipInfoList.add(info);
          }
        }

        child = childParentData.nextSibling;
      }

      for (final ChartTooltipInfo info in tooltipInfoList) {
        if (tooltipInfo?.point.x == info.point.x) {
          if (text == null) {
            if (info.hasMultipleYValues) {
              text = '${info.header}\n${info.text}';
            } else {
              text = '${info.header}${info.text}';
            }
          } else {
            text += '\n';
            if (info.hasMultipleYValues) {
              text += '${info.header}\n${info.text}';
            } else {
              text += '${info.header}${info.text}';
            }
          }
          if (info.markerColors.isNotEmpty) {
            markerColors.add(info.markerColors[0]);
          }
        }
      }

      if (tooltipInfo != null && text != null && position != null) {
        parent.showTooltip(ChartTooltipInfo(
          primaryPosition: position,
          secondaryPosition: tooltipInfo.secondaryPosition,
          text: text,
          data: tooltipInfo.data,
          point: tooltipInfo.point,
          series: tooltipInfo.series,
          renderer: tooltipInfo.renderer,
          header: header ?? tooltipInfo.header,
          seriesIndex: seriesIndex,
          pointIndex: pointIndex,
          segmentIndex: tooltipInfo.segmentIndex,
          markerColors: markerColors,
          markerBorderColor: tooltipInfo.markerBorderColor,
          markerType: tooltipInfo.markerType,
        ));
      }
    } else {
      parent.plotArea?.visitChildren((RenderObject child) {
        if (child is ChartSeriesRenderer) {
          final ChartSeriesRenderer render = child;
          if (render.index == seriesIndex) {
            final TooltipInfo? info =
                render.tooltipInfoFromPointIndex(pointIndex);
            if (info != null) {
              parent.showTooltip(info);
            }
          }
        }
      });
    }
  }
}