NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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
                        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)
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {

Thank you in advance!


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) ) {
                                    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 */


Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

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

Reply to this topic…

You need to create an account or log in to reply.

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.