Skip to content

Commit

Permalink
Better intervals
Browse files Browse the repository at this point in the history
While this still doesn't look as nice and centered as I would like to,
it is at least an improvement
  • Loading branch information
rolandgeider committed Nov 5, 2023
1 parent 511cde1 commit 005f4d3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
4 changes: 4 additions & 0 deletions lib/helpers/charts.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
double chartGetInterval(DateTime first, DateTime last, {divider: 3}) {
final dayDiff = last.difference(first);
return dayDiff.inMilliseconds.toDouble() / 3;
}
14 changes: 9 additions & 5 deletions lib/widgets/core/charts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -32,8 +33,6 @@ class MeasurementChartWidgetFl extends StatefulWidget {
}

class _MeasurementChartWidgetFlState extends State<MeasurementChartWidgetFl> {
final interval = 15 * Duration.millisecondsPerDay / 1000 / 60;

@override
Widget build(BuildContext context) {
return AspectRatio(
Expand Down Expand Up @@ -84,12 +83,17 @@ class _MeasurementChartWidgetFlState extends State<MeasurementChartWidgetFl> {
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(
Expand All @@ -112,7 +116,7 @@ class _MeasurementChartWidgetFlState extends State<MeasurementChartWidgetFl> {
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,
Expand Down
19 changes: 11 additions & 8 deletions lib/widgets/workouts/charts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -47,8 +48,6 @@ class _LogChartWidgetFlState extends State<LogChartWidgetFl> {
aspectRatio: 1.70,
child: Padding(
padding: const EdgeInsets.only(
right: 18,
left: 12,
top: 24,
bottom: 12,
),
Expand All @@ -60,11 +59,6 @@ class _LogChartWidgetFlState extends State<LogChartWidgetFl> {
}

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,
Expand Down Expand Up @@ -96,12 +90,21 @@ class _LogChartWidgetFlState extends State<LogChartWidgetFl> {
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(
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/workouts/log.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ExerciseLogChart extends StatelessWidget {
return FutureBuilder(
future: getChartEntries(context),
builder: (context, AsyncSnapshot<Map<String, dynamic>> snapshot) => SizedBox(
height: 150,
height: 190,
child: snapshot.connectionState == ConnectionState.waiting
? const Center(child: CircularProgressIndicator())
: LogChartWidgetFl(snapshot.data!, _currentDate),
Expand Down

0 comments on commit 005f4d3

Please sign in to comment.