ChartModel is an ObservableObject that holds all data and configuration for a chart. Pass it to ChartView and any change you make to the model — data, selection, axis attributes, or series colors — immediately re-renders the chart.
Initializers
Two public convenience initializers cover nearly all use cases.Standard 2D init (most chart types)
Use this initializer for line, column, bar, area, combo, stacked column, stacked bar, waterfall, donut, and micro charts.3D init (bubble, scatter, stock)
Use this initializer when each data point has three dimensions — for example[x, y, radius] for a bubble chart.
[x, y, radius] order per data point. The initializer reorders them internally.
Parameters
The chart type to render. See Chart types for the full list of values.
A two-dimensional array structured as series → categories. The outer array contains one element per series; each inner array contains the values for that series, one per category. Use
nil for missing data points — the chart will render a gap.Labels for the category axis. The outer array corresponds to series; in practice you usually supply a single inner array that applies to all series.
Per-category color overrides, keyed by
[seriesIndex: [categoryIndex: Color]]. Overrides the color from seriesAttributes for the specified categories.Axis title strings keyed by
ChartAxisId. Use .x, .y, .category, or .dual.Custom labels rendered on individual data points, in the same series × category layout as
data. Useful for annotating specific values.Number of gridlines drawn on the numeric axis. Constrained to the range 2–20.
Controls which plot items are selected when the user taps.
.single— selects one value in the active series and category..all— selects one value per series for the tapped category..multiple— multiple independent selections (donut charts only; set automatically).
Initial selection state. The dictionary maps
seriesIndex to an array of categoryIndex values.Enables pan, pinch-to-zoom, and drag gestures. Set to
false when using the chart in an iOS Widget.Enables tap and two-finger long-press selection gestures independently of
userInteractionEnabled.Per-series styling — colors, line width, point attributes. See Customizing charts for details.
Style and behavior attributes for the category axis (X axis for most chart types, Y axis for bar charts).
Style and behavior attributes for the primary numeric axis (Y axis for most chart types, X axis for bar charts).
Attributes for the secondary numeric axis. Only applicable to
.line, .area, and .combo chart types.When
true, the chart snaps to the nearest data point while the user drags.Series indexes assigned to the secondary Y axis. Only applies to
.line, .area, and .combo charts.For
.combo charts, the series indexes that render as columns. All other series render as lines.For
.waterfall charts, the category indexes that represent totals. If the corresponding value is nil in data, the chart computes the running total automatically.A closure that formats numeric axis label values. Use this for custom units or precision.
Examples
Line chart with two series
Donut chart
Donut charts use a single series and a single category per segment. TheselectionMode is automatically set to .multiple for donut charts.
Waterfall chart with totals
Observing selection changes
Subscribe toselectionDidChangePublisher using Combine to react to user-driven selection updates.
The
selectionDidChangePublisher publishes only when the user interacts with the chart. Setting model.selections programmatically does not trigger the publisher.