Plottable
A type that can serve as data to plot in a chart.
Declaration
protocol PlottableOverview
You create PlottableValue items from data that conforms to Plottable using a method like value(_:_:). You then use those items as the values in a chart, like for the BarMark chart in the following example:
BarMark(
x: .value("Category", "A")
y: .value("Value", 100)
)
.foregroundStyle(by: .value("Series", "Series 1"))You can create a custom plottable type by conforming it to this protocol. For example:
// Make `SomeValue` conform to `Plottable` and act as a categorical value in Swift Charts.
struct SomeValue: Plottable {
var primitivePlottable: String { ... }
init?(primitivePlottable: String) { ... }
}In addition, you can make an enum work as a categorical data value by using String as its raw value and conforming the type to Plottable. The string values will be used as localized string keys when the categories are displayed as text in a chart (for example, on an axis).
enum Status: String, Plottable {
case active = "Active"
case inactive = "Inactive"
}