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

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"

   

Partial solution:

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

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

   

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.