GlucoseChartView.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import Charts
  2. import Foundation
  3. import SwiftUI
  4. struct GlucoseChartView: ChartContent {
  5. let glucoseData: [GlucoseStored]
  6. let units: GlucoseUnits
  7. let highGlucose: Decimal
  8. let lowGlucose: Decimal
  9. let currentGlucoseTarget: Decimal
  10. let isSmoothingEnabled: Bool
  11. let glucoseColorScheme: GlucoseColorScheme
  12. var body: some ChartContent {
  13. drawGlucoseChart()
  14. }
  15. private func drawGlucoseChart() -> some ChartContent {
  16. ForEach(glucoseData) { item in
  17. let glucoseToDisplay = units == .mgdL ? Decimal(item.glucose) : Decimal(item.glucose).asMmolL
  18. // low glucose, high glucose and target is parsed in state to mmol/L; parse it back to mg/dl here for comparison
  19. let lowGlucose = units == .mgdL ? lowGlucose : lowGlucose.asMgdL
  20. let highGlucose = units == .mgdL ? highGlucose : highGlucose.asMgdL
  21. let targetGlucose = units == .mgdL ? currentGlucoseTarget : currentGlucoseTarget.asMgdL
  22. let pointMarkColor: Color = FreeAPS.getDynamicGlucoseColor(
  23. glucoseValue: Decimal(item.glucose),
  24. highGlucoseColorValue: highGlucose,
  25. lowGlucoseColorValue: lowGlucose,
  26. targetGlucose: targetGlucose,
  27. glucoseColorScheme: glucoseColorScheme,
  28. offset: 20
  29. )
  30. if !isSmoothingEnabled {
  31. PointMark(
  32. x: .value("Time", item.date ?? Date(), unit: .second),
  33. y: .value("Value", glucoseToDisplay)
  34. )
  35. .foregroundStyle(pointMarkColor)
  36. .symbolSize(20)
  37. .symbol {
  38. if item.isManual {
  39. Image(systemName: "drop.fill")
  40. .font(.system(size: 10))
  41. .symbolRenderingMode(.monochrome)
  42. .bold()
  43. .foregroundStyle(.red)
  44. } else {
  45. Image(systemName: "circle.fill")
  46. .font(.system(size: 5))
  47. .bold()
  48. .foregroundStyle(pointMarkColor)
  49. }
  50. }
  51. } else {
  52. PointMark(
  53. x: .value("Time", item.date ?? Date(), unit: .second),
  54. y: .value("Value", glucoseToDisplay)
  55. )
  56. .symbol {
  57. if item.isManual {
  58. Image(systemName: "drop.fill")
  59. .font(.system(size: 10))
  60. .symbolRenderingMode(.monochrome)
  61. .bold()
  62. .foregroundStyle(.red)
  63. } else {
  64. Image(systemName: "record.circle.fill")
  65. .font(.system(size: 8))
  66. .bold()
  67. .foregroundStyle(pointMarkColor)
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }