From 11461b325f1984bc3d628442c4156a46e66dd3ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Bj=C3=B6rkert?= Date: Fri, 10 Apr 2026 10:20:23 +0200 Subject: [PATCH] Clamp BG graph values to the same HIGH/LOW range as the header text When a Nightscout reading exceeds maxDisplayGlucose (400 mg/dL), the main display correctly shows "HIGH" but the chart was plotting the raw value (e.g. 550) and autoscaling the y-axis up to 600. This makes the chart inconsistent with the header. Clamp the plotted y-value in updateBGGraph to [minDisplayGlucose, maxDisplayGlucose] so the chart line stays within the same range the display text represents. The pill tooltip still shows the raw reading so the exact value is accessible on tap. --- LoopFollow/Controllers/Graphs.swift | 10 +++++++--- LoopFollow/Helpers/Globals.swift | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/LoopFollow/Controllers/Graphs.swift b/LoopFollow/Controllers/Graphs.swift index fe50491c7..18137375f 100644 --- a/LoopFollow/Controllers/Graphs.swift +++ b/LoopFollow/Controllers/Graphs.swift @@ -801,10 +801,14 @@ extension MainViewController { topBG = Storage.shared.minBGScale.value for i in 0 ..< entries.count { - if Double(entries[i].sgv) > topBG - maxBGOffset { - topBG = Double(entries[i].sgv) + maxBGOffset + // Clamp the plotted y-value to the same bounds the header text uses + // (HIGH/LOW), so the graph stays consistent with the main display. + // The pill tooltip still shows the raw reading. + let plottedSgv = Double(min(max(entries[i].sgv, globalVariables.minDisplayGlucose), globalVariables.maxDisplayGlucose)) + if plottedSgv > topBG - maxBGOffset { + topBG = plottedSgv + maxBGOffset } - let value = ChartDataEntry(x: Double(entries[i].date), y: Double(entries[i].sgv), data: formatPillText(line1: Localizer.toDisplayUnits(String(entries[i].sgv)), time: entries[i].date)) + let value = ChartDataEntry(x: Double(entries[i].date), y: plottedSgv, data: formatPillText(line1: Localizer.toDisplayUnits(String(entries[i].sgv)), time: entries[i].date)) mainChart.append(value) smallChart.append(value) diff --git a/LoopFollow/Helpers/Globals.swift b/LoopFollow/Helpers/Globals.swift index 770ad7f70..37fcfab83 100644 --- a/LoopFollow/Helpers/Globals.swift +++ b/LoopFollow/Helpers/Globals.swift @@ -15,7 +15,7 @@ enum globalVariables { // Glucose display range (mg/dL) // Values at or below the min are shown as "LOW" on the main display; // values at or above the max are shown as "HIGH". Also used to clamp - // prediction values on the graph. + // BG readings and prediction values on the graph. static let minDisplayGlucose: Int = 39 static let maxDisplayGlucose: Int = 400 }