GlucoseStackedAreaChart.swift 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import Charts
  2. import SwiftUI
  3. struct GlucoseStackedAreaChart: 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. .foregroundStyle(.primary)
  15. Chart(glucoseRangeStats) { range in
  16. ForEach(range.values, id: \.hour) { value in
  17. AreaMark(
  18. x: .value("Hour", Calendar.current.dateForChartHour(value.hour)),
  19. y: .value("Count", value.count),
  20. stacking: .normalized
  21. )
  22. .foregroundStyle(by: .value("Range", range.name))
  23. }
  24. }
  25. .chartForegroundStyleScale([
  26. "<54": .purple.opacity(0.7),
  27. "54-70": .red.opacity(0.7),
  28. "70-140": .green,
  29. "140-180": .green.opacity(0.7),
  30. "180-200": .yellow.opacity(0.7),
  31. "200-220": .orange.opacity(0.7),
  32. ">220": .orange.opacity(0.8)
  33. ])
  34. .chartYAxis {
  35. AxisMarks(position: .leading)
  36. }
  37. .chartYAxisLabel(alignment: .leading) {
  38. Text("Percentage")
  39. .foregroundStyle(.primary)
  40. .font(.caption)
  41. .padding(.vertical, 3)
  42. }
  43. .chartXAxis {
  44. AxisMarks(values: .stride(by: .hour, count: 3)) { _ in
  45. AxisValueLabel(format: .dateTime.hour(.defaultDigits(amPM: .narrow)), anchor: .top)
  46. AxisGridLine()
  47. }
  48. }
  49. .frame(height: 200)
  50. }
  51. }
  52. }