Просмотр исходного кода

Account for scaled IoB in chart axis

Sam King 1 год назад
Родитель
Сommit
8883d2b198
1 измененных файлов с 13 добавлено и 3 удалено
  1. 13 3
      Trio/Sources/Modules/Home/View/Chart/ChartElements/CobIobChart.swift

+ 13 - 3
Trio/Sources/Modules/Home/View/Chart/ChartElements/CobIobChart.swift

@@ -54,8 +54,10 @@ extension MainChartView {
     }
 
     func combinedYDomain() -> ClosedRange<Double> {
-        let minValue = min(state.minValueCobChart, state.minValueIobChart)
-        let maxValue = max(state.maxValueCobChart, state.maxValueIobChart)
+        let iobMin = scaleIobAmountForChart(state.minValueIobChart)
+        let iobMax = scaleIobAmountForChart(state.maxValueIobChart)
+        let minValue = min(state.minValueCobChart, iobMin)
+        let maxValue = max(state.maxValueCobChart, iobMax)
         return Double(minValue) ... Double(maxValue)
     }
 
@@ -79,6 +81,14 @@ extension MainChartView {
         .position(by: .value("Axis", axis))
     }
 
+    func scaleIobAmountForChart(_ rawAmount: Double) -> Double {
+        rawAmount > 0 ? rawAmount * 8 : rawAmount * 9
+    }
+
+    func scaleIobAmountForChart(_ rawAmount: Decimal) -> Decimal {
+        rawAmount > 0 ? rawAmount * 8 : rawAmount * 9
+    }
+
     func drawCOBIOBChart() -> some ChartContent {
         // Filter out duplicate entries by `deliverAt`,
         // We sometimes get two determinations when editing carbs, one without the entry-to-be-edited and then another one after editing the entry.
@@ -117,7 +127,7 @@ extension MainChartView {
             let rawAmount = item.iob?.doubleValue ?? 0
 
             // as iob and cob share the same y axis and cob is usually >> iob we need to weigh iob visually
-            let amountIOB: Double = rawAmount > 0 ? rawAmount * 8 : rawAmount * 9
+            let amountIOB: Double = scaleIobAmountForChart(rawAmount)
 
             AreaMark(x: .value("Time", date), y: .value("Amount", amountIOB))
                 .foregroundStyle(by: .value("Type", "IOB"))