ForeCastChart.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. @State private var endMarker = Date(timeIntervalSinceNow: 3 * 60 * 60)
  11. private var conversionFactor: Decimal {
  12. units == .mmolL ? 0.0555 : 1
  13. }
  14. var body: some View {
  15. forecastChart
  16. }
  17. private var forecastChart: some View {
  18. Chart {
  19. drawGlucose()
  20. drawCurrentTimeMarker()
  21. drawForecasts()
  22. }
  23. .chartXAxis { forecastChartXAxis }
  24. .chartXScale(domain: startMarker ... endMarker)
  25. .chartYAxis { forecastChartYAxis }
  26. }
  27. private func drawGlucose() -> some ChartContent {
  28. ForEach(state.glucoseFromPersistence) { item in
  29. if item.glucose > Int(state.highGlucose) {
  30. PointMark(
  31. x: .value("Time", item.date ?? Date(), unit: .second),
  32. y: .value("Value", Decimal(item.glucose) * conversionFactor)
  33. )
  34. .foregroundStyle(Color.orange.gradient)
  35. .symbolSize(20)
  36. } else if item.glucose < Int(state.lowGlucose) {
  37. PointMark(
  38. x: .value("Time", item.date ?? Date(), unit: .second),
  39. y: .value("Value", Decimal(item.glucose) * conversionFactor)
  40. )
  41. .foregroundStyle(Color.red.gradient)
  42. .symbolSize(20)
  43. } else {
  44. PointMark(
  45. x: .value("Time", item.date ?? Date(), unit: .second),
  46. y: .value("Value", Decimal(item.glucose) * conversionFactor)
  47. )
  48. .foregroundStyle(Color.green.gradient)
  49. .symbolSize(20)
  50. }
  51. }
  52. }
  53. private func timeForIndex(_ index: Int32) -> Date {
  54. let currentTime = Date()
  55. let timeInterval = TimeInterval(index * 300)
  56. return currentTime.addingTimeInterval(timeInterval)
  57. }
  58. private func drawForecasts() -> some ChartContent {
  59. let predictions = state.predictionsForChart
  60. let predictionData = [
  61. ("IOB", predictions?.iob),
  62. ("ZT", predictions?.zt),
  63. ("COB", predictions?.cob),
  64. ("UAM", predictions?.uam)
  65. ]
  66. return ForEach(predictionData, id: \.0) { name, values in
  67. if let values = values {
  68. ForEach(values.indices, id: \.self) { index in
  69. LineMark(
  70. x: .value("Time", timeForIndex(Int32(index))),
  71. y: .value("Value", Decimal(values[index]) * conversionFactor)
  72. )
  73. .foregroundStyle(by: .value("Prediction Type", name))
  74. }
  75. }
  76. }
  77. }
  78. // private func drawForecasts() -> some ChartContent {
  79. // ForEach(state.preprocessedData, id: \.id) { tuple in
  80. // let forecastValue = tuple.forecastValue
  81. // let forecast = tuple.forecast
  82. // let valueAsDecimal = Decimal(forecastValue.value)
  83. // let displayValue = units == .mmolL ? valueAsDecimal.asMmolL : valueAsDecimal
  84. //
  85. // LineMark(
  86. // x: .value("Time", timeForIndex(forecastValue.index)),
  87. // y: .value("Value", displayValue)
  88. // )
  89. // .foregroundStyle(by: .value("Predictions", forecast.type ?? ""))
  90. // }
  91. // }
  92. private func drawCurrentTimeMarker() -> some ChartContent {
  93. RuleMark(
  94. x: .value(
  95. "",
  96. Date(timeIntervalSince1970: TimeInterval(NSDate().timeIntervalSince1970)),
  97. unit: .second
  98. )
  99. ).lineStyle(.init(lineWidth: 2, dash: [3])).foregroundStyle(Color(.systemGray2))
  100. }
  101. private var forecastChartXAxis: some AxisContent {
  102. AxisMarks(values: .stride(by: .hour, count: 2)) { _ in
  103. AxisGridLine(stroke: .init(lineWidth: 0.5, dash: [2, 3]))
  104. AxisValueLabel(format: .dateTime.hour(.defaultDigits(amPM: .narrow)), anchor: .top)
  105. .font(.footnote)
  106. .foregroundStyle(Color.primary)
  107. }
  108. }
  109. private var forecastChartYAxis: some AxisContent {
  110. AxisMarks(position: .trailing) { _ in
  111. AxisGridLine(stroke: .init(lineWidth: 0.5, dash: [2, 3]))
  112. AxisTick(length: 3, stroke: .init(lineWidth: 3)).foregroundStyle(Color.secondary)
  113. AxisValueLabel().font(.footnote).foregroundStyle(Color.primary)
  114. }
  115. }
  116. }