One Logo Pool Ball
Charts · IOS API REFERENCE

RuleMark

struct · iOS 16.0+ · SDK 27.1

@MainActor @preconcurrency struct RuleMark

Chart content that represents data using a single horizontal or vertical rule. You can use `RuleMark` to plot a horizontal or vertical rule in your chart. ### Show Range with Rule Marks To create a horizontal line at a `y` position from `xStart` to `xEnd` you use the ``init(xStart:xEnd:y:)`` or ``init(xStart:xEnd:y:)``. To create a vertical line at an `x` position from `yStart` to `yEnd` you use ``init(x:yStart:yEnd:)`` or ``init(x:yStart:yEnd:)``. The example below uses the `Pollen` structure and the ``init(xStart:xEnd:y:)`` to map horizontal lines that span from the value of the `startDate` to the value of the `endDate` for x positions to a pollen `source` property's y position: ```swift struct Pollen { var source: String var startDate: Date var endDate: Date init(startMonth: Int, numMonths: Int, source: String) { self.source = source let calendar = Calendar.autoupdatingCurrent self.startDate = calendar.date(from: DateComponents(year: 2020, month: startMonth, day: 1))! self.endDate = calendar.date(byAdding: .month, value: numMonths, to: startDate)! } } var data: [Pollen] = [ Pollen(startMonth: 1, numMonths: 9, source: "Trees"), Pollen(startMonth: 12, numMonths: 1, source: "Trees"), Pollen(startMonth: 3, numMonths: 8, source: "Grass"), Pollen(startMonth: 4, numMonths: 8, source: "Weeds") ] var body: some View { Chart(data) { RuleMark( xStart: .value("Start Date", $0.startDate), xEnd: .value("End Date", $0.endDate), y: .value("Pollen Source", $0.source) ) } } ``` ![Horizontal rule chart with x-axis showing the month in the year 2020 starting with January and ending with December, and with y-axis showing a pollen source: Trees, Grass, and Weeds. There are 4 rules. 2 for Trees 1 starting in January and going until the end of September and 1 spanning December, 1 for Grass starting in March and going until the end of August, and 1 for Weeds starting in April and going until the end of November.](LineSegmentMarkSwift.LineSegmentMarkHorizontalLineSegmentChart.png) ### Annotate a chart with rule mark You can annotate a chart with horizontal or vertically spanning rules. This allows viewers to easily compare values over a range to a constant value. Use the ``init(xStart:xEnd:y:)-444cp`` initializer to represent a constant `y` value or ``init(x:yStart:yEnd:)-6zemd`` for a constant `x` value. To span the plotting area of a chart with a line, omit the optional start and end parameters and plot a constant value. The example below results in a line that spans the chart horizontally at the y position of 9000: ```swift struct DepartmentProfit { var department: String var profit: Double } var data: [DepartmentProfit] = [ DepartmentProfit(department: "Production", profit: 15000), DepartmentProfit(department: "Marketing", profit: 8000), DepartmentProfit(department: "Finance", profit: 10000) ] var body: some View { Chart ForEach(data) { BarMark( x: .value("Department", $0.department), y: .value("Profit", $0.profit) ) } RuleMark(y: .value("Break Even Threshold", 9000)) .foregroundStyle(.red) } } ``` ![Vertical bar chart with x-axis showing department categories Production, Marketing, Finance, and R&D, and with y-axis ranging from 0 to 15000. There are 3 bars: Production 15000, Marketing 8000, Finance 10000. A rule mark at 9000 shows the break even threshold.](LineSegmentMarkSwift.LineSegmentMarkBarChartWithHorizontalLineSegmentMark.png) ### RuleMark in Chart3D To plot a rule in a 3D chart, use the ``init(x:y:z:)`` initializer. The rule extends along the axis that you provide a range for, and is positioned at the single points you specify for the other two axes. > Important: A 3D rule mark requires one parameter to be a numeric range and the other two parameters to be single numeric values. For example, the following `Chart3D` shows three rule marks. Each mark extends along one axis and is fixed at `0` on the other two. ```swift Chart3D { // A rule that extends along the x-axis RuleMark( x: .value("x", -0.5..<0.5), y: .value("y", 0), z: .value("z", 0) ) .foregroundStyle(.red) // A rule that extends along the y-axis RuleMark( x: .value("x", 0), y: .value("y", -0.5..<0.5), z: .value("z", 0) ) .foregroundStyle(.green) // A rule that extends along the z-axis RuleMark( x: .value("x", 0), y: .value("y", 0), z: .value("z", -0.5..<0.5) ) .foregroundStyle(.blue) } ```

Members (6)

init · iOS 16.0+

init

TypeScript
static initXYStartYEnd(x: (number) | null, yStart: PlottableValue<number>, yEnd: PlottableValue<number>): Promise<RuleMark>
Swift declaration
nonisolated init<Y>(x: CGFloat? = nil, yStart: PlottableValue<Y>, yEnd: PlottableValue<Y>) where Y : Plottable

Creates a vertical rule mark with a fixed x position and y interval encoding. - Parameters: - x: The x position. If `x` is `nil`, the rule will be centered horizontally by default. - yStart: The value plotted with y start. - yEnd: The value plotted with y end. ### Discussion Use this initializer to create a vertical rule at y positions from `yStart` to `yEnd` for a single x position: ```swift Chart(data) { RuleMark( yStart: .value("Start Date", $0.startDate), yEnd: .value("End Date", $0.endDate) ) } ``` ![Vertical rule chart with y-axis showing the month in the year 2020 starting with January and ending with December, and with x-axis showing a pollen source: Trees. There are 2 rules. 1 starting in January and going until the end of September and 1 spanning December.](LineSegmentMarkSwift.LineSegmentMarkVerticalSingleLineSegmentChart.png) See ``RuleMark`` for the setup of the structure containing the `startDate`, `endDate`, and `source` properties.

init · iOS 16.0+

init

TypeScript
static initXYStartYEnd92156(x: PlottableValue<number>, yStart?: (number) | null, yEnd?: (number) | null): Promise<RuleMark>
Swift declaration
nonisolated init<X>(x: PlottableValue<X>, yStart: CGFloat? = nil, yEnd: CGFloat? = nil) where X : Plottable

Creates a vertical rule mark with value plotted with x. - Parameters: - x: The value plotted with x. - yStart: The y start position. If `yStart` is `nil` the rule will start at the leading edge of the plotting area. - yEnd: The y end position. If `yEnd` is `nil` the rule will end at the trailing edge of the plotting area. ### Discussion Use this initializer to create a vertical rule across a chart's plotting area at an x position: ```swift Chart { ForEach(data) { BarMark( x: .value("Profit", $0.profit), y: .value("Department", $0.department) ) } RuleMark(x: .value("Break Even Threshold", 9000)) .foregroundStyle(.red) } ``` ![Horizontal bar chart with y-axis showing department categories Production, Marketing, Finance, and R&D, and with x-axis ranging from 0 to 15000. There are 3 bars: Production 15000, Marketing 8000, Finance 10000. A vertical rule mark at 9000 shows the break even threshold.](LineSegmentMarkSwift.LineSegmentMarkBarChartWithVerticalLineSegmentMark.png) See the first code example in ``RuleMark`` for the setup of the structure that contains `department` and `profit` properties.

init · iOS 16.0+

init

TypeScript
static initXYStartYEnd29917(x: PlottableValue<number>, yStart: PlottableValue<number>, yEnd: PlottableValue<number>): Promise<RuleMark>
Swift declaration
nonisolated init<X, Y>(x: PlottableValue<X>, yStart: PlottableValue<Y>, yEnd: PlottableValue<Y>) where X : Plottable, Y : Plottable

Creates a vertical rule mark with an x encoding and y interval encoding. - Parameters: - x: The value plotted with x. - yStart: The value plotted with y start. - yEnd: The value plotted with y end. ### Discussion Use this initializer to create a vertical line at y positions from `yStart` to `yEnd` to an x position: ```swift Chart(data) { RuleMark( x: .value("Pollen Source", $0.source), yStart: .value("Start Date", $0.startDate), yEnd: .value("End Date", $0.endDate) ) } ``` ![Vertical rule chart with y-axis showing the month in the year 2020 starting with January and ending with December, and with x-axis showing a pollen source: Trees, Grass, and Weeds. There are 4 rules. 2 for Trees 1 starting in January and going until the end of September and 1 spanning December, 1 for Grass starting in March and going until the end of August, and 1 for Weeds starting in April and going until the end of November.](LineSegmentMarkSwift.LineSegmentMarkVerticalLineSegmentChart.png) See ``RuleMark`` for the setup of the structure that contains the `startDate`, `endDate`, and `source` properties.

init · iOS 16.0+

init

TypeScript
static initXStartXEndY(xStart: (number) | null, xEnd: (number) | null, y: PlottableValue<number>): Promise<RuleMark>
Swift declaration
nonisolated init<Y>(xStart: CGFloat? = nil, xEnd: CGFloat? = nil, y: PlottableValue<Y>) where Y : Plottable

Creates a horizontal rule mark that plots a value on y. - Parameters: - xStart: The x start position. If `xStart` is `nil` the rule will start at the leading edge of the plotting area. - xEnd: The x end position. If `xEnd` is `nil` the rule will end at the trailing edge of the plotting area. - y: The value plotted with y. ### Discussion Use this initializer to create a horizontal rule across a chart's plotting area at a y position: ```swift Chart { ForEach(data) { BarMark( x: .value("Department", $0.department), y: .value("Profit", $0.profit) ) } RuleMark(y: .value("Break Even Threshold", 9000)) .foregroundStyle(.red) } ``` ![Vertical bar chart with x-axis showing department categories Production, Marketing, Finance, and R&D, and with y-axis ranging from 0 to 15000. There are 3 bars: Production 15000, Marketing 8000, Finance 10000. A horizontal rule mark at 9000 shows the break even threshold.](LineSegmentMarkSwift.LineSegmentMarkBarChartWithHorizontalLineSegmentMark.png) See the first code example in ``RuleMark`` for the setup of the structure that contains the `department` and `profit` properties.

init · iOS 16.0+

init

TypeScript
static initXStartXEndY64777(xStart: PlottableValue<number>, xEnd: PlottableValue<number>, y?: (number) | null): Promise<RuleMark>
Swift declaration
nonisolated init<X>(xStart: PlottableValue<X>, xEnd: PlottableValue<X>, y: CGFloat? = nil) where X : Plottable

Creates a horizontal rule mark that plots values on its x interval. - Parameters: - xStart: The value plotted with x start. - xEnd: The value plotted with x end. - y: The y position. If `y` is `nil`, the rule will be centered vertically by default. ### Discussion Use this initializer to create a horizontal rule at x positions from `xStart` to `xEnd` for a single y position: ```swift Chart(data) { RuleMark( xStart: .value("Start Date", $0.startDate), xEnd: .value("End Date", $0.endDate) ) } ``` ![Horizontal rule chart with x-axis showing the month in the year 2020 starting with January and ending with December, and with y-axis showing a pollen source: Trees. There are 2 rules. 1 starting in January and going until the end of September and 1 spanning December.](LineSegmentMarkSwift.LineSegmentMarkHorizontalSingleLineSegmentChart.png) See the second code example in ``RuleMark`` for the setup of the structure that contains the `startDate`, `endDate`, and `source` properties.

init · iOS 16.0+

init

TypeScript
static initXStartXEndY78706(xStart: PlottableValue<number>, xEnd: PlottableValue<number>, y: PlottableValue<number>): Promise<RuleMark>
Swift declaration
nonisolated init<X, Y>(xStart: PlottableValue<X>, xEnd: PlottableValue<X>, y: PlottableValue<Y>) where X : Plottable, Y : Plottable

Creates a horizontal rule mark that plots values on its x interval and on y. - Parameters: - xStart: The value plotted with x start. - xEnd: The value plotted with x end. - y: The value plotted with y. ### Discussion Use this initializer to create a horizontal rule at x positions from `xStart` to `xEnd` to a y position: ```swift Chart(data) { RuleMark( xStart: .value("Start Date", $0.startDate), xEnd: .value("End Date", $0.endDate), y: .value("Pollen Source", $0.source) ) } ``` ![Horizontal rule chart with x-axis showing the month in the year 2020 starting with January and ending with December, and with y-axis showing a pollen source: Trees, Grass, and Weeds. There are 4 rules. 2 for Trees 1 starting in January and going until the end of September and 1 spanning December, 1 for Grass starting in March and going until the end of August, and 1 for Weeds starting in April and going until the end of November.](LineSegmentMarkSwift.LineSegmentMarkHorizontalLineSegmentChart.png) See the second code example in ``RuleMark`` for the setup of the structure that contains the `startDate`, `endDate`, and `source` properties.

Skipped declarations

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

RuleMark.init(x:y:z:)

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

All Charts types →