-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathChartView.swift
More file actions
185 lines (170 loc) · 7.75 KB
/
ChartView.swift
File metadata and controls
185 lines (170 loc) · 7.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
// ChartValues.swift
// Loop Widget Extension
//
// Created by Bastiaan Verhaar on 25/06/2024.
// Copyright © 2024 LoopKit Authors. All rights reserved.
//
import Foundation
import SwiftUI
import Charts
@available(iOS 16.2, *)
struct ChartView: View {
private let glucoseSampleData: [ChartValues]
private let predicatedData: [ChartValues]
private let glucoseRanges: [GlucoseRangeValue]
private let preset: Preset?
private let yAxisMarks: [Double]
private let colorGradient: LinearGradient
init(glucoseSamples: [GlucoseSampleAttributes], predicatedGlucose: [Double], predicatedStartDate: Date?, predicatedInterval: TimeInterval?, useLimits: Bool, lowerLimit: Double, upperLimit: Double, glucoseRanges: [GlucoseRangeValue], preset: Preset?, yAxisMarks: [Double]) {
self.glucoseSampleData = ChartValues.convert(data: glucoseSamples, useLimits: useLimits, lowerLimit: lowerLimit, upperLimit: upperLimit)
self.predicatedData = ChartValues.convert(
data: predicatedGlucose,
startDate: predicatedStartDate ?? Date.now,
interval: predicatedInterval ?? .minutes(5),
useLimits: useLimits,
lowerLimit: lowerLimit,
upperLimit: upperLimit
)
self.colorGradient = ChartView.getGradient(useLimits: useLimits, lowerLimit: lowerLimit, upperLimit: upperLimit, highestValue: yAxisMarks.max() ?? 1)
self.preset = preset
self.glucoseRanges = glucoseRanges
self.yAxisMarks = yAxisMarks
}
init(glucoseSamples: [GlucoseSampleAttributes], useLimits: Bool, lowerLimit: Double, upperLimit: Double, glucoseRanges: [GlucoseRangeValue], preset: Preset?, yAxisMarks: [Double]) {
self.glucoseSampleData = ChartValues.convert(data: glucoseSamples, useLimits: useLimits, lowerLimit: lowerLimit, upperLimit: upperLimit)
self.predicatedData = []
self.preset = preset
self.glucoseRanges = glucoseRanges
self.yAxisMarks = yAxisMarks
self.colorGradient = ChartView.getGradient(useLimits: useLimits, lowerLimit: lowerLimit, upperLimit: upperLimit, highestValue: yAxisMarks.max() ?? 1)
}
private static func getGradient(useLimits: Bool, lowerLimit: Double, upperLimit: Double, highestValue: Double) -> LinearGradient {
var stops: [Gradient.Stop] = [Gradient.Stop(color: Color("glucose"), location: 0)]
if useLimits {
let lowerStop = lowerLimit / highestValue
let upperStop = upperLimit / highestValue
stops = [
Gradient.Stop(color: .red, location: 0),
Gradient.Stop(color: .red, location: lowerStop - 0.01),
Gradient.Stop(color: .green, location: lowerStop),
Gradient.Stop(color: .green, location: upperStop),
Gradient.Stop(color: .orange, location: upperStop + 0.01),
Gradient.Stop(color: .orange, location: 600), // Just use the mg/dl limit for the most upper value
]
}
return LinearGradient(
gradient: Gradient(stops: stops),
startPoint: .bottom,
endPoint: .top
)
}
var body: some View {
ZStack(alignment: Alignment(horizontal: .trailing, vertical: .top)){
Chart {
if let preset = self.preset, predicatedData.count > 0, preset.endDate > Date.now.addingTimeInterval(.hours(-6)) {
RectangleMark(
xStart: .value("Start", preset.startDate),
xEnd: .value("End", preset.endDate),
yStart: .value("Preset override", preset.minValue),
yEnd: .value("Preset override", preset.maxValue)
)
.foregroundStyle(.primary)
.opacity(0.6)
}
ForEach(glucoseRanges) { item in
RectangleMark(
xStart: .value("Start", item.startDate),
xEnd: .value("End", item.endDate),
yStart: .value("Glucose range", item.minValue),
yEnd: .value("Glucose range", item.maxValue)
)
.foregroundStyle(.primary)
.opacity(0.3)
}
ForEach(glucoseSampleData) { item in
PointMark (x: .value("Date", item.x),
y: .value("Glucose level", item.y)
)
.symbolSize(10)
.foregroundStyle(by: .value("Color", item.color))
}
ForEach(predicatedData) { item in
LineMark (x: .value("Date", item.x),
y: .value("Glucose level", item.y)
)
.lineStyle(StrokeStyle(lineWidth: 2, dash: [6, 5]))
.foregroundStyle(colorGradient)
}
}
.chartForegroundStyleScale([
"Good": .green,
"High": .orange,
"Low": .red,
"Default": Color("glucose")
])
.chartPlotStyle { plotContent in
plotContent.background(.cyan.opacity(0.15))
}
.chartLegend(.hidden)
.chartYScale(domain: [yAxisMarks.first ?? 0, yAxisMarks.last ?? 0])
.chartYAxis {
AxisMarks(values: yAxisMarks)
}
.chartYAxis {
AxisMarks(position: .leading) { _ in
AxisValueLabel().foregroundStyle(Color.primary)
AxisGridLine(stroke: .init(lineWidth: 0.1, dash: [2, 3]))
.foregroundStyle(Color.primary)
}
}
.chartXAxis {
AxisMarks(position: .automatic, values: .stride(by: .hour)) { _ in
AxisValueLabel(format: .dateTime.hour(.twoDigits(amPM: .narrow)), anchor: .top)
.foregroundStyle(Color.primary)
AxisGridLine(stroke: .init(lineWidth: 0.1, dash: [2, 3]))
.foregroundStyle(Color.primary)
}
}
if let preset = self.preset, preset.endDate > Date.now {
Text(preset.title)
.font(.footnote)
.padding(.trailing, 5)
.padding(.top, 2)
}
}
}
}
struct ChartValues: Identifiable {
public let id: UUID
public let x: Date
public let y: Double
public let color: String
init(x: Date, y: Double, color: String) {
self.id = UUID()
self.x = x
self.y = y
self.color = color
}
static func convert(data: [Double], startDate: Date, interval: TimeInterval, useLimits: Bool, lowerLimit: Double, upperLimit: Double) -> [ChartValues] {
let twoHours = Date.now.addingTimeInterval(.hours(4))
return data.enumerated().filter { (index, item) in
return startDate.addingTimeInterval(interval * Double(index)) < twoHours
}.map { (index, item) in
return ChartValues(
x: startDate.addingTimeInterval(interval * Double(index)),
y: item,
color: "Default" // Color is handled by the gradient
)
}
}
static func convert(data: [GlucoseSampleAttributes], useLimits: Bool, lowerLimit: Double, upperLimit: Double) -> [ChartValues] {
return data.map { item in
return ChartValues(
x: item.x,
y: item.y,
color: !useLimits ? "Default" : item.y < lowerLimit ? "Low" : item.y > upperLimit ? "High" : "Good"
)
}
}
}