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;
      num? baseXValue;
      Offset? position;
      ChartTooltipInfo? tooltipInfo;
      final List<Color?> markerColors = <Color?>[];
      final List<ChartTooltipInfo> tooltipInfoList = <ChartTooltipInfo>[];
      final RenderBox? firstChild = parent.plotArea?.firstChild;
      RenderBox? series = firstChild;
      while (series != null && series.parentData != null) {
        final ContainerParentDataMixin<RenderBox> seriesParentData =
            series.parentData! as ContainerParentDataMixin<RenderBox>;
        // It specifies for cartesian series renderer.
        if (series is CartesianSeriesRenderer &&
            series.isVisible() &&
            series.enableTooltip) {
          final ChartTooltipInfo? info = series
              .tooltipInfoFromPointIndex(pointIndex) as ChartTooltipInfo?;
          if (info != null && series.index == seriesIndex) {
            baseXValue = (info.point as CartesianChartPoint).xValue;
            break;
          }
        }

        series = seriesParentData.nextSibling;
      }

      RenderBox? child = firstChild;
      while (child != null && child.parentData != null) {
        final ContainerParentDataMixin<RenderBox> childParentData =
            child.parentData! as ContainerParentDataMixin<RenderBox>;
        if (child is ChartSeriesRenderer &&
            child.isVisible() &&
            child.enableTooltip) {
          final ChartTooltipInfo? info = child
              .tooltipInfoFromPointIndex(pointIndex) as ChartTooltipInfo?;
          if (info != null && info.text != null) {
            if (child.index == seriesIndex) {
              tooltipInfo ??= info;
              position ??= info.primaryPosition;
            }
          }

          // It specifies for circular & funnel & pyramid series renderer.
          if (baseXValue == null) {
            if (tooltipInfo?.point.x == info?.point.x) {
              tooltipInfoList.add(info!);
            }
          } else {
            // It specifies for cartesian series renderer.
            if (child.canFindLinearVisibleIndexes) {
              final int binaryIndex = binarySearch(child.xValues,
                  baseXValue.toDouble(), 0, child.dataCount - 1);
              if (binaryIndex >= 0) {
                final ChartTooltipInfo? info =
                    child.tooltipInfoFromPointIndex(binaryIndex)
                        as ChartTooltipInfo?;
                if (info != null && info.text != null) {
                  final num? infoXValue =
                      (info.point as CartesianChartPoint).xValue;
                  if (infoXValue != null && baseXValue == infoXValue) {
                    tooltipInfoList.add(info);
                  }
                }
              }
            } else {
              final int index = child.xValues.indexOf(baseXValue);
              if (index >= 0) {
                final ChartTooltipInfo? info = child
                    .tooltipInfoFromPointIndex(index) as ChartTooltipInfo?;
                if (info != null && info.text != null) {
                  tooltipInfoList.add(info);
                }
              }
            }
          }
        }

        child = childParentData.nextSibling;
      }

      for (final ChartTooltipInfo info in tooltipInfoList) {
        if (text == null) {
          text = '${info.text}';
        } else {
          text += '\n';
          text += '${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 &&
            child.isVisible() &&
            child.enableTooltip) {
          if (child.index == seriesIndex) {
            final TooltipInfo? info =
                child.tooltipInfoFromPointIndex(pointIndex);
            if (info != null) {
              parent.showTooltip(info);
            }
          }
        }
      });
    }
  }
}