GlucosePercentileChart.swift 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import Charts
  2. import SwiftUI
  3. struct GlucosePercentileChart: View {
  4. let glucose: [GlucoseStored]
  5. let highLimit: Decimal
  6. let lowLimit: Decimal
  7. let units: GlucoseUnits
  8. let hourlyStats: [HourlyStats]
  9. let isToday: Bool
  10. @State private var selection: Date? = nil
  11. private var selectedStats: HourlyStats? {
  12. guard let selection = selection else { return nil }
  13. // Don't show stats for future times if viewing today
  14. if isToday && selection > Date() {
  15. return nil
  16. }
  17. let calendar = Calendar.current
  18. let hour = calendar.component(.hour, from: selection)
  19. return hourlyStats.first { Int($0.hour) == hour }
  20. }
  21. var body: some View {
  22. VStack(alignment: .leading, spacing: 8) {
  23. Text("Ambulatory Glucose Profile (AGP)")
  24. .font(.headline)
  25. Chart {
  26. // TODO: ensure data is still correct
  27. // TODO: ensure area marks and line mark take color of respective range
  28. // Statistical view for longer periods
  29. ForEach(hourlyStats, id: \.hour) { stats in
  30. // 10-90 percentile area
  31. AreaMark(
  32. x: .value("Hour", Calendar.current.dateForChartHour(stats.hour)),
  33. yStart: .value("10th Percentile", stats.percentile10),
  34. yEnd: .value("90th Percentile", stats.percentile90),
  35. series: .value("10-90", "10-90")
  36. )
  37. .foregroundStyle(.blue.opacity(stats.median > 0 ? 0.2 : 0))
  38. // 25-75 percentile area
  39. AreaMark(
  40. x: .value("Hour", Calendar.current.dateForChartHour(stats.hour)),
  41. yStart: .value("25th Percentile", stats.percentile25),
  42. yEnd: .value("75th Percentile", stats.percentile75),
  43. series: .value("25-75", "25-75")
  44. )
  45. .foregroundStyle(.blue.opacity(stats.median > 0 ? 0.3 : 0))
  46. // Median line
  47. if stats.median > 0 {
  48. LineMark(
  49. x: .value("Hour", Calendar.current.dateForChartHour(stats.hour)),
  50. y: .value("Median", stats.median),
  51. series: .value("Median", "Median")
  52. )
  53. .lineStyle(StrokeStyle(lineWidth: 2))
  54. .foregroundStyle(.blue)
  55. }
  56. }
  57. // High/Low limit lines
  58. RuleMark(y: .value("High Limit", Double(highLimit)))
  59. .lineStyle(StrokeStyle(lineWidth: 1, dash: [5, 5]))
  60. .foregroundStyle(.orange)
  61. RuleMark(y: .value("Low Limit", Double(lowLimit)))
  62. .lineStyle(StrokeStyle(lineWidth: 1, dash: [5, 5]))
  63. .foregroundStyle(.red)
  64. if let selectedStats, let selection {
  65. RuleMark(x: .value("Selection", selection))
  66. .foregroundStyle(.secondary.opacity(0.5))
  67. .annotation(
  68. position: .top,
  69. spacing: 0,
  70. overflowResolution: .init(x: .fit, y: .disabled)
  71. ) {
  72. AGPSelectionPopover(
  73. stats: selectedStats,
  74. time: selection,
  75. units: units
  76. )
  77. }
  78. }
  79. }
  80. .chartYAxis {
  81. AxisMarks(position: .trailing) { value in
  82. if let glucose = value.as(Double.self) {
  83. AxisValueLabel {
  84. Text(glucose.formatted(.number.precision(.fractionLength(0))))
  85. .font(.footnote)
  86. }
  87. AxisGridLine()
  88. }
  89. }
  90. }
  91. .chartYAxisLabel(alignment: .trailing) {
  92. Text("\(units.rawValue)")
  93. .foregroundStyle(.primary)
  94. .font(.footnote)
  95. .padding(.vertical, 3)
  96. }
  97. .chartXAxis {
  98. AxisMarks(values: .stride(by: .hour, count: 3)) { _ in
  99. AxisValueLabel(format: .dateTime.hour(.defaultDigits(amPM: .narrow)), anchor: .top)
  100. .font(.footnote)
  101. AxisGridLine()
  102. }
  103. }
  104. .chartXSelection(value: $selection.animation(.easeInOut))
  105. .frame(height: 200)
  106. legend
  107. }
  108. }
  109. private var legend: some View {
  110. HStack(spacing: 20) {
  111. VStack {
  112. // 10-90 Percentile
  113. HStack(spacing: 8) {
  114. Rectangle()
  115. .frame(width: 20, height: 8)
  116. .foregroundStyle(.blue.opacity(0.2))
  117. Text("10% - 90%")
  118. .font(.caption)
  119. .foregroundStyle(.secondary)
  120. }
  121. // 25-75 Percentile
  122. HStack(spacing: 8) {
  123. Rectangle()
  124. .frame(width: 20, height: 8)
  125. .foregroundStyle(.blue.opacity(0.3))
  126. Text("25% - 75%")
  127. .font(.caption)
  128. .foregroundStyle(.secondary)
  129. }
  130. }
  131. // Median
  132. HStack(spacing: 8) {
  133. Rectangle()
  134. .frame(width: 20, height: 2)
  135. .foregroundStyle(.blue)
  136. Text("Median")
  137. .font(.caption)
  138. .foregroundStyle(.secondary)
  139. }
  140. VStack {
  141. // High Limit
  142. HStack(spacing: 8) {
  143. Rectangle()
  144. .frame(width: 20, height: 1)
  145. .foregroundStyle(.orange)
  146. Text("High Limit")
  147. .font(.caption)
  148. .foregroundStyle(.secondary)
  149. }
  150. // Low Limit
  151. HStack(spacing: 8) {
  152. Rectangle()
  153. .frame(width: 20, height: 1)
  154. .foregroundStyle(.red)
  155. Text("Low Limit")
  156. .font(.caption)
  157. .foregroundStyle(.secondary)
  158. }
  159. }
  160. }
  161. .padding(.horizontal)
  162. }
  163. }
  164. struct AGPSelectionPopover: View {
  165. let stats: HourlyStats
  166. let time: Date
  167. let units: GlucoseUnits
  168. private var timeText: String {
  169. if let hour = Calendar.current.dateComponents([.hour], from: time).hour {
  170. return "\(hour):00-\(hour + 1):00"
  171. } else {
  172. return time.formatted(.dateTime.hour().minute())
  173. }
  174. }
  175. var body: some View {
  176. VStack(alignment: .leading, spacing: 4) {
  177. HStack {
  178. Image(systemName: "clock")
  179. Text(timeText)
  180. .fontWeight(.bold)
  181. }
  182. .font(.subheadline)
  183. Grid(alignment: .leading, horizontalSpacing: 8) {
  184. GridRow {
  185. Text("90%:")
  186. Text(stats.percentile90.formatted(.number))
  187. Text(units.rawValue)
  188. .foregroundStyle(.secondary)
  189. }
  190. GridRow {
  191. Text("75%:")
  192. Text(stats.percentile75.formatted(.number))
  193. Text(units.rawValue)
  194. .foregroundStyle(.secondary)
  195. }
  196. GridRow {
  197. Text("Median:")
  198. Text(stats.median.formatted(.number))
  199. Text(units.rawValue)
  200. .foregroundStyle(.secondary)
  201. }
  202. GridRow {
  203. Text("25%:")
  204. Text(stats.percentile25.formatted(.number))
  205. Text(units.rawValue)
  206. .foregroundStyle(.secondary)
  207. }
  208. GridRow {
  209. Text("10%:")
  210. Text(stats.percentile10.formatted(.number))
  211. Text(units.rawValue)
  212. .foregroundStyle(.secondary)
  213. }
  214. }
  215. .font(.headline.bold())
  216. }
  217. .foregroundStyle(.white)
  218. .padding(20)
  219. .background {
  220. RoundedRectangle(cornerRadius: 10)
  221. .fill(Color.blue.gradient)
  222. }
  223. }
  224. }
  225. private extension Calendar {
  226. func startOfHour(for date: Date) -> Date {
  227. let components = dateComponents([.year, .month, .day, .hour], from: date)
  228. return self.date(from: components) ?? date
  229. }
  230. }