TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Charts - Multiple Lines & Data Separation

Forums > SwiftUI

How do I separate these two series of data in the Chart? They are meant to represent a curve and a straight line tangent to the curve, but it keeps connecting the two lines at one end. I know i'm not utilizing GraphModel in this sample data, it's just an idea to name each "line" independently. Also most values are hard coded in for simplicity. I would appreciate any help!

import SwiftUI
import Charts

struct GraphModel: Identifiable {
    let id = UUID()
    let line: String
    let values: [(x: Double, y: Double)]
}

class graphViewModel: ObservableObject {
    var startPoint: Int{ Int(-1084)}
    var endPoint: Int{ Int(startPoint + Int(2000))}
    var values1: [(x: Double, y: Double)]{
        (startPoint...endPoint).map { x in
            let y: Double = Double(3383) * Double(cosh(Double(x) / (3383)) - 1 )
            return (Double(x), y)
        }
    }

    var values2: [(x: Double, y: Double)]{
        (startPoint...endPoint).map { x in
            let y: Double = ((Double(x) * (-0.079)) - 10.62)
            return (Double(x), y)
        }
    }
}

struct ContentView: View {
    @ObservedObject var viewModel: graphViewModel

    var body: some View {
        VStack(alignment: .leading) {

            Chart {
                ForEach(viewModel.values1, id:\.x){ val in
                    LineMark(
                        x: .value("x", val.x),
                        y: .value("y", val.y))
                }
                ForEach(viewModel.values2, id:\.x){ val in
                    LineMark(
                        x: .value("x", val.x),
                        y: .value("y", val.y))
                }

            }.frame(height: 250)
                .chartXAxis(.hidden)
        }.padding()
    }
}

let viewModel = graphViewModel()
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView(viewModel: viewModel)
    }
}

1      

You're not utilizing the series information, so you can try this.

ForEach(viewModel.values1, id:\.x){ val in
    LineMark(
        x: .value("x", val.x),
        y: .value("y", val.y),
        series: .value("", "Curve"))
}
ForEach(viewModel.values2, id:\.x){ val in
    LineMark(
        x: .value("x", val.x),
        y: .value("y", val.y),
        series: .value("", "Tangent"))
}

1      

You're a genius sir, thanks!

1      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.