GlucoseChartView.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. let pointMarkColor = FreeAPS.getDynamicGlucoseColor(
  19. glucoseValue: glucoseToDisplay,
  20. highGlucoseColorValue: highGlucose,
  21. lowGlucoseColorValue: lowGlucose,
  22. targetGlucose: currentGlucoseTarget,
  23. glucoseColorScheme: glucoseColorScheme,
  24. offset: units == .mgdL ? 20 : 20.asMmolL
  25. )
  26. if !isSmoothingEnabled {
  27. PointMark(
  28. x: .value("Time", item.date ?? Date(), unit: .second),
  29. y: .value("Value", glucoseToDisplay)
  30. )
  31. .foregroundStyle(pointMarkColor)
  32. .symbolSize(20)
  33. .symbol {
  34. if item.isManual {
  35. Image(systemName: "drop.fill")
  36. .font(.system(size: 10))
  37. .symbolRenderingMode(.monochrome)
  38. .bold()
  39. .foregroundStyle(.red)
  40. } else {
  41. Image(systemName: "circle.fill")
  42. .font(.system(size: 5))
  43. .bold()
  44. .foregroundStyle(pointMarkColor)
  45. }
  46. }
  47. } else {
  48. PointMark(
  49. x: .value("Time", item.date ?? Date(), unit: .second),
  50. y: .value("Value", glucoseToDisplay)
  51. )
  52. .symbol {
  53. if item.isManual {
  54. Image(systemName: "drop.fill")
  55. .font(.system(size: 10))
  56. .symbolRenderingMode(.monochrome)
  57. .bold()
  58. .foregroundStyle(.red)
  59. } else {
  60. Image(systemName: "record.circle.fill")
  61. .font(.system(size: 8))
  62. .bold()
  63. .foregroundStyle(pointMarkColor)
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }