Contents

PlottableValue

Labeled data that you plot in a chart using marks.

Declaration

struct PlottableValue<Value> where Value : Plottable

Overview

Provide a PlottableValue to a Mark property (e.g., x, y, foregroundStyle) to plot data values with the mark property.

You can use the .value("Category", \.category) shorthand to create a PlottableValue. The example below plots category, value, and group with the bar mark’s x, y, and foregroundStyle.

struct Bar {
    let category: String
    let value: Double
    let group: String
}

let data: [Bar] = [
    Bar(category: "A", value: 20, group: "Group 1"),
    Bar(category: "A", value: 30, group: "Group 2"),
    Bar(category: "A", value: 10, group: "Group 3"),
    Bar(category: "B", value: 40, group: "Group 1"),
    Bar(category: "B", value: 20, group: "Group 2"),
    Bar(category: "B", value: 10, group: "Group 3"),
    //...
]

var body: some View {
    Chart(data) {
        BarMark(
            x: .value("Category", $0.category),
            y: .value("Quantity", $0.value)
        )
        .foregroundStyle(.value("Group", $0.group))
    }
}

Topics

Type Methods

See Also

Labeled data