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

SOLVED: Is there a method to increment plotting indices by a user-defined Integer?

Forums > SwiftUI

For example, I would like to plot every 10th point of the arrays "xData" and "yData". I have tried playing around with "index", but recieve an error stating that "index" is immutable. Another approach I tried was to create two new data arrays decremented by the approprate value (i.e., use verey 10th element) , but that seems messy.

Data arrays defined as follows:

public let xData = [499.9,500.9,501.9,502.9,503.9,504.9,505.9,506.9,507.9,508.9,509.9,510.9,511.9,512.9,513.9,514.9,515.9...]
public let yData = [104.59,104.17,103.76,103.34,102.93,102.51,102.1,101.68,101.27,100.86,100.45,100.04,99.633,99.225,98.818...]

Swiftui code that renders a LineMark plot:

import SwiftUI
import Charts

struct ContentView: View {
        var body: some View {
        NavigationStack {
                Chart(0..<xData.count, id: \.self) { index in
                    LineMark(
                        x: .value("X-Axis", xData[index]),
                        y: .value("Y-Axis", yData[index]) )
                }
                .chartXAxisLabel(position: .bottom, alignment: .center)
                { Text("X-Axis")
                    .font(.system(size: 14)) }

                .chartYAxisLabel(position: .leading, alignment: .center)
                { Text("Y-Axis")
                        .font(.system(size: 14))
                        .rotationEffect(Angle(degrees: 180))
                        .frame(width: 75, height: 75)
                }
                .chartYAxis {
                    AxisMarks(position: .leading)
                }
                                .padding()
        }
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Thank you in advance!

2      

I think I found a solution. Place the LineMark inside a closure that uses "index.isMultiple(of: 10)". This appaers to work.

                        Chart(0..<xValues.count, id: \.self) { index in
                            if( index.isMultiple(of: 10) ) {
                                LineMark(
                                    x: .value("X-Axis", xValues[index]),
                                    y: .value("Y-Axis", yValues[index]) )
                                .lineStyle(.init(lineWidth: 1.0))   /* Set LineMark width */
                                .foregroundStyle(.white)    /* Set LineMark color */
                            }
                        }

2      

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.