UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out 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.