show method
Displays the tooltip at the specified x and y-values.
*x & y - x & y point values at which the tooltip needs to be shown.
-
xAxisName - name of the x axis the given point must be bind to.
-
yAxisName - name of the y axis the given point must be bind to.
Implementation
void show(dynamic x, double y, [String? xAxisName, String? yAxisName]) {
if (_stateProperties != null &&
_stateProperties.chart is SfCartesianChart) {
final dynamic chart = _stateProperties.chart;
final RenderingDetails renderingDetails =
_stateProperties.renderingDetails;
final TooltipBehaviorRenderer tooltipBehaviorRenderer =
renderingDetails.tooltipBehaviorRenderer;
bool? isInsidePointRegion = false;
ChartAxisRendererDetails? xAxisDetails, yAxisDetails;
if (xAxisName != null && yAxisName != null) {
for (final ChartAxisRenderer axisRenderer
in _stateProperties.chartAxis.axisRenderersCollection) {
final ChartAxisRendererDetails axisDetails =
AxisHelper.getAxisRendererDetails(axisRenderer);
if (axisDetails.name == xAxisName) {
xAxisDetails = axisDetails;
} else if (axisDetails.name == yAxisName) {
yAxisDetails = axisDetails;
}
}
} else {
xAxisDetails = _stateProperties.chartAxis.primaryXAxisDetails;
yAxisDetails = _stateProperties.chartAxis.primaryYAxisDetails;
}
final ChartLocation position = calculatePoint(
(x is DateTime &&
(xAxisDetails! is DateTimeCategoryAxisDetails) == false)
? x.millisecondsSinceEpoch
: ((x is DateTime && xAxisDetails! is DateTimeCategoryAxisDetails)
? (xAxisDetails as DateTimeCategoryAxisDetails)
.labels
.indexOf(xAxisDetails.dateFormat.format(x))
: ((x is String && xAxisDetails is CategoryAxisDetails)
? xAxisDetails.labels.indexOf(x)
: x)),
y,
xAxisDetails!,
yAxisDetails!,
_stateProperties.requireInvertedAxis,
null,
_stateProperties.chartAxis.axisClipRect);
for (int i = 0;
i < _stateProperties.chartSeries.visibleSeriesRenderers.length;
i++) {
final SeriesRendererDetails seriesRendererDetails =
SeriesHelper.getSeriesRendererDetails(
_stateProperties.chartSeries.visibleSeriesRenderers[i]);
if (seriesRendererDetails.visible! == true &&
seriesRendererDetails.series.enableTooltip == true &&
seriesRendererDetails.regionalData != null) {
final double padding = (seriesRendererDetails.seriesType ==
'bubble' ||
seriesRendererDetails.seriesType == 'scatter' ||
seriesRendererDetails.seriesType.contains('column') == true ||
seriesRendererDetails.seriesType.contains('bar') == true)
? 0
: 15; // regional padding to detect smooth touch
seriesRendererDetails.regionalData!
.forEach((dynamic regionRect, dynamic values) {
final Rect region = regionRect[0];
final Rect paddedRegion = Rect.fromLTRB(
region.left - padding,
region.top - padding,
region.right + padding,
region.bottom + padding);
if (paddedRegion.contains(Offset(position.x, position.y))) {
isInsidePointRegion = true;
}
});
}
}
if (chart.tooltipBehavior.builder != null && x != null) {
renderingDetails.tooltipBehaviorRenderer._tooltipRenderingDetails
.showTemplateTooltip(Offset(position.x, position.y));
} else if (renderingDetails.tooltipBehaviorRenderer
._tooltipRenderingDetails.tooltipTemplate ==
null) {
final SfTooltipState? tooltipState =
tooltipBehaviorRenderer._tooltipRenderingDetails.chartTooltipState;
if (isInsidePointRegion ?? false) {
tooltipBehaviorRenderer._tooltipRenderingDetails
.showTooltip(position.x, position.y);
} else {
// To show tooltip when the position is out of point region.
tooltipBehaviorRenderer._tooltipRenderingDetails.show = true;
tooltipState?.needMarker = false;
renderingDetails.tooltipBehaviorRenderer._tooltipRenderingDetails
.showChartAreaTooltip(Offset(position.x, position.y),
xAxisDetails, yAxisDetails, chart);
}
}
tooltipBehaviorRenderer._tooltipRenderingDetails.isInteraction = false;
}
}