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

Need some assistance: Reverse the x-axis in lineMark

Forums > SwiftUI

Have tried several schemes to reverse the x-axis in SwiftUI's lineMark, but none are satisfactory. Specifically, would like to plot from high to low values (for example, index = 500 to 0) and have the x-axis labels start at 500 and go to 0.

Basic plottting code is below:

        Chart(0..<ydata.count, id: \.self) { index in
            LineMark(
                x: .value("X-axis", xdata[index]),
                y: .value("Y-axis", ydata[index])
            )
        }
.scaleEffect(x: -1, y: 1, anchor: .center)

The above does reverse the x-axis, but basically flips the entire chart and all of the axis labels are backwards.

 y: .value("Y-axis", xdata.reversed()[index])

The above also reverses the ordering of the xdata, but not the x-axis labels (i.e., starts at 0 and ends at 500).

.chartXScale(domain: highValue...lowValue)

The above results in an error - "Thread 1: Swift runtime failure: Range requires lowerBound <= upperBound"

2      

Partial solution:

        .chartXScale(
            domain: .automatic(includesZero: false, reversed: true)
        )

But unable to explicitly set XScale domain, .chartXScale(domain: 0...500) has no effect.

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot 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.