GlucoseDistributionChart.swift 1.8 KB

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