GlucoseChartView.swift 839 B

123456789101112131415161718192021222324252627282930
  1. import CareKitUI
  2. import SwiftUI
  3. struct GlucoseChartView: UIViewRepresentable {
  4. @Binding var glucose: [BloodGlucose]
  5. func makeUIView(context _: Context) -> OCKCartesianGraphView {
  6. let view = OCKCartesianGraphView(type: .scatter)
  7. makeDataPointsFor(view: view)
  8. return view
  9. }
  10. func updateUIView(_ view: OCKCartesianGraphView, context _: Context) {
  11. makeDataPointsFor(view: view)
  12. }
  13. private func makeDataPointsFor(view: OCKCartesianGraphView) {
  14. let dataPoints = glucose.map {
  15. CGPoint(x: CGFloat($0.dateString.timeIntervalSince1970), y: CGFloat($0.sgv ?? 0))
  16. }
  17. var data = OCKDataSeries(
  18. dataPoints: dataPoints,
  19. title: "Glucose",
  20. color: .green
  21. )
  22. data.size = 1
  23. view.dataSeries = [data]
  24. }
  25. }