BolusStatsView.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import Charts
  2. import SwiftUI
  3. struct BolusStatsView: View {
  4. let bolusStats: [BolusStats]
  5. var body: some View {
  6. StatCard {
  7. VStack(alignment: .leading, spacing: 8) {
  8. Text("Bolus Distribution")
  9. .font(.headline)
  10. Chart(bolusStats) { stat in
  11. // External Bolus (Bottom)
  12. BarMark(
  13. x: .value("Date", stat.date, unit: .day),
  14. y: .value("Amount", stat.external)
  15. )
  16. .foregroundStyle(by: .value("Type", "External"))
  17. // SMB (Middle)
  18. BarMark(
  19. x: .value("Date", stat.date, unit: .day),
  20. y: .value("Amount", stat.smb)
  21. )
  22. .foregroundStyle(by: .value("Type", "SMB"))
  23. // Manual Bolus (Top)
  24. BarMark(
  25. x: .value("Date", stat.date, unit: .day),
  26. y: .value("Amount", stat.manualBolus)
  27. )
  28. .foregroundStyle(by: .value("Type", "Manual"))
  29. }
  30. .chartForegroundStyleScale([
  31. "Manual": Color.teal,
  32. "SMB": Color.blue,
  33. "External": Color.purple
  34. ])
  35. .chartLegend(position: .bottom, alignment: .leading, spacing: 12)
  36. .frame(height: 200)
  37. .chartXAxis {
  38. bolusStatsChartXAxisMarks
  39. }
  40. .chartYAxis {
  41. bolusStatsChartYAxisMarks
  42. }
  43. }
  44. List {
  45. ForEach(bolusStats) { _ in
  46. Text("")
  47. }
  48. }
  49. }
  50. }
  51. private var bolusStatsChartXAxisMarks: some AxisContent {
  52. AxisMarks { value in
  53. if let date = value.as(Date.self) {
  54. AxisValueLabel {
  55. // if selectedDays < 8 {
  56. // Text(date, format: .dateTime.weekday(.abbreviated))
  57. // } else {
  58. // Text(date, format: .dateTime.day().month(.defaultDigits))
  59. // }
  60. }
  61. AxisGridLine()
  62. }
  63. }
  64. }
  65. private var bolusStatsChartYAxisMarks: some AxisContent {
  66. AxisMarks(position: .leading) { value in
  67. if let amount = value.as(Double.self) {
  68. AxisValueLabel {
  69. Text(amount.formatted(.number.precision(.fractionLength(1))) + " U")
  70. }
  71. AxisGridLine()
  72. }
  73. }
  74. }
  75. }