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

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