WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

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

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

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


Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.