Axis Customization
This example demonstrates how to customize chart axes using chartXAxis and chartYAxis. Beyond the legacy Visibility toggle, both modifiers now accept an AxisMarksConfig object that maps to SwiftUI Charts' AxisMarks + AxisGridLine + AxisTick + AxisValueLabel.
Quick reference
AxisMarkValues accepts:
'automatic'{ type: 'automatic', desiredCount?, roundLowerBound?, roundUpperBound? }{ type: 'stride', by: number }— forDoubleaxis data{ type: 'strideDate', by: CalendarComponent, count? }— forDateaxis data{ type: 'values', values: number[] | string[] | Date[] }— explicit ticks (the array element type must match the chart's plotted axis type)
Example sections
1. Default axes (backward-compatible)
If you do not set chartXAxis / chartYAxis, the system picks defaults — exactly as before.
2. Stride + dashed grid + currency labels
values: { type: 'stride', by: 1000 }puts a tick every 1000 units along the Y axis.gridLine.stroke.dashproduces a dashed grid line.valueLabel.format: 'currency'formats each tick label using the device locale's currency style.
3. Explicit values + percent labels
- Pins ticks at exactly the values you list.
format: 'percent'shows10%,20%, etc.
4. Custom view label
- Replaces every X-axis tick label with a custom view.
gridLine: falsehides grid lines entirely.- Performance note: the view content is rebuilt for every tick — keep the view tree small.
5. Legacy Visibility tokens
The original 'automatic' | 'visible' | 'hidden' form is fully preserved.
Format tokens
valueLabel.format accepts these tokens:
Pitfalls
valuesarray element type must match the chart's axis data type. Passing[Date]when the chart's X-axis data isstringrenders an empty axis (silent fallback, not a crash).multiLineTextAlignmentis deprecated — usemultiLabelAlignment(mirrors the SwiftUI Charts SDK name and accepts the full 9-directionAlignment). The deprecated alias still works for backward compatibility but only accepts'leading' | 'center' | 'trailing'.- Custom
valueLabel.contentre-renders every tick. Keep the view light and avoid heavy computation inside.
Conclusion
AxisMarksConfig lifts a previously hidden ceiling: anything you could express through SwiftUI's AxisMarks { ... } content closure is now available as a declarative chart prop, while the original Visibility form continues to work unchanged.
