@@ -68,15 +68,28 @@ public class PredictedGlucoseChart: GlucoseChart, ChartProviding {
6868 public private( set) var endDate : Date ?
6969
7070 private var predictedGlucoseSoftBounds : PredictedGlucoseBounds ?
71-
71+
72+ private let yAxisStepSizeMGDLOverride : Double ?
73+
74+ private var maxYAxisValue : ChartAxisValue ?
75+
76+ private var minYAxisValue : ChartAxisValue ?
77+
78+ private var maxYAxisSegmentCount : Double {
79+ // when a glucose value is below the predicted glucose minimum soft bound, allow for more y-axis segments
80+ return glucoseValueBelowSoftBoundsMinimum ( ) ? 5 : 4
81+ }
82+
7283 private func updateEndDate( _ date: Date ) {
7384 if endDate == nil || date > endDate! {
7485 self . endDate = date
7586 }
7687 }
7788
78- public init ( predictedGlucoseBounds: PredictedGlucoseBounds ? ) {
89+ public init ( predictedGlucoseBounds: PredictedGlucoseBounds ? = nil ,
90+ yAxisStepSizeMGDLOverride: Double ? = nil ) {
7991 self . predictedGlucoseSoftBounds = predictedGlucoseBounds
92+ self . yAxisStepSizeMGDLOverride = yAxisStepSizeMGDLOverride
8093 super. init ( )
8194 }
8295}
@@ -131,16 +144,19 @@ extension PredictedGlucoseChart {
131144 glucoseDisplayRangePoints
132145 ] . flatMap { $0 }
133146
134- let yAxisValues = ChartAxisValuesStaticGenerator . generateYAxisValuesWithChartPoints ( points,
147+ let yAxisValues = ChartAxisValuesStaticGenerator . generateYAxisValuesUsingLinearSegmentStep ( chartPoints : points,
135148 minSegmentCount: 2 ,
136- maxSegmentCount: 4 ,
137- multiple: glucoseUnit. chartableIncrement * 25 ,
149+ maxSegmentCount: maxYAxisSegmentCount ,
150+ multiple: glucoseUnit == . milligramsPerDeciliter ? ( yAxisStepSizeMGDLOverride ?? 25 ) : 1 ,
138151 axisValueGenerator: {
139152 ChartAxisValueDouble ( $0, labelSettings: axisLabelSettings)
140153 } ,
141154 addPaddingSegmentIfEdge: false
142155 )
143156
157+ minYAxisValue = yAxisValues. first
158+ maxYAxisValue = yAxisValues. last
159+
144160 let yAxisModel = ChartAxisModel ( axisValues: yAxisValues, lineColor: colors. axisLine, labelSpaceReservationMode: . fixed( labelsWidthY) )
145161
146162 let coordsSpace = ChartCoordsSpaceLeftBottomSingleAxis ( chartSettings: chartSettings, chartFrame: frame, xModel: xAxisModel, yModel: yAxisModel)
@@ -265,17 +281,31 @@ extension PredictedGlucoseChart {
265281
266282// MARK: - Clamping the predicted glucose values
267283extension PredictedGlucoseChart {
268- var chartedGlucoseValueMaximum : HKQuantity ? {
284+ var chartMaximumValue : HKQuantity ? {
269285 guard let glucosePointMaximum = glucosePoints. max ( by: { point1, point2 in point1. y. scalar < point2. y. scalar } ) else {
270286 return nil
271287 }
288+
289+ if let maxYAxisValue = maxYAxisValue,
290+ maxYAxisValue. scalar > glucosePointMaximum. y. scalar
291+ {
292+ return HKQuantity ( unit: glucoseUnit, doubleValue: maxYAxisValue. scalar)
293+ }
294+
272295 return HKQuantity ( unit: glucoseUnit, doubleValue: glucosePointMaximum. y. scalar)
273296 }
274-
275- var chartedGlucoseValueMinimum : HKQuantity ? {
297+
298+ var chartMinimumValue : HKQuantity ? {
276299 guard let glucosePointMinimum = glucosePoints. min ( by: { point1, point2 in point1. y. scalar < point2. y. scalar } ) else {
277300 return nil
278301 }
302+
303+ if let minYAxisValue = minYAxisValue,
304+ minYAxisValue. scalar < glucosePointMinimum. y. scalar
305+ {
306+ return HKQuantity ( unit: glucoseUnit, doubleValue: minYAxisValue. scalar)
307+ }
308+
279309 return HKQuantity ( unit: glucoseUnit, doubleValue: glucosePointMinimum. y. scalar)
280310 }
281311
@@ -284,9 +314,9 @@ extension PredictedGlucoseChart {
284314 return glucoseValues
285315 }
286316
287- let predictedGlucoseValueMaximum = chartedGlucoseValueMaximum != nil ? max ( predictedGlucoseBounds. maximum, chartedGlucoseValueMaximum !) : predictedGlucoseBounds. maximum
317+ let predictedGlucoseValueMaximum = chartMaximumValue != nil ? max ( predictedGlucoseBounds. maximum, chartMaximumValue !) : predictedGlucoseBounds. maximum
288318
289- let predictedGlucoseValueMinimum = chartedGlucoseValueMinimum != nil ? min ( predictedGlucoseBounds. minimum, chartedGlucoseValueMinimum !) : predictedGlucoseBounds. minimum
319+ let predictedGlucoseValueMinimum = chartMinimumValue != nil ? min ( predictedGlucoseBounds. minimum, chartMinimumValue !) : predictedGlucoseBounds. minimum
290320
291321 return glucoseValues. map {
292322 if $0. quantity > predictedGlucoseValueMaximum {
@@ -299,6 +329,24 @@ extension PredictedGlucoseChart {
299329 }
300330 }
301331
332+ var chartedGlucoseValueMinimum : HKQuantity ? {
333+ guard let glucosePointMinimum = glucosePoints. min ( by: { point1, point2 in point1. y. scalar < point2. y. scalar } ) else {
334+ return nil
335+ }
336+
337+ return HKQuantity ( unit: glucoseUnit, doubleValue: glucosePointMinimum. y. scalar)
338+ }
339+
340+ func glucoseValueBelowSoftBoundsMinimum( ) -> Bool {
341+ guard let predictedGlucoseSoftBounds = predictedGlucoseSoftBounds,
342+ let chartedGlucoseValueMinimum = chartedGlucoseValueMinimum else
343+ {
344+ return false
345+ }
346+
347+ return chartedGlucoseValueMinimum < predictedGlucoseSoftBounds. minimum
348+ }
349+
302350 public struct PredictedGlucoseBounds {
303351 var minimum : HKQuantity
304352 var maximum : HKQuantity
0 commit comments