Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Swift Charts Plotting

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))
            }
        }
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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))
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading