diff --git a/lib/helpers/charts.dart b/lib/helpers/charts.dart new file mode 100644 index 000000000..522d059dc --- /dev/null +++ b/lib/helpers/charts.dart @@ -0,0 +1,4 @@ +double chartGetInterval(DateTime first, DateTime last, {divider: 3}) { + final dayDiff = last.difference(first); + return dayDiff.inMilliseconds.toDouble() / 3; +} diff --git a/lib/widgets/core/charts.dart b/lib/widgets/core/charts.dart index 4fd223ae2..0b62ed2d5 100644 --- a/lib/widgets/core/charts.dart +++ b/lib/widgets/core/charts.dart @@ -19,6 +19,7 @@ import 'package:fl_chart/fl_chart.dart'; import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; +import 'package:wger/helpers/charts.dart'; import 'package:wger/theme/theme.dart'; class MeasurementChartWidgetFl extends StatefulWidget { @@ -32,8 +33,6 @@ class MeasurementChartWidgetFl extends StatefulWidget { } class _MeasurementChartWidgetFlState extends State { - final interval = 15 * Duration.millisecondsPerDay / 1000 / 60; - @override Widget build(BuildContext context) { return AspectRatio( @@ -84,12 +83,17 @@ class _MeasurementChartWidgetFlState extends State { sideTitles: SideTitles( showTitles: true, getTitlesWidget: (value, meta) { - final DateTime date = DateTime.fromMillisecondsSinceEpoch(value.toInt() * 1000 * 60); + // Don't show the first and last entries, otherwise they'll overlap with the + // calculated interval + if (value == meta.min || value == meta.max) { + return const Text(''); + } + final DateTime date = DateTime.fromMillisecondsSinceEpoch(value.toInt()); return Text( DateFormat.yMd(Localizations.localeOf(context).languageCode).format(date), ); }, - interval: interval, + interval: chartGetInterval(widget._entries.last.date, widget._entries.first.date), ), ), leftTitles: AxisTitles( @@ -112,7 +116,7 @@ class _MeasurementChartWidgetFlState extends State { LineChartBarData( spots: [ ...widget._entries - .map((e) => FlSpot(e.date.millisecondsSinceEpoch / 1000 / 60, e.value.toDouble())) + .map((e) => FlSpot(e.date.millisecondsSinceEpoch.toDouble(), e.value.toDouble())) ], isCurved: false, color: wgerSecondaryColor, diff --git a/lib/widgets/workouts/charts.dart b/lib/widgets/workouts/charts.dart index b7a3f7ec3..082830470 100644 --- a/lib/widgets/workouts/charts.dart +++ b/lib/widgets/workouts/charts.dart @@ -20,6 +20,7 @@ import 'package:fl_chart/fl_chart.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:intl/intl.dart'; +import 'package:wger/helpers/charts.dart'; import 'package:wger/helpers/colors.dart'; /// Sample time series data type. @@ -47,8 +48,6 @@ class _LogChartWidgetFlState extends State { aspectRatio: 1.70, child: Padding( padding: const EdgeInsets.only( - right: 18, - left: 12, top: 24, bottom: 12, ), @@ -60,11 +59,6 @@ class _LogChartWidgetFlState extends State { } LineChartData mainData() { - final dayDiff = DateTime.parse(widget._data['logs'].keys.last) - .difference(DateTime.parse(widget._data['logs'].keys.first)); - - final interval = dayDiff.inDays * 1.3 * Duration.millisecondsPerDay; - return LineChartData( gridData: FlGridData( show: true, @@ -96,12 +90,21 @@ class _LogChartWidgetFlState extends State { sideTitles: SideTitles( showTitles: true, getTitlesWidget: (value, meta) { + // Don't show the first and last entries, otherwise they'll overlap with the + // calculated interval + if (value == meta.min || value == meta.max) { + return const Text(''); + } + final DateTime date = DateTime.fromMillisecondsSinceEpoch(value.toInt()); return Text( DateFormat.yMd(Localizations.localeOf(context).languageCode).format(date), ); }, - interval: interval, + interval: chartGetInterval( + DateTime.parse(widget._data['logs'].keys.first), + DateTime.parse(widget._data['logs'].keys.last), + ), ), ), leftTitles: AxisTitles( diff --git a/lib/widgets/workouts/log.dart b/lib/widgets/workouts/log.dart index e58d0d732..2ed27940e 100644 --- a/lib/widgets/workouts/log.dart +++ b/lib/widgets/workouts/log.dart @@ -44,7 +44,7 @@ class ExerciseLogChart extends StatelessWidget { return FutureBuilder( future: getChartEntries(context), builder: (context, AsyncSnapshot> snapshot) => SizedBox( - height: 150, + height: 190, child: snapshot.connectionState == ConnectionState.waiting ? const Center(child: CircularProgressIndicator()) : LogChartWidgetFl(snapshot.data!, _currentDate),