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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.