Swift Charts
Build data visualizations with Swift Charts targeting iOS 26+. Compose marks
inside Chart or Chart3D, configure axes and scales with view modifiers, and
use vectorized plots or 3D plots when the data calls for them.
See references/charts-patterns.md [blocked] for extended patterns, 3D charts, accessibility, and theming guidance.
Contents
- Workflow
- Chart Container
- Mark Types
- Axis Customization
- Scale Configuration
- Foreground Style and Encoding
- Selection (iOS 17+)
- Scrollable Charts (iOS 17+)
- Annotations
- Legend
- Vectorized Plots (iOS 18+)
- 3D Charts (iOS 26+)
- Common Mistakes
- Review Checklist
- References
Workflow
1. Build a new chart
- Define data as an
Identifiablestruct or useid:key path. - Choose mark type(s):
BarMark,LineMark,PointMark,AreaMark,RuleMark,RectangleMark,SectorMark, orSurfacePlot. - Wrap 2D marks in
Chart; useChart3Donly for real spatial or surface data. - Encode visual channels:
.foregroundStyle(by:),.symbol(by:),.lineStyle(by:). - Configure axes with
.chartXAxis/.chartYAxis. - Set scale domains with
.chartXScale(domain:)/.chartYScale(domain:). - Add selection, scrolling, or annotations as needed.
- For 1000+ 2D data points, use vectorized plots (
BarPlot,LinePlot, etc.). - Render representative empty, typical, dense, large-text, high-contrast, and VoiceOver states; exercise selection and scrolling when present.
- If an encoding, axis, selection, or accessibility check fails, restore the data fixture, fix one layer, and rerun the same matrix before adding decoration.
2. Review existing chart code
Identify the data semantics before judging mark choice. Then trace every value through mark, scale, axis, style, selection, and accessibility output; run the same validation matrix used for new charts.
Chart Container
Use the data-driven initializer for a single collection. Use the content closure for mixed marks or multiple series, and pass id: when elements are not Identifiable. Load references/charts-patterns.md [blocked] for complete mixed-series, theming, accessibility, and 3D recipes.
Mark Types
BarMark (iOS 16+)
LineMark (iOS 16+)
PointMark (iOS 16+)
AreaMark (iOS 16+)
RuleMark (iOS 16+)
RectangleMark (iOS 16+)
SectorMark (iOS 17+)
Use SectorMark for strictly positive values; filter, aggregate, or explain zero/negative values outside the pie or donut.
Axis Customization
Scale Configuration
Foreground Style and Encoding
Selection (iOS 17+)
Scrollable Charts (iOS 17+)
Annotations
Legend
Vectorized Plots (iOS 18+)
Use for large datasets (1000+ points). Accept entire collections or functions.
Apply KeyPath-based modifiers before simple-value modifiers:
3D Charts (iOS 26+)
Use Chart3D for spatial data or bivariate surfaces, not as a decorative
replacement for ordinary 2D categorical or time-series charts. Chart3D
accepts SurfacePlot plus 3D initializers of PointMark, RuleMark, and
RectangleMark.
Common Mistakes
1. Missing series parameter for multi-line charts
2. Too many SectorMark slices
3. Missing scale domain when zero-baseline matters
4. Static foregroundStyle overriding data encoding
5. Individual marks for 10,000+ data points
6. Fixed chart height breaking Dynamic Type
7. KeyPath modifier after value modifier on vectorized plots
8. Missing accessibility labels
9. Treating angle selection as category selection
chartAngleSelection(value:) binds the selected plottable angle value. For
pie and donut charts, map that numeric value through cumulative sector ranges
before comparing it to a category label.
Review Checklist
- Data model uses
Identifiableor chart usesid:key path - Mark type matches goal (bar=comparison, line=trend, sector=proportion)
- Multi-series lines use
series:parameter or.foregroundStyle(by:) - Axes configured with appropriate labels, ticks, and grid lines
- Scale domain set explicitly when zero-baseline matters
- Pie/donut uses positive values, 5-7 sectors, and "Other" grouping
- Selection binding type matches axis data type (
Date?for date axis) - Pie/donut angle selection maps numeric angle values back to categories
- Scrollable charts set
.chartXVisibleDomain(length:)for viewport - Vectorized plots used for datasets exceeding 1000 points
- KeyPath modifiers applied before value modifiers on vectorized plots
-
Chart3Dused only for real 3D data or surfaces, with z scale and pose reviewed - Accessibility labels added to marks for VoiceOver
- Chart tested with Dynamic Type and Dark Mode
- Legend visible and positioned, or intentionally hidden
- Ensure chart data model types are Sendable; update chart data on @MainActor
References
- Extended patterns: references/charts-patterns.md [blocked]
- Apple docs: Swift Charts
- Apple docs: Creating a chart using Swift Charts
- Apple docs: Swift Charts updates
- Apple docs: Chart3D
- Apple docs: SurfacePlot


