GlucoseDistributionChart.swift 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import Charts
  2. import SwiftUI
  3. struct GlucoseDistributionChart: View {
  4. let glucose: [GlucoseStored]
  5. let highLimit: Decimal
  6. let lowLimit: Decimal
  7. let isToday: Bool
  8. let units: GlucoseUnits
  9. let glucoseRangeStats: [GlucoseRangeStats]
  10. var body: some View {
  11. VStack(alignment: .leading, spacing: 8) {
  12. Text("Glucose Distribution")
  13. .font(.headline)
  14. Chart(glucoseRangeStats) { range in
  15. ForEach(range.values, id: \.hour) { value in
  16. AreaMark(
  17. x: .value("Hour", Calendar.current.dateForChartHour(value.hour)),
  18. y: .value("Count", value.count),
  19. stacking: .normalized
  20. )
  21. .foregroundStyle(by: .value("Range", range.name))
  22. }
  23. }
  24. .chartForegroundStyleScale([
  25. "<54": .purple.opacity(0.7),
  26. "54-70": .red.opacity(0.7),
  27. "70-140": .green,
  28. "140-180": .green.opacity(0.7),
  29. "180-200": .yellow.opacity(0.7),
  30. "200-220": .orange.opacity(0.7),
  31. ">220": .orange.opacity(0.8)
  32. ])
  33. .chartYAxis {
  34. AxisMarks(position: .leading)
  35. }
  36. .chartYAxisLabel(alignment: .leading) {
  37. Text("Percentage")
  38. .foregroundStyle(.primary)
  39. .font(.caption)
  40. .padding(.vertical, 3)
  41. }
  42. .chartXAxis {
  43. AxisMarks(values: .stride(by: .hour, count: 3)) { _ in
  44. AxisValueLabel(format: .dateTime.hour(.defaultDigits(amPM: .narrow)), anchor: .top)
  45. AxisGridLine()
  46. }
  47. }
  48. .frame(height: 200)
  49. }
  50. }
  51. }