Skip to content

Add rolling-average trend line to meal statistics chart#34

Open
Sjoerd-Bo3 wants to merge 5 commits into
claude/insulin-charts-rolling-average-bbc2sbfrom
claude/meals-chart-rolling-average
Open

Add rolling-average trend line to meal statistics chart#34
Sjoerd-Bo3 wants to merge 5 commits into
claude/insulin-charts-rolling-average-bbc2sbfrom
claude/meals-chart-rolling-average

Conversation

@Sjoerd-Bo3

Copy link
Copy Markdown
Owner

Summary

Extends the rolling-average trend line to the Macro Nutrients chart in Statistics → Meals, matching the insulin charts.

The dashed line tracks total carbs, or carbs + fat + protein when FPU conversion is enabled (so it stays consistent with the stacked bars actually shown), plus a "Rolling average" legend entry.

⚠️ Stacked on #33

This PR is stacked on top of #33 (claude/insulin-charts-rolling-average-bbc2sb) because it reuses the StatChartUtils rolling-average helpers introduced there. Its base branch is set to that branch so the diff shows only the Meals change.

Merge #33 first, then this PR (GitHub will auto-retarget it to dev). Alternatively, if you'd prefer this as a single combined PR with #33, let me know and I'll consolidate.

What changed

  • MealStatsView: overlay the dashed LineMark (catmullRom interpolation, Color.primary) over the macro bars, and add a "Rolling average" entry to the legend.

No data-layer or helper changes — it relies entirely on the helpers added in #33.

Test plan

  • Build and open Statistics → Meals.
  • Verify the dashed trend line renders over the Macro Nutrients chart across D / W / M / 3M.
  • Toggle FPU conversion off/on and confirm the line tracks carbs-only vs. the full stacked total accordingly.
  • Confirm the legend shows "Rolling average" and light/dark contrast is fine.

🤖 Generated with Claude Code


Generated by Claude Code

claude added 4 commits June 16, 2026 10:31
Overlay the same centered rolling-average trend line on the Macro
Nutrients bar chart in Statistics > Meals, reusing the helpers added for
the insulin charts. The line tracks total carbs, or carbs + fat +
protein when FPU conversion is enabled, and a Rolling average legend
entry is added.
Add a debug-only slider on the Statistics insulin and meal tabs to adjust
the rolling-average window live, so the smoothing can be tuned on
TestFlight builds without rebuilding.

- Persist the override via @AppStorage under a shared key in StatChartUtils
  (0 = automatic per-interval default, 1 = no smoothing, higher = smoother).
- Add an override-aware rollingAverageWindow(for:override:) helper and route
  the TDD, bolus, and meal charts through it.
- Show a labelled debug card with the slider on the insulin and meal tabs.

Note: the control is intentionally always visible (there is no UI to toggle
the app's developer options) and should be gated or removed before merging
to the release branch.
Address correctness and stability issues with the rolling-average trend line:

- Time-windowed average: window is now defined in calendar time (not array
  index), so gaps (days with no bolus/meal) no longer distort the result.
  Previously it averaged data points, over-counting active days.
- Switch interpolation catmullRom -> monotone to stop the line overshooting
  past the data into misleading bumps.
- Add debug toggles (Statistics insulin/meal tabs) to compare live on
  TestFlight: rolling median vs mean (outlier robustness), counting empty
  days as zero (true per-day average), and a carry-over lead-in.
- Carry-over: persist a per-metric summary of the oldest retained days and
  use it to pad the leading edge so the average isn't one-sided at the
  90-day purge boundary. Refreshed whenever stats are recomputed; never
  drawn as a bar.

Note: the debug toggles are intentionally always visible for Build testing
and should be gated or removed before merging to the release branch.
The Stat module uses explicit Xcode file references (not a synchronized
group), so the standalone RollingAverageCarryOver.swift was never compiled,
causing 'no member updateMealCarryOver / tddCarryOverValue' archive errors.
Move the carry-over extension into StatStateModel.swift, which is already
referenced by the project.
@Sjoerd-Bo3
Sjoerd-Bo3 force-pushed the claude/insulin-charts-rolling-average-bbc2sb branch from d14e669 to 64aca90 Compare June 16, 2026 10:32
@Sjoerd-Bo3
Sjoerd-Bo3 force-pushed the claude/meals-chart-rolling-average branch from 912d79c to 06d5510 Compare June 16, 2026 10:32
Sjoerd-Bo3 pushed a commit that referenced this pull request Jun 16, 2026
Sync the Build branch up to upstream nightscout/Trio dev (0.8.2.1), while
preserving the fork-specific features and the rolling-average trend-line
work (insulin + meal charts, debug slider, gap-correct/robust averaging,
carry-over lead-in) already on Build.

Resolved one conflict in Localizable.xcstrings (kept the fork's extra
string keys). The Statistics feature files are identical to the rebased
PR branches #33 and #34.
- Selection popovers (TDD, bolus, meal) now also show the rolling-average
  (trend) value at the scrubbed point, matching the dashed line. Each
  chart computes its rolling-average points once (shared by the line and
  the popover), and RollingAveragePoint carries its original bar date so
  the value at the scrubbed bar can be looked up via
  StatChartUtils.rollingAverageValue(at:).
- Default the rolling-average window to 21 days for the week/month/3M
  charts (day view keeps 3 hours), computed over the full retained daily
  series for a smoother trend.
- Raise the debug smoothing-window slider max from 21 to 60.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants