Contents

LinePlot

Chart content that represents a function or a collection of data using a sequence of connected line segments.

Declaration

struct LinePlot<Content>

Overview

Use LinePlot when you want to visualize data in the same way as with LineMark, but you want to plot a function or visualize an entire data collection with a single plot.

Plotting lines from a collection

You can initialize and style the plot with simple values or key paths. Add modifiers with KeyPath before adding modifiers with simple values.

Chart {
    LinePlot(
        stocks,
        x: .value("Date", \.date),
        y: .value("Price", \.price),
        series: .value("Asset", \.symbol)
    )
    .foregroundStyle(by: .value("Asset", \.symbol))
}

Plotting functions

In addition to providing data points, you can provide a function to a LinePlot to plot a function. For example, you can plot the function y = x^2 with:

Chart {
    LinePlot(x: "x", y: "y") { x in x * x }
}
.chartXScale(domain: -10 ... 10)
.chartYScale(domain: -10 ... 10)

You can add multiple function plots in a chart and use different foreground styles to distinguish among them.

Chart {
    LinePlot(x: "x", y: "y = sin(x)") { sin($0) }
        .foregroundStyle(by: .value("expression", "y=sin(x)"))
        .lineStyle(StrokeStyle(lineWidth: 5, lineCap: .round))
        .opacity(0.8)

    LinePlot(x: "x", y: "y = cos(x)") { cos($0) }
        .foregroundStyle(by: .value("expression", "y=cos(x)"))
        .lineStyle(StrokeStyle(lineWidth: 5, lineCap: .round))
        .opacity(0.8)
}
.chartXScale(domain: -10 ... 10)
.chartYScale(domain: -10 ... 10)

You can plot a parametric function with the constructor with x, y, and t:

Chart {
    LinePlot(x: "x", y: "y", t: "t", domain: 0 ... .pi * 2) {
        t in (x: 10 * cos(5 * t) * cos(t), y: 10 * cos(5 * t) * sin(t))
    }
}
.chartXScale(domain: -10 ... 10)
.chartYScale(domain: -10 ... 10)

Topics

Plotting lines from a collection

Plotting functions

Plotting parametric functions

Supporting types

See Also

Vectorized plots