Skip to main content

Trendline chart

info

Charts are available with Avalonia Pro.

Trendlines are used in Cartesian charts to show the general direction or pattern of data. They filter out noise to highlight underlying trends. They can fit linear, polynomial, power, exponential, logarithmic, or moving average data.

Line chart with an overlaid trendline showing a line of best fit.

When to use

  • Sales forecasting: Projecting future sales based on historical trends.
  • Data smoothing: Identifying patterns in volatile stock or sensor data.
  • Performance evaluation: Visualizing if throughput is generally increasing or decreasing.

Code example

XAML

<charts:CartesianChart Title="Revenue Growth" Height="300">
<charts:CartesianChart.HorizontalAxis>
<charts:CategoryAxis Title="Year" />
</charts:CartesianChart.HorizontalAxis>
<charts:CartesianChart.VerticalAxis>
<charts:NumericalAxis Title="Revenue (M)" Minimum="0" />
</charts:CartesianChart.VerticalAxis>
<charts:CartesianChart.Series>
<charts:LineSeries Title="Revenue"
ItemsSource="{Binding LinearData}"
CategoryPath="Category"
ValuePath="Value"
StrokeThickness="2"
ShowMarkers="True">
<charts:LineSeries.Trendlines>
<charts:Trendline Type="Linear"
Stroke="#E53935"
StrokeThickness="2"
ForwardForecast="1" />
</charts:LineSeries.Trendlines>
</charts:LineSeries>
</charts:CartesianChart.Series>
</charts:CartesianChart>

Data model (C#)

public record ChartDataPoint(string Category, double Value);

public ObservableCollection<ChartDataPoint> LinearData { get; } = new()
{
new("2018", 12),
new("2019", 15),
new("2020", 18),
new("2021", 20),
new("2022", 25)
};

Common properties (Trendline)

PropertyDescriptionDefault
TypeData type the trendline should fit: Linear, Exponential, Logarithmic, Power, Polynomial, or MovingAverage.Linear
StrokeBrush used to draw the trendline.Gray
StrokeThicknessThickness of the trendline stroke.2.0
StrokeDashStyleDash style used for the trendline stroke.null
StrokeLineCapLine cap used at the trendline ends.Round
StrokeLineJoinLine join used where trendline segments meet.Round
IsVisibleWhether the trendline is rendered.true
ForwardForecastNumber of units to project forward.0
BackwardForecastNumber of units to project backward.0
PeriodFor MovingAverage, the number of points to average.2
PolynomialOrderPolynomial degree when Type is Polynomial.2

ChartTrendlineSeries

ChartTrendlineSeries is a standalone series that renders a trendline overlay using regression calculations. Unlike the Trendline attached property, it is added directly to the chart's Series collection and can reference another series via SourceSeries or use its own ItemsSource.

XAML

<charts:CartesianChart Title="Explicit Overlay Series" Height="320" ShowLegend="True">
<charts:CartesianChart.HorizontalAxis>
<charts:CategoryAxis Title="Index" />
</charts:CartesianChart.HorizontalAxis>
<charts:CartesianChart.VerticalAxis>
<charts:NumericalAxis Title="Value" Minimum="0" />
</charts:CartesianChart.VerticalAxis>

<charts:CartesianChart.Series>
<charts:ScatterSeries x:Name="StandaloneTrendlineSource"
Title="Observed Data"
ItemsSource="{Binding StandaloneTrendlineData}"
CategoryPath="Category"
ValuePath="Value"
Fill="#455A64"
MarkerSize="8" />
<charts:ChartTrendlineSeries Title="Polynomial Overlay"
SourceSeries="{Binding #StandaloneTrendlineSource}"
TrendlineType="Polynomial"
PolynomialOrder="3"
Stroke="#D32F2F"
StrokeThickness="2.5"
Extend="0.15" />
</charts:CartesianChart.Series>
</charts:CartesianChart>

Data model (C#)

public record ChartDataPoint(string Category, double Value);

public ObservableCollection<ChartDataPoint> StandaloneTrendlineData { get; } = new()
{
new("1", 16),
new("2", 20),
new("3", 27),
new("4", 29),
new("5", 38),
new("6", 43),
new("7", 47),
new("8", 56)
};

ChartTrendlineSeries properties

PropertyDescriptionDefault
TrendlineTypeThe regression type: Linear, Polynomial, Exponential, Logarithmic, Power, or MovingAverage.Linear
PolynomialOrderThe polynomial degree when TrendlineType is Polynomial.2
PeriodNumber of points used when TrendlineType is MovingAverage.2
SourceSeriesThe series to calculate the trendline from. If null, uses ItemsSource directly.null
ExtendHow far to extend the trendline beyond the data range, as a fraction (e.g., 0.1 = 10%).0

MovingAverageSeries

MovingAverageSeries displays a moving average overlay for financial or time-series data. It supports Simple (SMA), Exponential (EMA), Weighted (WMA), and Triangular (TMA) moving average calculations. It can be used in CartesianChart, or in FinancialChart as an overlay that follows the financial chart's date and price coordinates.

XAML

<charts:CartesianChart Title="Price with Moving Average" Height="300">
<charts:CartesianChart.Series>
<charts:LineSeries x:Name="PriceSeries"
ItemsSource="{Binding PriceData}"
CategoryPath="Date"
ValuePath="Close" />
<charts:MovingAverageSeries Title="SMA (14)"
SourceSeries="{Binding #PriceSeries}"
MovingAverageType="Simple"
Period="14"
Stroke="Orange"
StrokeThickness="2" />
</charts:CartesianChart.Series>
</charts:CartesianChart>

Data model (C#)

using System;

public record PricePoint(DateTime Date, double Close);

public ObservableCollection<PricePoint> PriceData { get; } = new()
{
new(new DateTime(2026, 1, 1), 100),
new(new DateTime(2026, 1, 2), 104),
new(new DateTime(2026, 1, 3), 101),
new(new DateTime(2026, 1, 4), 108),
new(new DateTime(2026, 1, 5), 112)
};

MovingAverageSeries properties

PropertyDescriptionDefault
MovingAverageTypeThe calculation type: Simple, Exponential, Weighted, or Triangular.Simple
PeriodThe number of data points used in the moving average window.14
SourceSeriesThe series to calculate the moving average from. If null, uses ItemsSource directly.null

See also