One Logo Pool Ball
Charts · IOS API REFERENCE

Chart

struct · iOS 16.0+ · SDK 27.1

@MainActor @preconcurrency struct Chart<Content> where Content : ChartContent

A SwiftUI view that displays a chart. To create a chart, instantiate a `Chart` structure with marks that display the properties of your data. For example, suppose you have an array of `ValuePerCategory` structures that define data points composed of a `category` and a `value`: struct ValuePerCategory { var category: String var value: Double } let data: [ValuePerCategory] = [ .init(category: "A", value: 5), .init(category: "B", value: 9), .init(category: "C", value: 7) ] You can use ``BarMark`` inside a chart to represent the `category` property as different bars in the chart and the `value` property as the y value for each bar: Chart(data, id: \.category) { item in BarMark( x: .value("Category", item.category), y: .value("Value", item.value) ) } This chart initializer behaves a lot like a SwiftUI <doc://com.apple.documentation/documentation/SwiftUI/ForEach>, creating a mark --- in this case, a bar --- for each of the values in the `data` array: ![A bar chart with three categories A, B, and C on the x-axis and a range of 0 to 10 on the y-axis. A bar appears in each category. All the bars are the same color. The first has a value of 5. The second has a value of 9. The third has a value of 7.](chart-1-macOS) ### Controlling data series inside a chart You can compose more sophisticated charts by providing more than one series of marks to the chart. For example, suppose you have profit data for two companies: struct ProfitOverTime { var date: Date var profit: Double } let departmentAProfit: [ProfitOverTime] = <#Profit array A#> let departmentBProfit: [ProfitOverTime] = <#Profit array B#> The following chart creates two different series of ``LineMark`` instances with different colors to represent the data for each company. In effect, it moves the `ForEach` construct from the chart's initializer into the body of the chart, enabling you to represent multiple different series: Chart { ForEach(departmentAProfit, id: \.date) { item in LineMark( x: .value("Date", item.date), y: .value("Profit A", item.profit), series: .value("Company", "A") ) .foregroundStyle(.blue) } ForEach(departmentBProfit, id: \.date) { item in LineMark( x: .value("Date", item.date), y: .value("Profit B", item.profit), series: .value("Company", "B") ) .foregroundStyle(.green) } RuleMark( y: .value("Threshold", 400) ) .foregroundStyle(.red) } You indicate which series a line mark belongs to by specifying its `series` input parameter. The above chart also uses a ``RuleMark`` to produce a horizontal line segment that displays a constant threshold value across the width of the chart: ![A line chart with three lines. Dates appear on the x-axis, spanning one full month, and company profit appears on the y-axis in the range of 0 to 750. Two of the lines trend upward from left to right, with occasional downward movement. The third line is purely horizontal at the level of 400.](chart-2-macOS)

Members (0)

This type has no directly callable members in this snapshot. See the framework’s skipped declarations for unsupported APIs.

Skipped declarations

4 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.

Chart.init(content:)

This declaration needs Swift generic type arguments. (no canonical witness for Content)

Chart.init(_:content:)

This declaration needs Swift generic type arguments. (no canonical witness for Data)

Chart.init(_:id:content:)

This declaration needs Swift generic type arguments. (no canonical witness for Data)

Chart.body

This protocol needs associated types that cannot yet cross the bridge.

All Charts types →