Keep the chart tooltips inside the chart

This commit is contained in:
Roland Geider
2026-08-07 22:23:00 +02:00
parent 62e06d63a4
commit 2ec683d18d
2 changed files with 29 additions and 0 deletions

View File

@@ -355,6 +355,10 @@ class MeasurementChartWidgetFl extends StatelessWidget {
LineTouchData tooltipData(BuildContext context, Set<int> hidden, Map<int, String?> 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) {

View File

@@ -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<BarChart>(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,