Home > Blockchain >  Swift Charts Plotting
Swift Charts Plotting

Time:10-02

I am wondering how I can plot Y(X) and X. An attempt is given below. Any suggestion/tips is highly appreciated.

import SwiftUI
import Charts
struct ContentView: View {
    let Y = [1.2,1.3,1.4,1.5]
    let X = [0.1,0.2,0.3,0.4]
    var body: some View {
        let YX = Array(zip(Y,X))
        Chart {
            ForEach(YX, id: \.self) { (sample,sample2) in
                LineMark(
                    x: .value("", sample),
                    y: .value("", sample2))
            }
        }
    }
}

CodePudding user response:

Use a struct instead of a tuple

struct YX: Hashable {
    let y: Double
    let x: Double
}

And create it using map

var body: some View {
    let yx = Array(zip(Y,X)).map(YX.init)
    Chart {
        ForEach(yx, id: \.self) { value in
            LineMark(
                x: .value("", value.x),
                y: .value("", value.y))
        }
    }
}
  • Related