Yakov Karpov 5 лет назад
Родитель
Сommit
7114eaf50c

+ 4 - 4
FreeAPS.xcodeproj/project.pbxproj

@@ -1274,11 +1274,12 @@
 		6610F96E25FAD534004781D7 /* Charts */ = {
 			isa = PBXGroup;
 			children = (
-				6610F96F25FAD534004781D7 /* Sample Data */,
-				6610F97125FAD534004781D7 /* Models */,
-				6610F97B25FAD534004781D7 /* Extensions */,
 				6610F98125FAD535004781D7 /* Views */,
+				6610F98825FAD535004781D7 /* ChartsConfig.swift */,
+				6610F97125FAD534004781D7 /* Models */,
 				6610F98C25FAD535004781D7 /* Helpers */,
+				6610F97B25FAD534004781D7 /* Extensions */,
+				6610F96F25FAD534004781D7 /* Sample Data */,
 			);
 			path = Charts;
 			sourceTree = "<group>";
@@ -1324,7 +1325,6 @@
 			children = (
 				6610F98225FAD535004781D7 /* Charts */,
 				6610F98425FAD535004781D7 /* Components */,
-				6610F98825FAD535004781D7 /* ChartsConfig.swift */,
 				6610F98925FAD535004781D7 /* Points */,
 			);
 			path = Views;

FreeAPS/Sources/Charts/Views/ChartsConfig.swift → FreeAPS/Sources/Charts/ChartsConfig.swift


+ 30 - 17
FreeAPS/Sources/Charts/Views/Charts/PointChartView.swift

@@ -32,7 +32,7 @@ struct PointChartView<PointEntry: View>: View {
                     .position(x: point.xPosition, y: point.yPosition ?? 0)
             }
         }
-        .frame(width: 1000)
+        .frame(width: 100000)
     }
 }
 
@@ -50,16 +50,17 @@ private func getGlucosePoints(
         .compactMap { $0.date }
         .first ?? UInt64(Date().timeIntervalSince1970)
     
-    let _ = width / CGFloat(60 * 60 * showHours)
+    let pointSize: CGFloat = ChartsConfig.glucosePointSize / 2
 
     /// y = mx + b where m = scalingFactor, b = addendum, x = value, y = mapped value
-    let scalingFactor = Double(height) / Double(maxValue - minValue)
+    let scalingFactor = Double(height - pointSize * 2) / Double(maxValue - minValue)
     let addendum = scalingFactor * Double(maxValue)
-    let pointSize: CGFloat = ChartsConfig.glucosePointSize / 2
-    let hoursMultiplier: Double = 12
+    let hoursMultiplier: Double = 14
 
     return data.map { glucose in
-        let xPosition = (CGFloat(0) * width / CGFloat(Double(showHours) * hoursMultiplier)) + pointSize
+        let xPositionIndex = CGFloat(glucose.date - firstEntryTime) / CGFloat(300 * showHours)
+        
+        let xPosition = (xPositionIndex * width / CGFloat(Double(showHours) * hoursMultiplier)) + pointSize
         
         guard let value = glucose.sgv else {
             return GlucosePointData(
@@ -69,26 +70,38 @@ private func getGlucosePoints(
         return GlucosePointData(
             value: value,
             xPosition: xPosition,
-            yPosition: CGFloat(-scalingFactor * Double(value) + addendum)
+            yPosition: CGFloat(-scalingFactor * Double(value) + addendum) + pointSize
         )
     }
 }
 
 struct PointChartView_Previews: PreviewProvider {
     
-    static let data = Array(SampleData.sampleData.prefix(10))
+    static let sampleData = Array(SampleData.sampleData)
+    
+    static let testingData = [
+        BloodGlucose(sgv: 3, direction: nil, date: 1615179600, dateString: Date(), filtered: nil, noise: nil, glucose: nil),
+        BloodGlucose(sgv: 4, direction: nil, date: 1615179900, dateString: Date(), filtered: nil, noise: nil, glucose: nil),
+        BloodGlucose(sgv: 5, direction: nil, date: 1615180200, dateString: Date(), filtered: nil, noise: nil, glucose: nil),
+        BloodGlucose(sgv: 6, direction: nil, date: 1615180200, dateString: Date(), filtered: nil, noise: nil, glucose: nil),
+        BloodGlucose(sgv: 7, direction: nil, date: 1615180800, dateString: Date(), filtered: nil, noise: nil, glucose: nil),
+        BloodGlucose(sgv: 8, direction: nil, date: 1615181300, dateString: Date(), filtered: nil, noise: nil, glucose: nil),
+    ]
 
     static var previews: some View {
-        ScrollView(.horizontal) {
-            PointChartView(
-                width: 500,
-                showHours: 1,
-                glucoseData: data
-            ) { value in
-                GlucosePointView(value: value)
+        Group {
+            ScrollView(.horizontal) {
+                PointChartView(
+                    width: 500,
+                    showHours: 1,
+                    glucoseData: testingData
+                ) { value in
+                    GlucosePointView(value: value)
+                }
             }
-            .background(Color.gray)
+            .padding(.vertical)
+            
+            .preferredColorScheme(/*@START_MENU_TOKEN@*/ .dark/*@END_MENU_TOKEN@*/)
         }
-        .preferredColorScheme(/*@START_MENU_TOKEN@*/ .dark/*@END_MENU_TOKEN@*/)
     }
 }