ChartsView.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import Charts
  2. import CoreData
  3. import SwiftDate
  4. import SwiftUI
  5. struct ChartsView: View {
  6. let highLimit: Decimal
  7. let lowLimit: Decimal
  8. let units: GlucoseUnits
  9. let overrideUnit: Bool
  10. let standing: Bool
  11. let glucose: [GlucoseStored]
  12. @State var headline: Color = .secondary
  13. private var conversionFactor: Decimal {
  14. units == .mmolL ? 0.0555 : 1
  15. }
  16. var body: some View {
  17. glucoseChart
  18. Rectangle().fill(.cyan.opacity(0.2)).frame(maxHeight: 3)
  19. if standing {
  20. VStack {
  21. tirChartLaying
  22. Rectangle().fill(.cyan.opacity(0.2)).frame(maxHeight: 3)
  23. groupedGlucoseStatsLaying
  24. }
  25. } else {
  26. HStack(spacing: 20) {
  27. tirChartStanding
  28. groupedGlucoseStatsStanding
  29. }.padding(.horizontal, 10)
  30. }
  31. }
  32. init(
  33. highLimit: Decimal,
  34. lowLimit: Decimal,
  35. units: GlucoseUnits,
  36. overrideUnit: Bool,
  37. standing: Bool,
  38. glucose: [GlucoseStored]
  39. ) {
  40. self.highLimit = highLimit
  41. self.lowLimit = lowLimit
  42. self.units = units
  43. self.overrideUnit = overrideUnit
  44. self.standing = standing
  45. self.glucose = glucose
  46. }
  47. var glucoseChart: some View {
  48. let low = lowLimit * conversionFactor
  49. let high = highLimit * conversionFactor
  50. let count = glucose.count
  51. // The symbol size when fewer readings are larger
  52. let size: CGFloat = count < 20 ? 50 : count < 50 ? 35 : count > 2000 ? 5 : 15
  53. return Chart {
  54. ForEach(glucose) { item in
  55. if item.glucose > Int(highLimit) {
  56. PointMark(
  57. x: .value("Time", item.date ?? Date(), unit: .second),
  58. y: .value("Value", Decimal(item.glucose) * conversionFactor)
  59. ).foregroundStyle(Color.orange.gradient).symbolSize(size).interpolationMethod(.cardinal)
  60. } else if item.glucose < Int(lowLimit) {
  61. PointMark(
  62. x: .value("Time", item.date ?? Date(), unit: .second),
  63. y: .value("Value", Decimal(item.glucose) * conversionFactor)
  64. ).foregroundStyle(Color.red.gradient).symbolSize(size).interpolationMethod(.cardinal)
  65. } else {
  66. PointMark(
  67. x: .value("Time", item.date ?? Date(), unit: .second),
  68. y: .value("Value", Decimal(item.glucose) * conversionFactor)
  69. ).foregroundStyle(Color.green.gradient).symbolSize(size).interpolationMethod(.cardinal)
  70. }
  71. }
  72. }
  73. .chartYAxis {
  74. AxisMarks(
  75. values: [
  76. 0,
  77. low,
  78. high,
  79. units == .mmolL ? 15 : 270
  80. ]
  81. )
  82. }
  83. }
  84. var tirChartLaying: some View {
  85. let fetched = tir()
  86. let low = lowLimit * conversionFactor
  87. let high = highLimit * conversionFactor
  88. let fraction = units == .mgdL ? 0 : 1
  89. let data: [ShapeModel] = [
  90. .init(
  91. type: NSLocalizedString(
  92. "Low",
  93. comment: ""
  94. ) + " (<\(low.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fraction)))))",
  95. percent: fetched[0].decimal
  96. ),
  97. .init(type: NSLocalizedString("In Range", comment: ""), percent: fetched[1].decimal),
  98. .init(
  99. type: NSLocalizedString(
  100. "High",
  101. comment: ""
  102. ) + " (>\(high.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fraction)))))",
  103. percent: fetched[2].decimal
  104. )
  105. ]
  106. return VStack {
  107. Chart(data) { shape in
  108. BarMark(
  109. x: .value("TIR", shape.percent)
  110. )
  111. .foregroundStyle(by: .value("Group", shape.type))
  112. .annotation(position: .top, alignment: .center) {
  113. Text(
  114. "\(shape.percent, format: .number.precision(.fractionLength(0 ... 1))) %"
  115. ).font(.footnote).foregroundColor(.secondary)
  116. }
  117. }
  118. .chartXAxis(.hidden)
  119. .chartForegroundStyleScale([
  120. NSLocalizedString(
  121. "Low",
  122. comment: ""
  123. ) + " (<\(low.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fraction)))))": .red,
  124. NSLocalizedString("In Range", comment: ""): .green,
  125. NSLocalizedString(
  126. "High",
  127. comment: ""
  128. ) + " (>\(high.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fraction)))))": .orange
  129. ]).frame(maxHeight: 25)
  130. }
  131. .frame(maxWidth: .infinity, alignment: .center) // Align the entire VStack to center
  132. .padding(.horizontal, 5)
  133. }
  134. var tirChartStanding: some View {
  135. let fetched = tir()
  136. let low = lowLimit * conversionFactor
  137. let high = highLimit * conversionFactor
  138. let fraction = units == .mmolL ? 1 : 0
  139. let data: [ShapeModel] = [
  140. .init(
  141. type: NSLocalizedString(
  142. "Low",
  143. comment: ""
  144. ) + " (< \(low.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fraction)))))",
  145. percent: fetched[0].decimal
  146. ),
  147. .init(
  148. type: "\(low.formatted(.number.precision(.fractionLength(fraction)))) - \(high.formatted(.number.precision(.fractionLength(fraction))))",
  149. percent: fetched[1].decimal
  150. ),
  151. .init(
  152. type: NSLocalizedString(
  153. "High",
  154. comment: ""
  155. ) + " (> \(high.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fraction)))))",
  156. percent: fetched[2].decimal
  157. )
  158. ]
  159. return Chart(data) { shape in
  160. BarMark(
  161. x: .value("Shape", shape.type),
  162. y: .value("Percentage", shape.percent)
  163. )
  164. .foregroundStyle(by: .value("Group", shape.type))
  165. .annotation(position: shape.percent > 19 ? .overlay : .automatic, alignment: .center) {
  166. Text(shape.percent == 0 ? "" : "\(shape.percent, format: .number.precision(.fractionLength(0 ... 1)))")
  167. }
  168. }
  169. .chartXAxis(.hidden)
  170. .chartYAxis {
  171. AxisMarks(
  172. format: Decimal.FormatStyle.Percent.percent.scale(1)
  173. )
  174. }
  175. .chartForegroundStyleScale([
  176. NSLocalizedString(
  177. "Low",
  178. comment: ""
  179. ) + " (< \(low.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fraction)))))": .red,
  180. "\(low.formatted(.number.precision(.fractionLength(fraction)))) - \(high.formatted(.number.precision(.fractionLength(fraction))))": .green,
  181. NSLocalizedString(
  182. "High",
  183. comment: ""
  184. ) + " (> \(high.formatted(.number.grouping(.never).rounded().precision(.fractionLength(fraction)))))": .orange
  185. ])
  186. }
  187. var groupedGlucoseStatsStanding: some View {
  188. VStack(alignment: .leading, spacing: 15) {
  189. let mapGlucose = glucose.compactMap({ each in each.glucose })
  190. if !mapGlucose.isEmpty {
  191. let mapGlucoseAcuteLow = mapGlucose.filter({ $0 < Int16(3.3 / 0.0555) })
  192. let mapGlucoseHigh = mapGlucose.filter({ $0 > Int16(11 / 0.0555) })
  193. let mapGlucoseNormal = mapGlucose.filter({ $0 > Int16(3.8 / 0.0555) && $0 < Int16(7.9 / 0.0555) })
  194. HStack {
  195. let value = 100.0 * Double(mapGlucoseHigh.count) / Double(mapGlucose.count)
  196. Text(units == .mmolL ? "> 11 " : "> 198 ")
  197. .frame(width: 45, alignment: .leading)
  198. .foregroundColor(.secondary)
  199. Text("\(value.formatted(.number.precision(.fractionLength(0 ... 1)))) %")
  200. .frame(width: 45, alignment: .trailing)
  201. .foregroundColor(.orange)
  202. }.font(.caption)
  203. HStack {
  204. let value = 100.0 * Double(mapGlucoseNormal.count) / Double(mapGlucose.count)
  205. Text(units == .mmolL ? "3.9-7.8" : "70-140")
  206. .frame(width: 45, alignment: .leading)
  207. .foregroundColor(.secondary)
  208. Text("\(value.formatted(.number.precision(.fractionLength(0 ... 1)))) %")
  209. .frame(width: 45, alignment: .trailing)
  210. .foregroundColor(.green)
  211. }.font(.caption)
  212. HStack {
  213. let value = 100.0 * Double(mapGlucoseAcuteLow.count) / Double(mapGlucose.count)
  214. Text(units == .mmolL ? "< 3.3 " : "< 59 ")
  215. .frame(width: 45, alignment: .leading)
  216. .foregroundColor(.secondary)
  217. Text("\(value.formatted(.number.precision(.fractionLength(0 ... 1)))) %")
  218. .frame(width: 45, alignment: .trailing)
  219. .foregroundColor(.red)
  220. }.font(.caption)
  221. }
  222. }
  223. }
  224. var groupedGlucoseStatsLaying: some View {
  225. HStack {
  226. let mapGlucose = glucose.compactMap({ each in each.glucose })
  227. if !mapGlucose.isEmpty {
  228. let mapGlucoseLow = mapGlucose.filter({ $0 < Int16(3.3 / 0.0555) })
  229. let mapGlucoseNormal = mapGlucose.filter({ $0 > Int16(3.8 / 0.0555) && $0 < Int16(7.9 / 0.0555) })
  230. let mapGlucoseAcuteHigh = mapGlucose.filter({ $0 > Int16(11 / 0.0555) })
  231. Spacer()
  232. HStack {
  233. let value = 100.0 * Double(mapGlucoseLow.count) / Double(mapGlucose.count)
  234. Text(units == .mmolL ? "< 3.3" : "< 59").font(.caption2).foregroundColor(.secondary)
  235. Text(value.formatted(.number.precision(.fractionLength(0 ... 1)))).font(.caption)
  236. .foregroundColor(value == 0 ? .green : .red)
  237. Text("%").font(.caption)
  238. }
  239. Spacer()
  240. HStack {
  241. let value = 100.0 * Double(mapGlucoseNormal.count) / Double(mapGlucose.count)
  242. Text(units == .mmolL ? "3.9-7.8" : "70-140").foregroundColor(.secondary)
  243. Text(value.formatted(.number.precision(.fractionLength(0 ... 1)))).foregroundColor(.green)
  244. Text("%").foregroundColor(.secondary)
  245. }.font(.caption)
  246. Spacer()
  247. HStack {
  248. let value = 100.0 * Double(mapGlucoseAcuteHigh.count) / Double(mapGlucose.count)
  249. Text(units == .mmolL ? "> 11.0" : "> 198").font(.caption).foregroundColor(.secondary)
  250. Text(value.formatted(.number.precision(.fractionLength(0 ... 1)))).font(.caption)
  251. .foregroundColor(value == 0 ? .green : .orange)
  252. Text("%").font(.caption)
  253. }
  254. Spacer()
  255. }
  256. }
  257. }
  258. private func tir() -> [(decimal: Decimal, string: String)] {
  259. let hypoLimit = Int(lowLimit)
  260. let hyperLimit = Int(highLimit)
  261. let justGlucoseArray = glucose.compactMap({ each in Int(each.glucose as Int16) })
  262. let totalReadings = justGlucoseArray.count
  263. let hyperArray = glucose.filter({ $0.glucose > hyperLimit })
  264. let hyperReadings = hyperArray.compactMap({ each in each.glucose as Int16 }).count
  265. let hyperPercentage = Double(hyperReadings) / Double(totalReadings) * 100
  266. let hypoArray = glucose.filter({ $0.glucose < hypoLimit })
  267. let hypoReadings = hypoArray.compactMap({ each in each.glucose as Int16 }).count
  268. let hypoPercentage = Double(hypoReadings) / Double(totalReadings) * 100
  269. let tir = 100 - (hypoPercentage + hyperPercentage)
  270. var array: [(decimal: Decimal, string: String)] = []
  271. array.append((decimal: Decimal(hypoPercentage), string: "Low"))
  272. array.append((decimal: Decimal(tir), string: "NormaL"))
  273. array.append((decimal: Decimal(hyperPercentage), string: "High"))
  274. return array
  275. }
  276. }