Commit 2cb46d5

derdilla <82763757+NobodyForNothing@users.noreply.github.com>
2023-10-01 10:52:51
make graph title format dependent on the range that is actually shown
Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com>
1 parent 260966f
Changed files (1)
lib
lib/components/measurement_graph.dart
@@ -22,7 +22,7 @@ class _LineChart extends StatefulWidget {
 }
 
 class _LineChartState extends State<_LineChart> {
-  double _lineChartTitleIntervall = 100000000;
+  double _lineChartTitleIntervall = 100000000; // TODO: remove settings as well
   int _reloadsLeft = 30;
 
   @override
@@ -34,8 +34,7 @@ class _LineChartState extends State<_LineChart> {
             return Consumer<BloodPressureModel>(builder: (context, model, child) {
               var end = settings.displayDataEnd;
               return ConsistentFutureBuilder<UnmodifiableListView<BloodPressureRecord>>(
-                  future: (settings.graphStepSize == TimeStep.lifetime) ? model.all
-                      : model.getInTimeRange(settings.displayDataStart, end),
+                  future: model.getInTimeRange(settings.displayDataStart, end),
                   onData: (context, fetchedData) {
                     List<BloodPressureRecord> data = fetchedData.toList();
                     data.sort((a, b) => a.creationTime.compareTo(b.creationTime));
@@ -80,8 +79,9 @@ class _LineChartState extends State<_LineChart> {
                           LineChartData(
                               minY: minValue.toDouble(),
                               maxY: maxValue + 5,
-                              clipData: FlClipData.all(),
-                              titlesData: _buildFlTitlesData(settings),
+                              clipData: const FlClipData.all(),
+                              titlesData: _buildFlTitlesData(settings,
+                                  DateTimeRange(start: data.first.creationTime, end: data.last.creationTime)),
                               lineTouchData: const LineTouchData(
                                   touchTooltipData: LineTouchTooltipData(tooltipMargin: -200, tooltipRoundedRadius: 20)
                               ),
@@ -119,7 +119,7 @@ class _LineChartState extends State<_LineChart> {
     return bars;
   }
 
-  FlTitlesData _buildFlTitlesData(Settings settings) {
+  FlTitlesData _buildFlTitlesData(Settings settings, DateTimeRange graphRange) {
     const noTitels = AxisTitles(sideTitles: SideTitles(reservedSize: 40, showTitles: false));
     return FlTitlesData(
       topTitles: noTitels,
@@ -127,21 +127,8 @@ class _LineChartState extends State<_LineChart> {
       bottomTitles: AxisTitles(
         sideTitles: SideTitles(
           showTitles: true,
-          interval: _lineChartTitleIntervall,
-          getTitlesWidget: (double pos, TitleMeta meta) {
-            // calculate new intervall
-            // as graphWidth can technically be as low as one max is needed here to avoid freezes
-            double graphWidth = meta.max - meta.min;
-            if ((max(graphWidth - 2,1) / settings.graphTitlesCount) != _lineChartTitleIntervall && (_reloadsLeft > 0)) {
-              // simple hack needed to change the state during build https://stackoverflow.com/a/63607696/21489239
-              Future.delayed(Duration.zero, () async {
-                setState(() {
-                  _reloadsLeft--;
-                  _lineChartTitleIntervall = max(graphWidth - 2,1) / settings.graphTitlesCount;
-                });
-              });
-            }
 
+          getTitlesWidget: (double pos, TitleMeta meta) {
             // don't show fixed titles, as they are replaced by long dates below
             if (meta.axisPosition <= 1 || pos >= meta.max) {
               return const SizedBox.shrink();
@@ -149,29 +136,20 @@ class _LineChartState extends State<_LineChart> {
 
             // format of titles
             late final DateFormat formatter;
-            switch (settings.graphStepSize) {
-              case TimeStep.day:
-                formatter = DateFormat('H:m');
-                break;
-              case TimeStep.month:
-              case TimeStep.last7Days:
-                formatter = DateFormat('d');
-                break;
-              case TimeStep.week:
-                formatter = DateFormat('E');
-                break;
-              case TimeStep.year:
-                formatter = DateFormat('MMM');
-                break;
-              case TimeStep.lifetime:
-                formatter = DateFormat('yyyy');
-                break;
-              case TimeStep.last30Days:
-              case TimeStep.custom:
-                formatter = DateFormat.MMMd();
+
+            if (graphRange.duration < const Duration(days: 2)) {
+              formatter = DateFormat.Hm();
+            } else if (graphRange.duration < const Duration(days: 15)) {
+              formatter = DateFormat.EEEE();
+            } else if (graphRange.duration < const Duration(days: 30)) {
+              formatter = DateFormat.d();
+            } else if (graphRange.duration < const Duration(days: 500)) {
+              formatter = DateFormat.MMM();
+            } else {
+              formatter = DateFormat.y();
             }
-            return Text(formatter
-                .format(DateTime.fromMillisecondsSinceEpoch(pos.toInt())));
+              return Text(formatter
+                  .format(DateTime.fromMillisecondsSinceEpoch(pos.toInt())));
           }
         ),
       ),