iosswiftswiftuicharts

How to fix leading and trailing x axis label truncation in Swift Chart


I have a simple chart that only shows it's leading (first) and trailing (last) labels on x axis. Something like this

import SwiftUI
import Charts

// ...

Chart(data) {
  
}
.chartXAxis {
  AxisMarks(values: [firstValue, lastValue]) { _ in
    AxisValueLabel()
  }
}

This works fine with few data points i.e.

enter image description here

But as soon as my graph grows and these labels are pushed to the "edges" of it, they get truncated like this

enter image description here

Is there any way to disable this behavior / force these to always show despite amount of data, what actual labels say and device screen size?


Solution

  • Adding AxisMarks(preset: .aligned) fixes the issue and displays labels in full.