GlucosePercentileChart.swift 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. overflowResolution: .init(
  72. x: .fit(to: .chart),
  73. y: .fit(to: .chart)
  74. ) // for coherence with the other charts
  75. ) {
  76. AGPSelectionPopover(
  77. stats: selectedStats,
  78. time: selection,
  79. units: units
  80. )
  81. }
  82. }
  83. }
  84. .chartYAxis {
  85. AxisMarks(position: .leading)
  86. }
  87. .chartYAxisLabel(alignment: .leading) {
  88. Text("\(units.rawValue)")
  89. .foregroundStyle(.primary)
  90. .font(.caption)
  91. .padding(.vertical, 3)
  92. }
  93. .chartXAxis {
  94. AxisMarks(values: .stride(by: .hour, count: 3)) { _ in
  95. AxisValueLabel(format: .dateTime.hour(.defaultDigits(amPM: .narrow)), anchor: .top)
  96. AxisGridLine()
  97. }
  98. }
  99. .chartXSelection(value: $selection.animation(.easeInOut))
  100. .frame(height: 200)
  101. legend
  102. }
  103. }
  104. private var legend: some View {
  105. HStack(spacing: 20) {
  106. VStack {
  107. // 10-90 Percentile
  108. HStack(spacing: 8) {
  109. Rectangle()
  110. .frame(width: 20, height: 8)
  111. .foregroundStyle(.blue.opacity(0.2))
  112. Text("10% - 90%")
  113. .font(.caption)
  114. .foregroundStyle(.secondary)
  115. }
  116. // 25-75 Percentile
  117. HStack(spacing: 8) {
  118. Rectangle()
  119. .frame(width: 20, height: 8)
  120. .foregroundStyle(.blue.opacity(0.3))
  121. Text("25% - 75%")
  122. .font(.caption)
  123. .foregroundStyle(.secondary)
  124. }
  125. }
  126. // Median
  127. HStack(spacing: 8) {
  128. Rectangle()
  129. .frame(width: 20, height: 2)
  130. .foregroundStyle(.blue)
  131. Text("Median")
  132. .font(.caption)
  133. .foregroundStyle(.secondary)
  134. }
  135. VStack {
  136. // High Limit
  137. HStack(spacing: 8) {
  138. Rectangle()
  139. .frame(width: 20, height: 1)
  140. .foregroundStyle(.orange)
  141. Text("High Limit")
  142. .font(.caption)
  143. .foregroundStyle(.secondary)
  144. }
  145. // Low Limit
  146. HStack(spacing: 8) {
  147. Rectangle()
  148. .frame(width: 20, height: 1)
  149. .foregroundStyle(.red)
  150. Text("Low Limit")
  151. .font(.caption)
  152. .foregroundStyle(.secondary)
  153. }
  154. }
  155. }
  156. .padding(.horizontal)
  157. }
  158. }
  159. struct AGPSelectionPopover: View {
  160. let stats: HourlyStats
  161. let time: Date
  162. let units: GlucoseUnits
  163. var body: some View {
  164. VStack(alignment: .leading, spacing: 4) {
  165. HStack {
  166. Image(systemName: "clock")
  167. Text(time.formatted(.dateTime.hour().minute(.twoDigits)))
  168. .fontWeight(.bold)
  169. }
  170. .font(.subheadline)
  171. Grid(alignment: .leading, horizontalSpacing: 8) {
  172. GridRow {
  173. Text("90%:")
  174. Text(stats.percentile90.formatted(.number))
  175. Text(units.rawValue)
  176. .foregroundStyle(.secondary)
  177. }
  178. GridRow {
  179. Text("75%:")
  180. Text(stats.percentile75.formatted(.number))
  181. Text(units.rawValue)
  182. .foregroundStyle(.secondary)
  183. }
  184. GridRow {
  185. Text("Median:")
  186. Text(stats.median.formatted(.number))
  187. Text(units.rawValue)
  188. .foregroundStyle(.secondary)
  189. }
  190. GridRow {
  191. Text("25%:")
  192. Text(stats.percentile25.formatted(.number))
  193. Text(units.rawValue)
  194. .foregroundStyle(.secondary)
  195. }
  196. GridRow {
  197. Text("10%:")
  198. Text(stats.percentile10.formatted(.number))
  199. Text(units.rawValue)
  200. .foregroundStyle(.secondary)
  201. }
  202. }
  203. .font(.headline.bold())
  204. }
  205. .foregroundStyle(.white)
  206. .padding(20)
  207. .background {
  208. RoundedRectangle(cornerRadius: 10)
  209. .fill(Color.blue.gradient)
  210. }
  211. }
  212. }
  213. private extension Calendar {
  214. func startOfHour(for date: Date) -> Date {
  215. let components = dateComponents([.year, .month, .day, .hour], from: date)
  216. return self.date(from: components) ?? date
  217. }
  218. }