CalibrationsChart.swift 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import SwiftUI
  2. struct CalibrationsChart: View {
  3. @EnvironmentObject var state: Calibrations.StateModel
  4. private let maxValue = 400.0
  5. var body: some View {
  6. GeometryReader { geo in
  7. ZStack(alignment: .top) {
  8. Rectangle().fill(Color.secondary)
  9. .frame(height: geo.size.width)
  10. Path { path in
  11. let size = geo.size.width
  12. path.move(
  13. to:
  14. CGPoint(
  15. x: 0,
  16. y: size - state.calibrate(0) / maxValue * geo.size.width
  17. )
  18. )
  19. path.addLine(
  20. to: CGPoint(
  21. x: size,
  22. y: size - state.calibrate(maxValue) / maxValue * geo.size.width
  23. )
  24. )
  25. }
  26. .stroke(.blue, lineWidth: 2)
  27. ForEach(state.calibrations, id: \.self) { value in
  28. Circle().fill(.red)
  29. .frame(width: 6, height: 6)
  30. .position(
  31. x: value.x / maxValue * geo.size.width,
  32. y: geo.size.width - (value.y / maxValue * geo.size.width)
  33. )
  34. }
  35. }
  36. .frame(height: geo.size.width)
  37. .clipped()
  38. }
  39. }
  40. }