فهرست منبع

Line mode for charts

Ivan Valkou 5 سال پیش
والد
کامیت
1699dd649b

+ 3 - 0
FreeAPS/Sources/Charts/Views/Charts/CombinedChartView.swift

@@ -5,6 +5,8 @@ struct CombinedChartView: View {
     let showHours: Int
     @Binding var glucoseData: [BloodGlucose]
     @Binding var predictionsData: [PredictionLineData]
+    let mode: PointChartViewMode
+
     var body: some View {
         let allValues = getAllValues()
         let minValue = allValues.min() ?? 40
@@ -15,6 +17,7 @@ struct CombinedChartView: View {
                 minValue: minValue,
                 maxValue: maxValue,
                 maxWidth: maxWidth,
+                mode: mode,
                 showHours: showHours,
                 glucoseData: $glucoseData
             ) { value in

+ 31 - 8
FreeAPS/Sources/Charts/Views/Charts/PointChartView.swift

@@ -1,9 +1,15 @@
 import SwiftUI
 
+enum PointChartViewMode {
+    case line
+    case dots
+}
+
 struct PointChartView<PointEntry: View>: View {
     let minValue: Int
     let maxValue: Int
     let maxWidth: CGFloat
+    let mode: PointChartViewMode
 
     let showHours: Int
     @Binding var glucoseData: [BloodGlucose]
@@ -24,14 +30,31 @@ struct PointChartView<PointEntry: View>: View {
         }
 
         return GeometryReader { geometry in
-            ForEach(
-                getGlucosePoints(
-                    height: geometry.size.height, firstEntryTime: firstEntryTime
-                ),
-                id: \.self
-            ) { point in
-                pointEntry(point.value)
-                    .position(x: point.xPosition, y: point.yPosition ?? 0)
+            switch mode {
+            case .line:
+                Path { path in
+                    let points = getGlucosePoints(
+                        height: geometry.size.height, firstEntryTime: firstEntryTime
+                    )
+
+                    for point in points {
+                        if point == points.first {
+                            path.move(to: CGPoint(x: point.xPosition, y: point.yPosition ?? 0))
+                        } else {
+                            path.addLine(to: CGPoint(x: point.xPosition, y: point.yPosition ?? 0))
+                        }
+                    }
+                }.stroke(Color.blue, lineWidth: 1)
+            case .dots:
+                ForEach(
+                    getGlucosePoints(
+                        height: geometry.size.height, firstEntryTime: firstEntryTime
+                    ),
+                    id: \.self
+                ) { point in
+                    pointEntry(point.value)
+                        .position(x: point.xPosition, y: point.yPosition ?? 0)
+                }
             }
         }
         .frame(width: width + pointSize)

+ 1 - 0
FreeAPS/Sources/Charts/Views/Charts/PredictionsChartView.swift

@@ -14,6 +14,7 @@ struct PredictionsChartView: View {
                     minValue: minValue,
                     maxValue: maxValue,
                     maxWidth: maxWidth,
+                    mode: .dots,
                     showHours: showHours,
                     glucoseData: $data[index].values
                 ) { value in

+ 5 - 3
FreeAPS/Sources/Modules/Home/View/HomeRootView.swift

@@ -12,7 +12,8 @@ extension Home {
                         maxWidth: geo.size.width,
                         showHours: showHours,
                         glucoseData: $viewModel.glucose,
-                        predictionsData: .constant([])
+                        predictionsData: .constant([]),
+                        mode: .dots
                     )
                 }
             }
@@ -27,13 +28,15 @@ extension Home {
                     maxWidth: geo.size.width,
                     showHours: 24,
                     glucoseData: $viewModel.glucose,
-                    predictionsData: .constant([])
+                    predictionsData: .constant([]),
+                    mode: .line
                 )
             }
             .frame(maxWidth: .infinity)
             .padding(.vertical)
             .background(Color(.systemGray6))
             .cornerRadius(10)
+            .drawingGroup()
         }
 
         var body: some View {
@@ -48,7 +51,6 @@ extension Home {
 
                         mainChart
                             .frame(height: geo.size.height * 0.6)
-
                             .padding(.horizontal)
 
                         previewChart