From 2ec683d18de18ee2c0b0af51ecd55ae02aba929e Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Fri, 7 Aug 2026 22:23:00 +0200 Subject: [PATCH] Keep the chart tooltips inside the chart --- .../widgets/charts/chart_widgets.dart | 12 ++++++++++++ .../widgets/charts/chart_widgets_test.dart | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/features/measurements/widgets/charts/chart_widgets.dart b/lib/features/measurements/widgets/charts/chart_widgets.dart index 3a16bd92e..7ae2988ec 100644 --- a/lib/features/measurements/widgets/charts/chart_widgets.dart +++ b/lib/features/measurements/widgets/charts/chart_widgets.dart @@ -355,6 +355,10 @@ class MeasurementChartWidgetFl extends StatelessWidget { LineTouchData tooltipData(BuildContext context, Set hidden, Map labels) { return LineTouchData( touchTooltipData: LineTouchTooltipData( + // Kept inside the chart, else the points near an edge show a tooltip + // that is cut off by it, or lies outside it entirely + fitInsideHorizontally: true, + fitInsideVertically: true, getTooltipColor: (touchedSpot) => Theme.of(context).colorScheme.primaryContainer, getTooltipItems: (touchedSpots) { // The plan context belongs to the touched date, not to a series, so @@ -534,6 +538,10 @@ class MeasurementBarChartWidgetFl extends StatelessWidget { BarTouchData tooltipData(BuildContext context) { return BarTouchData( touchTooltipData: BarTouchTooltipData( + // Kept inside the chart, else the bars near an edge show a + // tooltip that is cut off by it, or lies outside it entirely + fitInsideHorizontally: true, + fitInsideVertically: true, getTooltipColor: (group) => Theme.of(context).colorScheme.primaryContainer, getTooltipItem: (group, groupIndex, rod, rodIndex) { if (groupIndex < 0 || groupIndex >= _entries.length) { @@ -660,6 +668,10 @@ class MeasurementStackedBarChartWidgetFl extends StatelessWidget { BarTouchData tooltipData(BuildContext context) { return BarTouchData( touchTooltipData: BarTouchTooltipData( + // Kept inside the chart, else the bars near an edge show a + // tooltip that is cut off by it, or lies outside it entirely + fitInsideHorizontally: true, + fitInsideVertically: true, getTooltipColor: (group) => Theme.of(context).colorScheme.primaryContainer, getTooltipItem: (group, groupIndex, rod, rodIndex) { if (groupIndex < 0 || groupIndex >= _entries.length) { diff --git a/test/features/measurements/widgets/charts/chart_widgets_test.dart b/test/features/measurements/widgets/charts/chart_widgets_test.dart index 309759367..6f0badcc7 100644 --- a/test/features/measurements/widgets/charts/chart_widgets_test.dart +++ b/test/features/measurements/widgets/charts/chart_widgets_test.dart @@ -56,6 +56,23 @@ void main() { expect(find.byType(BarChart), findsOneWidget); }); + testWidgets('the tooltip stays inside the chart', (tester) async { + // Without this the reading at either end shows a tooltip that is cut + // off by the chart edge, or lies outside it altogether + await tester.pumpWidget( + _wrap(MeasurementBarChartWidgetFl([entry(1000, DateTime(2026, 1, 1))], 'steps')), + ); + await tester.pumpAndSettle(); + + final tooltip = tester + .widget(find.byType(BarChart)) + .data + .barTouchData + .touchTooltipData; + expect(tooltip.fitInsideHorizontally, isTrue); + expect(tooltip.fitInsideVertically, isTrue); + }); + testWidgets('only labels a few dates on the x axis', (tester) async { final entries = List.generate( 40,