19.3 C
London
Monday, September 2, 2024

ios – SwiftUI ➙ Swift Chart: How can I exploit a variable to decide on which vector(?) in my struct to plot?


I’m making an attempt to reuse a SwiftUI Swift Chart view as an alternative of getting six individually outlined Chart views. (The six separate charts ARE working wonderful)

Q: How does one use a var to symbolize totally different “columns” of information from my array of structs I need to be plotted.

(factor.xGyrofactor.yGyro ) within the y: .worth("xGyro", factor.xGyro)

To scale back code duplication, I might reasonably not use a swap/case assertion for the entire Chart {} block.

I did strive utilizing a swap/case for simply the y: .worth... line however I acquired an error.

What I do not know learn how to do is exchange the (at present) hard-coded factor.xGyro with a variable that I can go in to plot totally different factors [ xGyro | yGyro | zGyro | xAccel | yAccel | zAccel ] within the following line with a variable.

y: .worth("xGyro", factor.xGyro)

The supply for my charts is an array of structs MotionDataPointClass.swift:

class MotionDataPointM: Identifiable {
   var xGyro: Double
   var yGyro: Double
   var zGyro: Double
   var xAccel: Double
   var yAccel: Double
   var zAccel: Double
   var timeStamp: Date
   var myIndex: Int
   var id: Date { timeStamp }
}

The chart code is SingleChartView.swift

import os
import SwiftUI
import CoreMotion
import CoreLocation
import Charts


struct SingleChartView: View {
   @ObservedObject var locationsHandler = LocationsHandler.shared
   let motionDataPoints: [MotionDataPointM]
   
   let numberOfPointsToPlot: Int
   let vertScaleMin: Double
   let vertScaleMax: Double
   let cornerLabel: String
   let lineColor: Colour
   let valuesToPlot: Int
   
   var physique: some View {
      Chart{
         ForEach(motionDataPoints.suffix(numberOfPointsToPlot)) { factor in
            LineMark(
               x: .worth("Date", factor.timeStamp),

y: .worth("xGyro", factor.xGyro)

            )
         }
         .interpolationMethod(.catmullRom)
      }
      .foregroundStyle(lineColor)
      .chartYScale(area: [vertScaleMin, vertScaleMax])
      .padding(.prime, 10.0)
      .padding(.backside, 3.0)
      .padding(.horizontal, 3.0)
      .overlay(
         RoundedRectangle(cornerRadius: 10)
            .stroke(Colour.purple, lineWidth: 1.0)
      )
      .overlay(
         HStack {
            VStack {
               Textual content("(cornerLabel)")
                  .font(.largeTitle)
                  .fontWeight(.black)
                  .foregroundColor(lineColor)
                  .opacity(0.3)
                  .padding(.main, 4)
                  .padding(.prime, -3)
               Spacer()
            }
            Spacer()
         }
      )
   }
}

I’m calling/invoking the chart from my ContentView.swift file…

SingleChartView(motionDataPoints: motionDataPoints, numberOfPointsToPlot: numberOfPointsToPlot, vertScaleMin: gyroMinX, vertScaleMax: gyroMaxX, cornerLabel: "W", lineColor: Colour("YChartLines"), valuesToPlot: vectorToPlot.xGyro)

I outlined an enum to symbolize the totally different columns/vectors within the knowledge struct…

enum vectorToPlot {
   public static let xGyro = 1
   public static let yGyro = 2
   public static let zGyro = 3
   public static let xAccel = 4
   public static let yAccel = 5
   public static let zAccel = 6
}

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here