ChartsView.swift 13 KB

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