ForeCastChart.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import Charts
  2. import CoreData
  3. import Foundation
  4. import SwiftUI
  5. struct ForeCastChart: View {
  6. @StateObject var state: Bolus.StateModel
  7. @Environment(\.colorScheme) var colorScheme
  8. @Binding var units: GlucoseUnits
  9. @State private var startMarker = Date(timeIntervalSinceNow: -4 * 60 * 60)
  10. private var endMarker: Date {
  11. Date(timeIntervalSinceNow: TimeInterval(2 * 5 * state.minCount * 60)) // min is 2h -> (2*1h = 2*(5*12*60))
  12. }
  13. var body: some View {
  14. VStack {
  15. forecastChart
  16. .padding(.vertical, 3)
  17. HStack {
  18. Spacer()
  19. Image(systemName: "arrow.right.circle")
  20. .font(.system(size: 16, weight: .bold))
  21. if let eventualBG = state.simulatedDetermination?.eventualBG {
  22. HStack {
  23. Text("\(eventualBG)")
  24. .font(.footnote)
  25. .foregroundStyle(.primary)
  26. Text("\(units.rawValue)")
  27. .font(.footnote)
  28. .foregroundStyle(.secondary)
  29. }
  30. } else {
  31. Text("---")
  32. .font(.footnote)
  33. .foregroundStyle(.primary)
  34. Text("\(units.rawValue)")
  35. .font(.footnote)
  36. .foregroundStyle(.secondary)
  37. }
  38. }
  39. }
  40. }
  41. private var forecastChart: some View {
  42. Chart {
  43. drawGlucose()
  44. drawCurrentTimeMarker()
  45. drawForecastArea()
  46. }
  47. .chartXAxis { forecastChartXAxis }
  48. .chartXScale(domain: startMarker ... endMarker)
  49. .chartYAxis { forecastChartYAxis }
  50. .chartYScale(domain: units == .mgdL ? 0 ... 300 : 0.asMmolL ... 300.asMmolL)
  51. }
  52. private func drawGlucose() -> some ChartContent {
  53. ForEach(state.glucoseFromPersistence) { item in
  54. if item.glucose > Int(state.highGlucose) {
  55. PointMark(
  56. x: .value("Time", item.date ?? Date(), unit: .second),
  57. y: .value("Value", units == .mgdL ? Decimal(item.glucose) : Decimal(item.glucose).asMmolL)
  58. )
  59. .foregroundStyle(Color.orange.gradient)
  60. .symbolSize(20)
  61. } else if item.glucose < Int(state.lowGlucose) {
  62. PointMark(
  63. x: .value("Time", item.date ?? Date(), unit: .second),
  64. y: .value("Value", units == .mgdL ? Decimal(item.glucose) : Decimal(item.glucose).asMmolL)
  65. )
  66. .foregroundStyle(Color.red.gradient)
  67. .symbolSize(20)
  68. } else {
  69. PointMark(
  70. x: .value("Time", item.date ?? Date(), unit: .second),
  71. y: .value("Value", units == .mgdL ? Decimal(item.glucose) : Decimal(item.glucose).asMmolL)
  72. )
  73. .foregroundStyle(Color.green.gradient)
  74. .symbolSize(20)
  75. }
  76. }
  77. }
  78. private func timeForIndex(_ index: Int32) -> Date {
  79. let currentTime = Date()
  80. let timeInterval = TimeInterval(index * 300)
  81. return currentTime.addingTimeInterval(timeInterval)
  82. }
  83. private func drawForecastArea() -> some ChartContent {
  84. ForEach(0 ..< max(state.minForecast.count, state.maxForecast.count), id: \.self) { index in
  85. if index < state.minForecast.count, index < state.maxForecast.count {
  86. let yMinValue = Decimal(state.minForecast[index]) <= 300 ? Decimal(state.minForecast[index]) : Decimal(300)
  87. let yMaxValue = Decimal(state.maxForecast[index]) <= 300 ? Decimal(state.maxForecast[index]) : Decimal(300)
  88. AreaMark(
  89. x: .value("Time", timeForIndex(Int32(index)) <= endMarker ? timeForIndex(Int32(index)) : endMarker),
  90. yStart: .value("Min Value", units == .mgdL ? yMinValue : yMinValue.asMmolL),
  91. yEnd: .value("Max Value", units == .mgdL ? yMaxValue : yMaxValue.asMmolL)
  92. )
  93. .foregroundStyle(Color.blue.opacity(0.5))
  94. .interpolationMethod(.catmullRom)
  95. }
  96. }
  97. }
  98. private func drawCurrentTimeMarker() -> some ChartContent {
  99. RuleMark(
  100. x: .value(
  101. "",
  102. Date(timeIntervalSince1970: TimeInterval(NSDate().timeIntervalSince1970)),
  103. unit: .second
  104. )
  105. ).lineStyle(.init(lineWidth: 2, dash: [3])).foregroundStyle(Color(.systemGray2))
  106. }
  107. private var forecastChartXAxis: some AxisContent {
  108. AxisMarks(values: .stride(by: .hour, count: 2)) { _ in
  109. AxisGridLine(stroke: .init(lineWidth: 0.5, dash: [2, 3]))
  110. AxisValueLabel(format: .dateTime.hour(.defaultDigits(amPM: .narrow)), anchor: .top)
  111. .font(.footnote)
  112. .foregroundStyle(Color.primary)
  113. }
  114. }
  115. private var forecastChartYAxis: some AxisContent {
  116. AxisMarks(position: .trailing) { _ in
  117. AxisGridLine(stroke: .init(lineWidth: 0.5, dash: [2, 3]))
  118. AxisTick(length: 3, stroke: .init(lineWidth: 3)).foregroundStyle(Color.secondary)
  119. AxisValueLabel().font(.footnote).foregroundStyle(Color.primary)
  120. }
  121. }
  122. }