Contents

ChartContent

A type that represents the content that you draw on a chart.

Declaration

@MainActor @preconcurrency protocol ChartContent

Overview

You build a Chart by adding instances that conform to the ChartContent protocol to the chart’s content closure. The following example adds three explicit BarMark instances to a chart:

Chart {
    BarMark(
        x: .value("Shape Type", "Cube"),
        y: .value("Total Count", 5)
    )
    BarMark(
        x: .value("Shape Type", "Sphere"),
        y: .value("Total Count", 6)
    )
    BarMark(
        x: .value("Shape Type", "Pyramid"),
        y: .value("Total Count", 4)
    )
}

The chart draws marks that correspond to the instances that you specify:

[Image]

You can combine any number of marks or types of marks in a single chart by listing them individually as shown in the above example, wrapping them in a ForEach, or any combination of these. For some mark types, like LineMark, you can group the marks into series using the mark’s series initialization parameter.

Configure chart content

The ChartContent protocol provides a set of modifiers that you use to configure the properties of chart content. These behave like SwiftUI view modifiers, except that they act on chart content rather than views. Any of the types that conform to the protocol can use these modifiers. For example, you can add the foregroundStyle(_:) modifier to the bar representing the number of spheres in the previous example to make it red:

BarMark(
    x: .value("Shape Type", "Sphere"),
    y: .value("Total Count", 6)
)
.foregroundStyle(.red)

[Image]

Topics

Styling marks

Positioning marks

Setting symbol appearance

Encoding data into mark characteristics

Annotating marks

Layering chart content

Masking and clipping

Configuring accessibility

Implementing chart content

Supporting types

See Also

Charts