Skip to content

Add rolling-average trend line to insulin statistics charts#33

Open
Sjoerd-Bo3 wants to merge 1 commit into
devfrom
claude/insulin-charts-rolling-average-bbc2sb
Open

Add rolling-average trend line to insulin statistics charts#33
Sjoerd-Bo3 wants to merge 1 commit into
devfrom
claude/insulin-charts-rolling-average-bbc2sb

Conversation

@Sjoerd-Bo3

Copy link
Copy Markdown
Owner

Summary

Adds a rolling-average (moving average) trend line overlaid on the insulin bar charts in Statistics → Insulin, so longer-term trends are easy to read on top of the noisy per-bar values — similar to the trend line in the attached reference example.

The dashed line is added to both charts in the Insulin tab:

  • Total Daily Dose — rolling average of the daily/hourly TDD.
  • Bolus Insulin — rolling average of the total bolus (manual + SMB + external), tracking the top of the stacked bars.

What changed

  • StatChartUtils: new reusable helpers
    • rollingAverage(for:date:value:window:centerOffset:) — a centered moving average with clamped partial windows at the edges, so the line spans the full visible range and isn't shifted relative to the bars.
    • rollingAverageWindow(for:) — interval-aware window (3 hours for Day; 3 days for Week; 7 days for Month/3M).
    • barCenterOffset(for:) — re-centers the line on bar centers (bars are binned to the start of the hour/day).
    • dashedLegendItem(label:color:) + a small HorizontalLine shape for a dashed legend symbol.
    • Shared rollingAverageStrokeStyle.
  • TotalDailyDoseChart: overlay the dashed LineMark (catmullRom interpolation) and add a legend (the chart previously had none).
  • BolusStatsView: overlay the dashed LineMark over the total bolus and add a "Rolling average" entry to the existing legend.

The line uses Color.primary, so it stays high-contrast in both light and dark mode.

Notes / design choices

  • Centered vs trailing average: centered keeps the line aligned with the bars it summarizes (no lag), at the cost of the last point not being a "pure" trailing average — appropriate for a retrospective statistics view.
  • Window sizes are heuristic; happy to tune them.
  • No data-layer changes — the average is computed in the view from the already-fetched stats arrays.

Does this make sense for the other Statistics tabs?

  • Meals (Macro Nutrients) — yes, this is the most natural next candidate; carb/macro intake is bursty day-to-day and a trend line reads well. Trivial to add with the same helper (rolling average of carbs, or per-macro).
  • Glucose — the Glucose tab already shows trend-style summaries (percentiles, TIR, distributions) rather than a single per-day bar series, so a rolling-average line doesn't map cleanly onto those charts. (A daily-average-glucose line would be a separate, larger feature.)
  • Looping — the loop chart is a horizontal share/percentage bar (success vs glucose count) without a time series, so a rolling average doesn't apply.

If you'd like, I can extend the trend line to the Meals chart in this same PR.

Test plan

  • Build and open Statistics → Insulin.
  • Verify the dashed trend line renders over both the Total Daily Dose and Bolus Insulin charts across D / W / M / 3M.
  • Confirm the line aligns with bar centers and the legend shows "Rolling average".
  • Confirm light/dark mode contrast.

🤖 Generated with Claude Code


Generated by Claude Code

Overlay a centered rolling-average (moving average) line on the Total
Daily Dose and Bolus Insulin bar charts in the Statistics > Insulin tab,
making longer-term trends easier to read on top of the noisy per-bar
values.

- Add reusable rolling-average helpers to StatChartUtils: a centered
  moving-average computation with clamped edge windows, interval-aware
  window sizing, bar-center alignment offset, and a dashed legend item.
- Overlay the dashed trend line on TotalDailyDoseChart and BolusStatsView
  (bolus line tracks the total of manual + SMB + external).
- Add a legend entry so the dashed line is self-explanatory.
@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 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.
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