Allow disabling the average and the trend lines

This commit is contained in:
Roland Geider
2026-08-20 18:56:51 +02:00
parent 16bc83c306
commit 34b9b97081
23 changed files with 142 additions and 68 deletions

View File

@@ -143,7 +143,7 @@ class DashboardWeightWidget extends ConsumerWidget {
final (entriesAll, average) = sensibleRange(
points,
averageDays: category.chartSettings.averageWindow,
averageDays: category.chartSettings.averageWindow ?? ChartSettings.fallbackWindow,
);
return Column(

View File

@@ -260,7 +260,10 @@ MeasurementBucketLevel chartBucketLevel(MetricType metricType, ChartType chartTy
ChartRange range,
ChartSettings settings,
) {
final average = movingAverage(points, days: settings.averageWindow);
final average = movingAverage(
points,
days: settings.averageWindow ?? ChartSettings.fallbackWindow,
);
final cutoff = range.cutoff;
return cutoff == null

View File

@@ -92,17 +92,24 @@ enum ChartType {
);
}
/// What both line settings take when the user turns their line off
const chartLineOff = 'none';
/// How closely the trend line follows the values, as the EMA period it maps to.
///
/// Stored as the character rather than the number, so the periods stay tunable
/// without touching what users configured.
enum TrendCharacter {
/// No trend line at all, see [chartLineOff]
none(chartLineOff, null),
reactive('reactive', 5),
balanced('balanced', 10),
sluggish('sluggish', 20);
final String wireValue;
final int emaPeriod;
/// Null for [TrendCharacter.none], which draws nothing
final int? emaPeriod;
const TrendCharacter(this.wireValue, this.emaPeriod);
/// Falls back to [TrendCharacter.balanced], which is the unconfigured chart.
@@ -117,6 +124,7 @@ extension MeasurementTrendCharacterL10n on TrendCharacter {
String localized(BuildContext context) {
final l10n = AppLocalizations.of(context);
return switch (this) {
TrendCharacter.none => l10n.off,
TrendCharacter.reactive => l10n.trendReactive,
TrendCharacter.balanced => l10n.trendBalanced,
TrendCharacter.sluggish => l10n.trendSluggish,
@@ -133,10 +141,14 @@ class ChartSettings {
/// Windows the moving average may be computed over, in days.
static const averageWindows = [7, 14, 30];
/// Window the numbers derived from the average use when the line is off
static const fallbackWindow = 7;
final TrendCharacter trend;
/// Window the moving average covers, in days
final int averageWindow;
/// Window the moving average covers, in days, null when the user turned the
/// average line off
final int? averageWindow;
const ChartSettings({
this.trend = TrendCharacter.balanced,
@@ -152,7 +164,9 @@ class ChartSettings {
return ChartSettings(
trend: TrendCharacter.fromWire(config?['trend']),
averageWindow: window is int && averageWindows.contains(window)
averageWindow: window == chartLineOff
? null
: window is int && averageWindows.contains(window)
? window
: averageWindows.first,
);

View File

@@ -366,10 +366,13 @@ class _ChartSettingsSection extends StatelessWidget {
}
: null,
),
DropdownButtonFormField<int>(
initialValue: settings.averageWindow,
// The window is a number, "off" is the string the server stores, so
// the two share one field only as objects
DropdownButtonFormField<Object>(
initialValue: settings.averageWindow ?? chartLineOff,
decoration: InputDecoration(labelText: i18n.chartAverageWindow),
items: [
DropdownMenuItem(value: chartLineOff, child: Text(i18n.off)),
for (final days in ChartSettings.averageWindows)
DropdownMenuItem(value: days, child: Text(i18n.chartAverageWindowDays(days))),
],

View File

@@ -50,7 +50,9 @@ List<Widget> buildChartSection(
: buildChartForMetricType(
metricType,
raw,
avg,
// The average is still computed for the number below, the
// setting only decides whether the line is drawn
settings.averageWindow == null ? const [] : avg,
unit,
planPeriods: planPeriods,
trend: trend,
@@ -64,6 +66,16 @@ List<Widget> buildChartSection(
];
}
/// The trend over [raw], null when the user turned the trend line off
List<MeasurementChartEntry>? _trendlineFor(
List<MeasurementChartEntry> raw,
ChartSettings settings,
) {
final period = settings.trend.emaPeriod;
return period == null ? null : smoothedTrendline(raw, period: period);
}
/// The chart of a measurement series: [buildChartSection] for the chart the
/// type resolves to, plus the legend naming the lines it draws.
List<Widget> buildSeriesChartSection(
@@ -113,7 +125,8 @@ List<Widget> buildSeriesChartSection(
// The points arrive condensed, so the trend follows the shape across weeks
// rather than the swings within a single day
final summed = metricType.isSummedPerDay;
final trendAll = summed ? null : smoothedTrendline(entriesAll, period: settings.trend.emaPeriod);
final period = settings.trend.emaPeriod;
final trendAll = summed || period == null ? null : smoothedTrendline(entriesAll, period: period);
return [
...buildChartSection(
context,
@@ -134,12 +147,13 @@ List<Widget> buildSeriesChartSection(
text: AppLocalizations.of(context).indicatorRaw,
isSquare: true,
),
Indicator(
color: Theme.of(context).colorScheme.tertiary,
text: AppLocalizations.of(context).indicatorAvg,
isSquare: true,
),
if (!metricType.isSummedPerDay)
if (settings.averageWindow != null)
Indicator(
color: Theme.of(context).colorScheme.tertiary,
text: AppLocalizations.of(context).indicatorAvg,
isSquare: true,
),
if (trendAll != null)
Indicator(
color: Theme.of(context).colorScheme.secondary,
text: AppLocalizations.of(context).indicatorTrend,
@@ -416,7 +430,7 @@ Widget buildChartForMetricType(
raw,
unit,
avgs: avg,
trend: trend ?? smoothedTrendline(raw, period: settings.trend.emaPeriod),
trend: trend ?? _trendlineFor(raw, settings),
planPeriods: planPeriods,
);
}

View File

@@ -158,7 +158,7 @@ class _NutriscoreSlider extends StatelessWidget {
min: 0,
max: maxIndex.toDouble(),
divisions: maxIndex,
label: value == null ? i18n.filterNutriscoreOff : value!.name.toUpperCase(),
label: value == null ? i18n.off : value!.name.toUpperCase(),
onChanged: (v) => onChanged(_indexToValue(v.round())),
),
Padding(
@@ -167,7 +167,7 @@ class _NutriscoreSlider extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
i18n.filterNutriscoreOff,
i18n.off,
style: Theme.of(context).textTheme.labelSmall,
),
...NutriScore.values.map(

View File

@@ -26,6 +26,7 @@ import 'package:wger/features/account/providers/user_profile_notifier.dart';
import 'package:wger/features/measurements/charts/data.dart';
import 'package:wger/features/measurements/measurements.dart';
import 'package:wger/features/measurements/models/measurement_bucket.dart';
import 'package:wger/features/measurements/models/measurement_category.dart';
import 'package:wger/features/measurements/models/unit_conversion.dart';
import 'package:wger/features/measurements/providers/body_weight_provider.dart';
import 'package:wger/features/measurements/providers/measurement_notifier.dart';
@@ -78,7 +79,7 @@ class NutritionalPlansList extends riverpod.ConsumerWidget {
);
final average = movingAverage(
entriesAll,
days: category.chartSettings.averageWindow,
days: category.chartSettings.averageWindow ?? ChartSettings.fallbackWindow,
).whereDateWithInterpolation(startDate, endDate);
if (average.length < 2) {
return const SizedBox.shrink();

View File

@@ -21,6 +21,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:wger/features/account/providers/user_profile_notifier.dart';
import 'package:wger/features/measurements/charts/data.dart';
import 'package:wger/features/measurements/models/measurement_bucket.dart';
import 'package:wger/features/measurements/models/measurement_category.dart';
import 'package:wger/features/measurements/models/unit_conversion.dart';
import 'package:wger/features/measurements/providers/body_weight_provider.dart';
import 'package:wger/features/measurements/providers/measurement_notifier.dart';
@@ -79,7 +80,7 @@ class PlanWeightChart extends ConsumerWidget {
}
final settings = category.chartSettings;
final avg = movingAverage(points, days: settings.averageWindow);
final avg = movingAverage(points, days: settings.averageWindow ?? ChartSettings.fallbackWindow);
return Column(
children: buildChartSection(

View File

@@ -1011,9 +1011,9 @@
"@filterNutriscore": {
"description": "Heading for the Nutri-Score slider in the ingredient search filter dialog"
},
"filterNutriscoreOff": "Aus",
"@filterNutriscoreOff": {
"description": "Label for the first slider stop, which disables the Nutri-Score filter"
"off": "Aus",
"@off": {
"description": "Generic label for the option that switches something off, e.g. a filter or a line of a chart"
},
"filterNutriscoreNoFilter": "Kein Filter",
"@filterNutriscoreNoFilter": {

View File

@@ -1726,9 +1726,9 @@
"@filterNutriscore": {
"description": "Heading for the Nutri-Score slider in the ingredient search filter dialog"
},
"filterNutriscoreOff": "Off",
"@filterNutriscoreOff": {
"description": "Label for the first slider stop, which disables the Nutri-Score filter"
"off": "Off",
"@off": {
"description": "Generic label for the option that switches something off, e.g. a filter or a line of a chart"
},
"filterNutriscoreNoFilter": "No filter",
"@filterNutriscoreNoFilter": {

View File

@@ -1046,9 +1046,9 @@
"@filterNutriscore": {
"description": "Heading for the Nutri-Score slider in the ingredient search filter dialog"
},
"filterNutriscoreOff": "Apagado",
"@filterNutriscoreOff": {
"description": "Label for the first slider stop, which disables the Nutri-Score filter"
"off": "Apagado",
"@off": {
"description": "Generic label for the option that switches something off, e.g. a filter or a line of a chart"
},
"filterNutriscoreNoFilter": "Sin filtro",
"@filterNutriscoreNoFilter": {

View File

@@ -1017,9 +1017,9 @@
"@filterNutriscore": {
"description": "Heading for the Nutri-Score slider in the ingredient search filter dialog"
},
"filterNutriscoreOff": "Off",
"@filterNutriscoreOff": {
"description": "Label for the first slider stop, which disables the Nutri-Score filter"
"off": "Off",
"@off": {
"description": "Generic label for the option that switches something off, e.g. a filter or a line of a chart"
},
"filterNutriscoreNoFilter": "Pas de filtre",
"@filterNutriscoreNoFilter": {
@@ -1217,11 +1217,11 @@
"calculationDuplicate": "Vous avez déjà une catégorie {name} avec ces réglages",
"calculationLocked": "Le calcul ne peut pas être modifié. Supprimez la catégorie pour arrêter de la calculer.",
"calculationMissingHeight": "La taille manque dans votre profil, aucune valeur ne peut donc encore être calculée.",
"calculationNoSourceCategory": "Vous n'avez pas encore d'autre catégorie à lire.",
"calculationNoSourceCategory": "Vous n'avez pas encore d'autre catégorie dans laquelle puiser.",
"calculationBadge": "Calculé",
"calculationEntryInfo": "Cette valeur est calculée par wger et ne peut pas être modifiée",
"calculationNameBmi": "IMC",
"calculationNameWhtr": "Rapport tour de taille sur taille",
"calculationNameWhtr": "Rapport taille-hauteur",
"calculationNameOneRepMax": "1RM",
"calculationNameOneRmTotal": "Total 1RM",
"calculationDescriptionBmi": "À partir de votre poids et de la taille indiquée dans votre profil",

View File

@@ -1082,9 +1082,9 @@
"@filterNutriscore": {
"description": "Heading for the Nutri-Score slider in the ingredient search filter dialog"
},
"filterNutriscoreOff": "Isključeno",
"@filterNutriscoreOff": {
"description": "Label for the first slider stop, which disables the Nutri-Score filter"
"off": "Isključeno",
"@off": {
"description": "Generic label for the option that switches something off, e.g. a filter or a line of a chart"
},
"filterNutriscoreNoFilter": "Bez filtra",
"@filterNutriscoreNoFilter": {

View File

@@ -1305,9 +1305,9 @@
"@filterNutriscore": {
"description": "Heading for the Nutri-Score slider in the ingredient search filter dialog"
},
"filterNutriscoreOff": "Ki",
"@filterNutriscoreOff": {
"description": "Label for the first slider stop, which disables the Nutri-Score filter"
"off": "Ki",
"@off": {
"description": "Generic label for the option that switches something off, e.g. a filter or a line of a chart"
},
"filterNutriscoreNoFilter": "Nincs szűrés",
"@filterNutriscoreNoFilter": {

View File

@@ -1117,9 +1117,9 @@
"@filterNutriscore": {
"description": "Heading for the Nutri-Score slider in the ingredient search filter dialog"
},
"filterNutriscoreOff": "Mati",
"@filterNutriscoreOff": {
"description": "Label for the first slider stop, which disables the Nutri-Score filter"
"off": "Mati",
"@off": {
"description": "Generic label for the option that switches something off, e.g. a filter or a line of a chart"
},
"filterNutriscoreNoFilter": "Tanpa filter",
"@filterNutriscoreNoFilter": {

View File

@@ -1112,9 +1112,9 @@
"@filterNutriscore": {
"description": "Heading for the Nutri-Score slider in the ingredient search filter dialog"
},
"filterNutriscoreOff": "Spento",
"@filterNutriscoreOff": {
"description": "Label for the first slider stop, which disables the Nutri-Score filter"
"off": "Spento",
"@off": {
"description": "Generic label for the option that switches something off, e.g. a filter or a line of a chart"
},
"filterNutriscoreNoFilter": "Nessun filtro",
"@filterNutriscoreNoFilter": {

View File

@@ -1050,9 +1050,9 @@
},
"editorItalic": "Cursief",
"editorList": "Lijst",
"filterNutriscoreOff": "Uit",
"@filterNutriscoreOff": {
"description": "Label for the first slider stop, which disables the Nutri-Score filter"
"off": "Uit",
"@off": {
"description": "Generic label for the option that switches something off, e.g. a filter or a line of a chart"
},
"searchModeExact": "Exact",
"@searchModeExact": {

View File

@@ -1004,9 +1004,9 @@
"@filterNutriscore": {
"description": "Heading for the Nutri-Score slider in the ingredient search filter dialog"
},
"filterNutriscoreOff": "Desligado",
"@filterNutriscoreOff": {
"description": "Label for the first slider stop, which disables the Nutri-Score filter"
"off": "Desligado",
"@off": {
"description": "Generic label for the option that switches something off, e.g. a filter or a line of a chart"
},
"filterNutriscoreNoFilter": "Sem filtro",
"@filterNutriscoreNoFilter": {

View File

@@ -1090,9 +1090,9 @@
"@filterNutriscore": {
"description": "Heading for the Nutri-Score slider in the ingredient search filter dialog"
},
"filterNutriscoreOff": "Desligado",
"@filterNutriscoreOff": {
"description": "Label for the first slider stop, which disables the Nutri-Score filter"
"off": "Desligado",
"@off": {
"description": "Generic label for the option that switches something off, e.g. a filter or a line of a chart"
},
"filterNutriscoreNoFilter": "Sem filtro",
"@filterNutriscoreNoFilter": {

View File

@@ -1350,9 +1350,9 @@
"@filterNutriscore": {
"description": "Heading for the Nutri-Score slider in the ingredient search filter dialog"
},
"filterNutriscoreOff": "Izklopljeno",
"@filterNutriscoreOff": {
"description": "Label for the first slider stop, which disables the Nutri-Score filter"
"off": "Izklopljeno",
"@off": {
"description": "Generic label for the option that switches something off, e.g. a filter or a line of a chart"
},
"filterNutriscoreNoFilter": "Brez filtra",
"@filterNutriscoreNoFilter": {

View File

@@ -991,9 +991,9 @@
"@filterNutriscore": {
"description": "Heading for the Nutri-Score slider in the ingredient search filter dialog"
},
"filterNutriscoreOff": "關閉",
"@filterNutriscoreOff": {
"description": "Label for the first slider stop, which disables the Nutri-Score filter"
"off": "關閉",
"@off": {
"description": "Generic label for the option that switches something off, e.g. a filter or a line of a chart"
},
"filterNutriscoreNoFilter": "不套用篩選",
"@filterNutriscoreNoFilter": {

View File

@@ -112,6 +112,18 @@ void main() {
expect(withConfig({'average_window': 'a fortnight'}).chartSettings.averageWindow, 7);
});
test('a line the user turned off has no period and no window', () {
expect(withConfig({'trend': 'none'}).chartSettings.trend.emaPeriod, isNull);
expect(withConfig({'average_window': 'none'}).chartSettings.averageWindow, isNull);
});
test('turning one line off leaves the other alone', () {
final settings = withConfig({'trend': 'none', 'average_window': 14}).chartSettings;
expect(settings.trend, TrendCharacter.none);
expect(settings.averageWindow, 14);
});
test('a setting is changed without dropping the keys of another client', () {
final category = withConfig({'goal_line': 75}).withChartSetting('trend', 'reactive');
@@ -119,8 +131,8 @@ void main() {
});
test('the trend character maps to the EMA period the chart uses', () {
expect(TrendCharacter.reactive.emaPeriod, lessThan(TrendCharacter.balanced.emaPeriod));
expect(TrendCharacter.sluggish.emaPeriod, greaterThan(TrendCharacter.balanced.emaPeriod));
expect(TrendCharacter.reactive.emaPeriod, lessThan(TrendCharacter.balanced.emaPeriod!));
expect(TrendCharacter.sluggish.emaPeriod, greaterThan(TrendCharacter.balanced.emaPeriod!));
});
});

View File

@@ -158,7 +158,7 @@ void main() {
await tester.pumpAndSettle();
expect(find.byType(DropdownButtonFormField<TrendCharacter>), findsOneWidget);
expect(find.byType(DropdownButtonFormField<int>), findsOneWidget);
expect(find.byType(DropdownButtonFormField<Object>), findsOneWidget);
});
testWidgets('a summed type has no line to configure', (tester) async {
@@ -171,7 +171,7 @@ void main() {
await tester.pumpAndSettle();
expect(find.byType(DropdownButtonFormField<TrendCharacter>), findsNothing);
expect(find.byType(DropdownButtonFormField<int>), findsNothing);
expect(find.byType(DropdownButtonFormField<Object>), findsNothing);
});
testWidgets('the line settings are disabled for a chart without a line', (tester) async {
@@ -193,14 +193,40 @@ void main() {
);
expect(
tester
.widget<DropdownButtonFormField<int>>(
find.byType(DropdownButtonFormField<int>),
.widget<DropdownButtonFormField<Object>>(
find.byType(DropdownButtonFormField<Object>),
)
.onChanged,
isNull,
);
});
testWidgets('both lines of the chart can be turned off', (tester) async {
final category = getMeasurementCategories()[1].copyWith(chartType: ChartType.auto);
stubMeasurementReads(mockRepo, [category]);
await tester.pumpWidget(wrap(MeasurementCategoryForm(category)));
await tester.pumpAndSettle();
await tester.tap(find.byType(DropdownButtonFormField<TrendCharacter>));
await tester.pumpAndSettle();
await tester.tap(find.text('Off').last);
await tester.pumpAndSettle();
await tester.tap(find.byType(DropdownButtonFormField<Object>));
await tester.pumpAndSettle();
await tester.tap(find.text('Off').last);
await tester.pumpAndSettle();
await tester.tap(find.byType(ElevatedButton));
await tester.pumpAndSettle();
final saved =
verify(mockRepo.updateLocalDriftCategory(captureAny)).captured.single
as MeasurementCategory;
expect(saved.chartConfig, {'trend': 'none', 'average_window': 'none'});
});
testWidgets('the line settings are enabled while the chart is a line', (tester) async {
final category = getMeasurementCategories()[1].copyWith(chartType: ChartType.auto);
stubMeasurementReads(mockRepo, [category]);