Quick Tip Icon
Quick Tip

Align y-axis to leading edge in Swift Charts

The default behaviour in Swift Charts appears to always draw the y-axis in the trailing edge of the chart. However, in many cases I would prefer my charts to render with this y-axis on the leading edge.

I have found that I can controle the position of the y-axis ticks using chartYAxis() modifier in combination with AxisMarks(position: .leading).

struct SimpleChart: View {    
    var body: some View {
        Chart { ... }
        .chartYAxis {
            AxisMarks(position: .leading)
        }
    }
}
A bar chart with the y-axis on the leading edge