<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: 16.0.0 -",
    "macOS: 13.0.0 -",
    "tvOS: 16.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 9.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Charts",
  "identifier" : "/documentation/Charts/RuleMark",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Swift Charts"
    ],
    "preciseIdentifier" : "s:6Charts8RuleMarkV"
  },
  "title" : "RuleMark"
}
-->

# RuleMark

Chart content that represents data using a single horizontal or vertical rule.

```
@MainActor @preconcurrency struct RuleMark
```

## Overview

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.](images/com.apple.Charts/LineSegmentMarkSwift.LineSegmentMarkHorizontalLineSegmentChart@2x.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:)`](/documentation/Charts/RuleMark/init(xStart:xEnd:y:)-444cp) initializer to represent a constant `y` value or [`init(x:yStart:yEnd:)`](/documentation/Charts/RuleMark/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.](images/com.apple.Charts/LineSegmentMarkSwift.LineSegmentMarkBarChartWithHorizontalLineSegmentMark@2x.png)

### RuleMark in Chart3D

To plot a rule in a 3D chart, use the [`init(x:y:z:)`](/documentation/Charts/RuleMark/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)
}
```

## Relationships

### Conforms To

[`Sendable`](/documentation/Swift/Sendable)

[`ChartContent`](/documentation/Charts/ChartContent)

[`Copyable`](/documentation/Swift/Copyable)

[`Chart3DContent`](/documentation/Charts/Chart3DContent)

[`Escapable`](/documentation/Swift/Escapable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)