CalibrationsChart.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import SwiftUI
  2. struct CalibrationsChart: View {
  3. var state: Calibrations.StateModel
  4. private var dateFormatter: DateFormatter {
  5. let formatter = DateFormatter()
  6. formatter.timeStyle = .short
  7. formatter.dateStyle = .short
  8. return formatter
  9. }
  10. private let maxValue = 400.0
  11. var body: some View {
  12. GeometryReader { geo in
  13. ZStack(alignment: .top) {
  14. Rectangle().fill(Color.secondary)
  15. .frame(height: geo.size.width)
  16. Path { path in
  17. let size = geo.size.width
  18. path.move(
  19. to:
  20. CGPoint(
  21. x: 0,
  22. y: size - state.calibrate(0) / maxValue * geo.size.width
  23. )
  24. )
  25. path.addLine(
  26. to: CGPoint(
  27. x: size,
  28. y: size - state.calibrate(Int(maxValue)) / maxValue * geo.size.width
  29. )
  30. )
  31. }
  32. .stroke(.blue, lineWidth: 2)
  33. ForEach(state.calibrations, id: \.self) { value in
  34. ZStack {
  35. Circle().fill(.red)
  36. .frame(width: 6, height: 6)
  37. .position(
  38. x: value.x / maxValue * geo.size.width,
  39. y: geo.size.width - (value.y / maxValue * geo.size.width)
  40. )
  41. Text(dateFormatter.string(from: value.date))
  42. .foregroundColor(.white)
  43. .font(.system(size: 10))
  44. .position(
  45. x: value.x / maxValue * geo.size.width,
  46. y: geo.size.width - (value.y / maxValue * geo.size.width) + 10
  47. )
  48. }
  49. }
  50. }
  51. .frame(height: geo.size.width)
  52. .clipped()
  53. }
  54. }
  55. }