ChartsView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. tirChart
  22. Rectangle().fill(.cyan.opacity(0.2)).frame(maxHeight: 3)
  23. groupedGlucoseStatsLaying
  24. }
  25. } else {
  26. HStack(spacing: 20) {
  27. standingTIRchart
  28. groupedGlucose
  29. }
  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 tirChart: some View {
  85. let fetched = tir()
  86. let low = lowLimit * conversionFactor
  87. let high = highLimit * conversionFactor
  88. let data: [ShapeModel] = [
  89. .init(
  90. type: NSLocalizedString(
  91. "Low",
  92. comment: ""
  93. ) + " (<\(low.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))))",
  94. percent: fetched[0].decimal
  95. ),
  96. .init(type: NSLocalizedString("In Range", comment: ""), percent: fetched[1].decimal),
  97. .init(
  98. type: NSLocalizedString(
  99. "High",
  100. comment: ""
  101. ) + " (>\(high.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))))",
  102. percent: fetched[2].decimal
  103. )
  104. ]
  105. return Chart(data) { shape in
  106. BarMark(
  107. x: .value("TIR", shape.percent)
  108. )
  109. .foregroundStyle(by: .value("Group", shape.type))
  110. .annotation(position: .top, alignment: .center) {
  111. Text(
  112. "\(shape.percent, format: .number.precision(.fractionLength(0))) %"
  113. ).font(.footnote).foregroundColor(.secondary)
  114. }
  115. }
  116. .chartXAxis(.hidden)
  117. .chartForegroundStyleScale([
  118. NSLocalizedString(
  119. "Low",
  120. comment: ""
  121. ) + " (<\(low.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))))": .red,
  122. NSLocalizedString("In Range", comment: ""): .green,
  123. NSLocalizedString(
  124. "High",
  125. comment: ""
  126. ) + " (>\(high.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))))": .orange
  127. ]).frame(maxHeight: 25)
  128. }
  129. var standingTIRchart: some View {
  130. let fetched = tir()
  131. let low = lowLimit * conversionFactor
  132. let high = highLimit * conversionFactor
  133. let fraction = units == .mmolL ? 1 : 0
  134. let data: [ShapeModel] = [
  135. .init(
  136. type: NSLocalizedString(
  137. "Low",
  138. comment: ""
  139. ) + " (< \(low.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))))",
  140. percent: fetched[0].decimal
  141. ),
  142. .init(
  143. type: "\(low.formatted(.number.precision(.fractionLength(fraction)))) - \(high.formatted(.number.precision(.fractionLength(fraction))))",
  144. percent: fetched[1].decimal
  145. ),
  146. .init(
  147. type: NSLocalizedString(
  148. "High",
  149. comment: ""
  150. ) + " (> \(high.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))))",
  151. percent: fetched[2].decimal
  152. )
  153. ]
  154. return Chart(data) { shape in
  155. BarMark(
  156. x: .value("Shape", shape.type),
  157. y: .value("Percentage", shape.percent)
  158. )
  159. .foregroundStyle(by: .value("Group", shape.type))
  160. .annotation(position: shape.percent > 19 ? .overlay : .automatic, alignment: .center) {
  161. Text(shape.percent == 0 ? "" : "\(shape.percent, format: .number.precision(.fractionLength(0)))")
  162. }
  163. }
  164. .chartXAxis(.hidden)
  165. .chartYAxis {
  166. AxisMarks(
  167. format: Decimal.FormatStyle.Percent.percent.scale(1)
  168. )
  169. }
  170. .chartForegroundStyleScale([
  171. NSLocalizedString(
  172. "Low",
  173. comment: ""
  174. ) + " (< \(low.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))))": .red,
  175. "\(low.formatted(.number.precision(.fractionLength(fraction)))) - \(high.formatted(.number.precision(.fractionLength(fraction))))": .green,
  176. NSLocalizedString(
  177. "High",
  178. comment: ""
  179. ) + " (> \(high.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))))": .orange
  180. ])
  181. }
  182. var groupedGlucose: some View {
  183. VStack(alignment: .leading, spacing: 20) {
  184. let mapGlucose = glucose.compactMap({ each in each.glucose })
  185. if !mapGlucose.isEmpty {
  186. let mapGlucoseAcuteLow = mapGlucose.filter({ $0 < Int16(3.3 / 0.0555) })
  187. let mapGlucoseHigh = mapGlucose.filter({ $0 > Int16(11 / 0.0555) })
  188. let mapGlucoseNormal = mapGlucose.filter({ $0 > Int16(3.8 / 0.0555) && $0 < Int16(7.9 / 0.0555) })
  189. HStack {
  190. let value = Double(mapGlucoseHigh.count * 100 / mapGlucose.count)
  191. Text(units == .mmolL ? "> 11 " : "> 198 ").foregroundColor(.secondary)
  192. Text(value.formatted()).foregroundColor(.orange)
  193. Text("%").foregroundColor(.secondary)
  194. }.font(.caption)
  195. HStack {
  196. let value = Double(mapGlucoseNormal.count * 100 / mapGlucose.count)
  197. Text(units == .mmolL ? "3.9-7.8" : "70-140").foregroundColor(.secondary)
  198. Text(value.formatted()).foregroundColor(.green)
  199. Text("%").foregroundColor(.secondary)
  200. }.font(.caption)
  201. HStack {
  202. let value = Double(mapGlucoseAcuteLow.count * 100 / mapGlucose.count)
  203. Text(units == .mmolL ? "< 3.3 " : "< 59 ").foregroundColor(.secondary)
  204. Text(value.formatted()).foregroundColor(.red)
  205. Text("%").foregroundColor(.secondary)
  206. }.font(.caption)
  207. }
  208. }
  209. }
  210. var groupedGlucoseStatsLaying: some View {
  211. HStack {
  212. let mapGlucose = glucose.compactMap({ each in each.glucose })
  213. if !mapGlucose.isEmpty {
  214. let mapGlucoseLow = mapGlucose.filter({ $0 < Int16(3.3 / 0.0555) })
  215. let mapGlucoseNormal = mapGlucose.filter({ $0 > Int16(3.8 / 0.0555) && $0 < Int16(7.9 / 0.0555) })
  216. let mapGlucoseAcuteHigh = mapGlucose.filter({ $0 > Int16(11 / 0.0555) })
  217. HStack {
  218. let value = Double(mapGlucoseLow.count * 100 / mapGlucose.count)
  219. Text(units == .mmolL ? "< 3.3" : "< 59").font(.caption2).foregroundColor(.secondary)
  220. Text(value.formatted()).font(.caption).foregroundColor(value == 0 ? .green : .red)
  221. Text("%").font(.caption)
  222. }
  223. Spacer()
  224. HStack {
  225. let value = Double(mapGlucoseNormal.count * 100 / mapGlucose.count)
  226. Text(units == .mmolL ? "3.9-7.8" : "70-140").foregroundColor(.secondary)
  227. Text(value.formatted()).foregroundColor(.green)
  228. Text("%").foregroundColor(.secondary)
  229. }.font(.caption)
  230. Spacer()
  231. HStack {
  232. let value = Double(mapGlucoseAcuteHigh.count * 100 / mapGlucose.count)
  233. Text(units == .mmolL ? "> 11.0" : "> 198").font(.caption).foregroundColor(.secondary)
  234. Text(value.formatted()).font(.caption).foregroundColor(value == 0 ? .green : .orange)
  235. Text("%").font(.caption)
  236. }
  237. }
  238. }
  239. }
  240. private func tir() -> [(decimal: Decimal, string: String)] {
  241. let hypoLimit = Int(lowLimit)
  242. let hyperLimit = Int(highLimit)
  243. let justGlucoseArray = glucose.compactMap({ each in Int(each.glucose as Int16) })
  244. let totalReadings = justGlucoseArray.count
  245. let hyperArray = glucose.filter({ $0.glucose > hyperLimit })
  246. let hyperReadings = hyperArray.compactMap({ each in each.glucose as Int16 }).count
  247. let hyperPercentage = Double(hyperReadings) / Double(totalReadings) * 100
  248. let hypoArray = glucose.filter({ $0.glucose < hypoLimit })
  249. let hypoReadings = hypoArray.compactMap({ each in each.glucose as Int16 }).count
  250. let hypoPercentage = Double(hypoReadings) / Double(totalReadings) * 100
  251. let tir = 100 - (hypoPercentage + hyperPercentage)
  252. var array: [(decimal: Decimal, string: String)] = []
  253. array.append((decimal: Decimal(hypoPercentage), string: "Low"))
  254. array.append((decimal: Decimal(tir), string: "NormaL"))
  255. array.append((decimal: Decimal(hyperPercentage), string: "High"))
  256. return array
  257. }
  258. }