Trendlines in Xamarin.Android Chart

25 Nov 20224 minutes to read

The Trendline is a line drawn over the chart to display the overall direction of the results. And it built on the assumption based on current and past beliefs.

The following code examples shows how to add Trendline in SfChart.

  • C#
  • [C#]
    
    ColumnSeries columnSeries = new ColumnSeries();
    columnSeries.Trendlines = new ChartTrendlineCollection();
    ChartTrendline trendline = new ChartTrendline();
    columnSeries.Trendlines.Add(trendline);

    Types of Trendline

    SfChart support following types of Trendlines.

    Linear

    Linear trendline was best-fit straight line for simple linear datasets. A linear trend line usually shows that something is increasing or decreasing at a steady rate. This is the default trendline to be drawn for the SfChart.

    The following is the code example of linear trendline.

  • C#
  • [C#]
    
    columnSeries.Trendlines = new ChartTrendlineCollection();
    ChartTrendline trendline = new ChartTrendline();
    trendline.Type = ChartTrendlineType.Linear;
    columnSeries.Trendlines.Add(trendline);

    Linear type trendline in Xamarin.Android Chart

    Logarithmic

    A Logarithmic trendline is the strongest-fit curved line, that is most effective when the data change rate increases or decreases rapidly. Logarithmic trends may use negative and/or positive values as well.

    The following is the code example of logarithmic trendline.

  • C#
  • [C#]
    
    ColumnSeries columnSeries = new ColumnSeries();
    columnSeries.Trendlines = new ChartTrendlineCollection();
    ChartTrendline trendline = new ChartTrendline();
    trendline.Type = ChartTrendlineType.Logarithmic;
    columnSeries.Trendlines.Add(trendline);

    Logarithmic type trendline in Xamarin.Android Chart

    Exponential

    The Exponential trendline is the curved line most useful for data values rise or fall at increasingly higher rates.

    NOTE

    SfChart will not generate Exponential trendline when your data contains zero or negative values.

  • C#
  • [C#]
    
    ColumnSeries columnSeries = new ColumnSeries();
    columnSeries.Trendlines = new ChartTrendlineCollection();
    ChartTrendline trendline = new ChartTrendline();
    trendline.Type = ChartTrendlineType.Exponential;
    columnSeries.Trendlines.Add(trendline);

    Exponential type trendline in Xamarin.Android Chart

    Power

    The Power trendline is typically used with data sets to compare measurements that grow at a specific rate.

    The following is the code example of power trendline.

  • C#
  • [C#]
    
    ColumnSeries columnSeries = new ColumnSeries();
    columnSeries.Trendlines = new ChartTrendlineCollection();
    ChartTrendline trendline = new ChartTrendline();
    trendline.Type = ChartTrendlineType.Power;
    columnSeries.Trendlines.Add(trendline);

    Power type trendline in Xamarin.Android Chart

    Polynomial

    The polynomial trendline is a curved line that is used when there are more data fluctuations. By default, this trendline calculated with order of 2, it will be override by the property PolynomialOrder.

    The following is the code example of polynomial trendline.

  • C#
  • [C#]
    
    columnSeries.Trendlines = new ChartTrendlineCollection();
    ChartTrendline trendline = new ChartTrendline();
    trendline.Type = ChartTrendlineType.Polynomial;
    trendline.PolynomialOrder = 3;
    columnSeries.Trendlines.Add(trendline);

    Polynomial type trendline in Xamarin.Android Chart

    Forecasting

    Forecasting is used to display trends about the future and the past beliefs.

    The following two types of forecasting are available in SfChart:

    • Forward Forecasting
    • Backward Forecasting

    Forward Forecasting

    For determining the future trends (in forward direction). The
    following code example explains the how to set the value for ForwardForecast.

  • C#
  • [C#]
    
    columnSeries.Trendlines = new ChartTrendlineCollection();
    ChartTrendline trendline = new ChartTrendline();
    trendline.Type = ChartTrendlineType.Linear;
    trendline.ForwardForecast = 2;
    columnSeries.Trendlines.Add(trendline);

    ForwardForecast in trendline Xamarin.Android Chart

    Backward Forecasting

    For determining the future trends (in backward direction). The following code example explains the how to set the value for BackwardForecast.

  • C#
  • [C#]
    
    columnSeries.Trendlines = new ChartTrendlineCollection();
    ChartTrendline trendline = new ChartTrendline();
    trendline.Type = ChartTrendlineType.Linear;
    trendline.BackwardForecast = 2;
    columnSeries.Trendlines.Add(trendline);

    BackwardForecast in trendline Xamarin.Android Chart

    Customization

    We can customize the trendline appearance using StrokeWidth, StrokeColor and PathEffect properties.

  • C#
  • [C#]
    
    trendline.StrokeColor = Color.Black;
    trendline.StrokeWidth = 2;
    trendline.PathEffect = new DashPathEffect(new float[] { 5, 6 }, 2);

    Trendlines customization in Xamarin.Android Chart

    Legend Item Visibility

    We can able to control the visibility of the trendline legend items using VisibilityOnLegend property of the Trendline.

  • C#
  • [C#]
    
    trendline.VisibilityOnLegend = Visibility.Visible;