ChartsView.swift 12 KB

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