GlucoseChartView.swift 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import Charts
  2. import Foundation
  3. import SwiftUI
  4. struct GlucoseChartView: ChartContent {
  5. let glucoseData: [GlucoseStored]
  6. let manualGlucoseData: [GlucoseStored]
  7. let units: GlucoseUnits
  8. let highGlucose: Decimal
  9. let lowGlucose: Decimal
  10. let smooth: Bool
  11. let gradientStops: [Gradient.Stop]
  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. if smooth {
  19. LineMark(x: .value("Time", item.date ?? Date()), y: .value("Value", glucoseToDisplay))
  20. .foregroundStyle(
  21. LinearGradient(
  22. gradient: Gradient(stops: gradientStops),
  23. startPoint: .bottom,
  24. endPoint: .top
  25. )
  26. )
  27. .symbol(.circle)
  28. .symbolSize(34)
  29. } else {
  30. if glucoseToDisplay > highGlucose {
  31. PointMark(
  32. x: .value("Time", item.date ?? Date(), unit: .second),
  33. y: .value("Value", glucoseToDisplay)
  34. ).foregroundStyle(Color.orange.gradient).symbolSize(20)
  35. } else if glucoseToDisplay < lowGlucose {
  36. PointMark(
  37. x: .value("Time", item.date ?? Date(), unit: .second),
  38. y: .value("Value", glucoseToDisplay)
  39. ).foregroundStyle(Color.red.gradient).symbolSize(20)
  40. } else {
  41. PointMark(
  42. x: .value("Time", item.date ?? Date(), unit: .second),
  43. y: .value("Value", glucoseToDisplay)
  44. ).foregroundStyle(Color.green.gradient).symbolSize(20)
  45. }
  46. }
  47. }
  48. }
  49. }