ChartsView.swift 12 KB

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