MainChartView2.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import Charts
  2. import SwiftUI
  3. struct MainChartView2: View {
  4. // MARK: BINDINGS
  5. @Binding var glucose: [BloodGlucose]
  6. @Binding var screenHours: Int16
  7. var body: some View {
  8. NavigationStack {
  9. ScrollView {
  10. VStack {
  11. let filteredGlucose: [BloodGlucose] = filterGlucoseData(for: screenHours)
  12. Chart(filteredGlucose) {
  13. PointMark(
  14. x: .value("Time", $0.dateString),
  15. y: .value("Value", $0.value)
  16. )
  17. .foregroundStyle(Color.green.gradient)
  18. .cornerRadius(0)
  19. }
  20. .frame(height: 350)
  21. .chartXAxis {
  22. // MARK: THIS PIECE OF CODE BELONGS TO THE UNIQUEHOURLABELS FUNC....BUT DOES NOT WORK
  23. // let uniqueHourLabels = self.uniqueHourLabels(for: filteredGlucose)
  24. // AxisMarks(values: uniqueHourLabels) { _ in
  25. // AxisValueLabel(format: .dateTime.hour())
  26. // }
  27. AxisMarks(values: filteredGlucose.map(\.dateString)) { _ in
  28. AxisValueLabel(format: .dateTime.hour())
  29. }
  30. }
  31. Legend()
  32. }
  33. .padding()
  34. }
  35. }
  36. }
  37. // // MARK: THIS FUNCTION DOES NOT WORK BUT COULD MAYBE IMPROVED?
  38. //
  39. // private func uniqueHourLabels(for glucoseData: [BloodGlucose]) -> [String] {
  40. // var uniqueLabels: Set<String> = Set()
  41. // var result: [String] = []
  42. //
  43. // let dateFormatter = DateFormatter()
  44. // dateFormatter.locale = Locale(identifier: "en_US_POSIX")
  45. // dateFormatter.setLocalizedDateFormatFromTemplate("HH")
  46. //
  47. // let currentHour = Calendar.current.component(.hour, from: Date())
  48. //
  49. // for entry in glucoseData {
  50. // let hourLabel = dateFormatter.string(from: entry.dateString)
  51. // if !uniqueLabels.contains(hourLabel), currentHour == Calendar.current.component(.hour, from: entry.dateString) {
  52. // uniqueLabels.insert(hourLabel)
  53. // result.append(hourLabel)
  54. // }
  55. // }
  56. //
  57. // return result
  58. // }
  59. // MARK: WORKS NOW...
  60. private func filterGlucoseData(for hours: Int16) -> [BloodGlucose] {
  61. guard hours > 0 else {
  62. return glucose
  63. }
  64. let currentDate = Date()
  65. let startDate = Calendar.current.date(byAdding: .hour, value: -Int(hours), to: currentDate) ?? currentDate
  66. // print("die hours sind ++++++++++++++++++++")
  67. // print(hours)
  68. return glucose.filter { $0.dateString >= startDate }
  69. }
  70. }
  71. // MARK: LEGEND PANEL FOR CHART
  72. struct Legend: View {
  73. var body: some View {
  74. HStack {
  75. Image(systemName: "line.diagonal")
  76. .rotationEffect(Angle(degrees: 45))
  77. .foregroundColor(.green)
  78. Text("BG")
  79. .foregroundColor(.secondary)
  80. Spacer()
  81. Image(systemName: "line.diagonal")
  82. .rotationEffect(Angle(degrees: 45))
  83. .foregroundColor(.insulin)
  84. Text("IOB")
  85. .foregroundColor(.secondary)
  86. Spacer()
  87. Image(systemName: "line.diagonal")
  88. .rotationEffect(Angle(degrees: 45))
  89. .foregroundColor(.purple)
  90. Text("ZT")
  91. .foregroundColor(.secondary)
  92. Spacer()
  93. Image(systemName: "line.diagonal")
  94. .rotationEffect(Angle(degrees: 45))
  95. .foregroundColor(.loopYellow)
  96. Text("COB")
  97. .foregroundColor(.secondary)
  98. Spacer()
  99. Image(systemName: "line.diagonal")
  100. .rotationEffect(Angle(degrees: 45))
  101. .foregroundColor(.orange)
  102. Text("UAM")
  103. .foregroundColor(.secondary)
  104. }
  105. .font(.caption2)
  106. .padding(.horizontal, 4)
  107. .padding(.vertical, 10)
  108. }
  109. }