CobChart.swift 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import Charts
  2. import Foundation
  3. import SwiftUI
  4. extension MainChartView {
  5. var cobChart: some View {
  6. Chart {
  7. drawCurrentTimeMarker()
  8. drawCOB(dummy: false)
  9. if #available(iOS 17, *) {
  10. if let selectedCOBValue {
  11. PointMark(
  12. x: .value("Time", selectedCOBValue.deliverAt ?? now, unit: .minute),
  13. y: .value("Value", selectedCOBValue.cob)
  14. )
  15. .symbolSize(CGSize(width: 15, height: 15))
  16. .foregroundStyle(Color.orange.opacity(0.8))
  17. PointMark(
  18. x: .value("Time", selectedCOBValue.deliverAt ?? now, unit: .minute),
  19. y: .value("Value", selectedCOBValue.cob)
  20. )
  21. .symbolSize(CGSize(width: 6, height: 6))
  22. .foregroundStyle(Color.primary)
  23. }
  24. }
  25. }
  26. .frame(minHeight: geo.size.height * 0.12)
  27. .frame(width: fullWidth(viewWidth: screenSize.width))
  28. .chartXScale(domain: startMarker ... endMarker)
  29. .backport.chartXSelection(value: $selection)
  30. .chartXAxis { basalChartXAxis }
  31. .chartYAxis { cobChartYAxis }
  32. .chartYScale(domain: state.minValueCobChart ... state.maxValueCobChart)
  33. }
  34. func drawCOB(dummy: Bool) -> some ChartContent {
  35. ForEach(state.enactedAndNonEnactedDeterminations) { cob in
  36. let amount = Int(cob.cob)
  37. let date: Date = cob.deliverAt ?? Date()
  38. if dummy {
  39. LineMark(x: .value("Time", date), y: .value("Value", amount))
  40. .foregroundStyle(Color.clear)
  41. AreaMark(x: .value("Time", date), y: .value("Value", amount)).foregroundStyle(
  42. Color.clear
  43. )
  44. } else {
  45. LineMark(x: .value("Time", date), y: .value("Value", amount))
  46. .foregroundStyle(Color.orange.gradient)
  47. AreaMark(x: .value("Time", date), y: .value("Value", amount)).foregroundStyle(
  48. LinearGradient(
  49. gradient: Gradient(
  50. colors: [
  51. Color.orange.opacity(0.8),
  52. Color.orange.opacity(0.01)
  53. ]
  54. ),
  55. startPoint: .top,
  56. endPoint: .bottom
  57. )
  58. )
  59. }
  60. }
  61. }
  62. }