One Logo Pool Ball
Charts · IOS API REFERENCE

LineMark

struct · iOS 16.0+ · SDK 27.1

@MainActor @preconcurrency struct LineMark

Chart content that represents data using a sequence of connected line segments. You create a line chart by plotting a category or date property, typically with the x position, and plotting a number category, typically with the y position. The example below plots the `date` property to the x position and the `hoursOfSunshine` property to the y position using ``init(x:y:)``: ```swift struct MonthlyHoursOfSunshine { var date: Date var hoursOfSunshine: Double init(month: Int, hoursOfSunshine: Double) { let calendar = Calendar.autoupdatingCurrent self.date = calendar.date(from: DateComponents(year: 2020, month: month))! self.hoursOfSunshine = hoursOfSunshine } } var data: [MonthlyHoursOfSunshine] = [ MonthlyHoursOfSunshine(month: 1, hoursOfSunshine: 74), MonthlyHoursOfSunshine(month: 2, hoursOfSunshine: 99), // ... MonthlyHoursOfSunshine(month: 12, hoursOfSunshine: 62) ] var body: some View { Chart(data) { LineMark( x: .value("Month", $0.date), y: .value("Hours of Sunshine", $0.hoursOfSunshine) ) } } ``` ![Line chart with date on x-axis and hours of sunshine on y-axis. One line showing 12 points representing hours of sunshine in a month 1 74, 2 99, 3 154, 4 201, 5 247, 6 234, 7 304, 8 248, 9 197, 10 122, 11 77, 12 62.](LineMarkSwift.LineMarkLineChart.png) ### Plotting multiple lines You can plot multiple lines in a chart either by explicitly specifying the `series` parameter in ``init(x:y:series:)`` or by applying the ``ChartContent/foregroundStyle(_:)`` or ``ChartContent/lineStyle(_:)`` modifiers. Line marks with the same `series` value will always be rendered together as a single line. When `series` is unspecified line marks with the same value plotted to foreground style and line style will be grouped together and plotted on their own line. The example below plots one line per distinct value in `city` and colors each line based on the `city` it represents: ```swift struct MonthlyHoursOfSunshine { var city: String var date: Date var hoursOfSunshine: Double init(city: String, month: Int, hoursOfSunshine: Double) { let calendar = Calendar.autoupdatingCurrent self.city = city self.date = calendar.date(from: DateComponents(year: 2020, month: month))! self.hoursOfSunshine = hoursOfSunshine } } var data: [MonthlyHoursOfSunshine] = [ MonthlyHoursOfSunshine(city: "Seattle", month: 1, hoursOfSunshine: 74), MonthlyHoursOfSunshine(city: "Cupertino", month: 1, hoursOfSunshine: 196), // ... MonthlyHoursOfSunshine(city: "Seattle", month: 12, hoursOfSunshine: 62), MonthlyHoursOfSunshine(city: "Cupertino", month: 12, hoursOfSunshine: 199) ] var body: some View { Chart(data) { LineMark( x: .value("Month", $0.date), y: .value("Hours of Sunshine", $0.hoursOfSunshine) ) .foregroundStyle(by: .value("City", $0.city)) } } ``` ![Multi-series line chart with date on x-axis and hours of sunshine on y-axis. Two lines showing 12 points representing hours of sunshine in a month for a city. Seattle: 1 74, 2 99, 3 154, 4 201, 5 247, 6 234, 7 304, 8 248, 9 197, 10 122, 11 77, 12 62 and Cupertino: 1 196, 2 204, 3 253, 4 288, 5 311, 6 342, 7 360, 8 331, 9 300, 10 246, 11 212, 12 199.](LineMarkSwift.LineMarkMultiSeriesLineChart.png) > Note: Colors are repeated if the number of series is greater than the total number of colors.

Members (2)

init · iOS 16.0+

init

TypeScript
static initXYSeries(x: PlottableValue<number>, y: PlottableValue<number>, series: PlottableValue<number>): Promise<LineMark>
Swift declaration
nonisolated init<X, Y, S>(x: PlottableValue<X>, y: PlottableValue<Y>, series: PlottableValue<S>) where X : Plottable, Y : Plottable, S : Plottable

Creates a separate line for each unique value of series. - Parameters: - x: The value plotted with x. - y: The value plotted with y. - series: The value with which to split the line series by. ### Discussion Use this initializer to create a multi-line chart from a single data source. ```swift Chart(sunshineData) { LineMark( x: .value("Month", $0.date), y: .value("Hours of Sunshine", $0.hoursOfSunshine) ) .foregroundStyle(by: .value("City", $0.city)) } ``` ![Multi-series line chart with date on x-axis and hours of sunshine on y-axis. Two lines showing 12 points representing hours of sunshine in a month for a city. Seattle: 1 74, 2 99, 3 154, 4 201, 5 247, 6 234, 7 304, 8 248, 9 197, 10 122, 11 77, 12 62 and Cupertino: 1 196, 2 204, 3 253, 4 288, 5 311, 6 342, 7 360, 8 331, 9 300, 10 246, 11 212, 12 199.](LineMarkSwift.LineMarkMultiSeriesLineChart.png)

init · iOS 16.0+

init

TypeScript
static initXY(x: PlottableValue<number>, y: PlottableValue<number>): Promise<LineMark>
Swift declaration
nonisolated init<X, Y>(x: PlottableValue<X>, y: PlottableValue<Y>) where X : Plottable, Y : Plottable

Creates a line mark. - Parameters: - x: The value plotted with x. - y: The value plotted with y. ### Discussion Use this initializer to create a chart with a single line. ```swift Chart(sunshineData) { LineMark( x: .value("Month", $0.date), y: .value("Hours of Sunshine", $0.hoursOfSunshine) ) } ``` ![Line chart with date on x-axis and hours of sunshine on y-axis. One line showing 12 points representing hours of sunshine in a month 1 74, 2 99, 3 154, 4 201, 5 247, 6 234, 7 304, 8 248, 9 197, 10 122, 11 77, 12 62.](LineMarkSwift.LineMarkLineChart.png)

Skipped declarations

No declarations were skipped in this snapshot.

All Charts types →