From c4f7999ec4964681b7f9dbd89ba3a7615f512582 Mon Sep 17 00:00:00 2001 From: Hanh Date: Tue, 7 Sep 2021 12:40:21 +0800 Subject: [PATCH] Fix chart tooltip --- lib/chart.dart | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/chart.dart b/lib/chart.dart index 7ed062c..b5a7359 100644 --- a/lib/chart.dart +++ b/lib/chart.dart @@ -62,6 +62,9 @@ class LineChartTimeSeriesState extends State { theme.primaryColor, ]; + final textStyle = theme.textTheme.bodyText1; + final bgColor = theme.backgroundColor; + return LineChartData( gridData: FlGridData( show: true, @@ -131,6 +134,20 @@ class LineChartTimeSeriesState extends State { ), ), ], + lineTouchData: LineTouchData( + touchTooltipData: LineTouchTooltipData( + tooltipBgColor: bgColor, + getTooltipItems: (touchedSpots) => + touchedSpots.map((LineBarSpot spot) { + final x = spot.x; + final dt = DateTime.fromMillisecondsSinceEpoch(x.toInt() * DAY_MS); + final xdt = DateFormat.Md().format(dt); + final y = spot.y.toStringAsFixed(3); + return LineTooltipItem("$xdt - $y", + textStyle); + }).toList() + ) + ) ); } }