Trendlines

28 Feb 20227 minutes to read

Trendlines are used to show the direction and speed of price.

Stock Chart supports 6 types of trendlines namely Linear,Exponential,Logarithmic,Polynomial,Power,Moving Average. By using trendline dropdown button you can add or remove the required trendline type.

Linear

A linear trendline is a best fit straight line that is used with simpler data sets.To render a linear trendline, use trendline Type as Linear.

Exponential

An exponential trendline is a curved line that is most useful when data values rise or fall at increasingly higher rates. You cannot create an exponential trendline, if your data contains zero or negative values.
To render a exponential trendline, use trendline Type as Exponential.

Logarithmic

A logarithmic trendline is a best-fit curved line that is most useful when the rate of change in the data increases or decreases quickly and then levels out. A logarithmic trendline can use negative and/or positive values.
To render a logarithmic trendline, use trendline Type as Logarithmic.

Polynomial

A polynomial trendline is a curved line that is used when data fluctuates.
To render a polynomial trendline, use trendline Type as Polynomial.
PolynomialOrder used to define the polynomial value.

Power

A power trendline is a curved line that is best used with data sets that compare measurements that increase at a specific rate.
To render a power trendline, use trendline Type as Power.

Moving Average

A moving average trendline smoothen out fluctuations in data to show a pattern or trend more clearly.
To render a moving average trendline, use trendline Type as MovingAverage.

Period property defines the period to find the moving average.

<script src="~/financial-data.js"></script>

            @(Html.EJS().StockChart("container").Load("stockload").Title("AAPL Stock Price").IndicatorType(new List<Object>() { }).ExportType(new List<Object>() { }).SeriesType(new List<Object>() { })
                                                                .Series(sr =>
                                                                {
                                                                    sr.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Candle).DataSource("data").Add();
                                                                })
                                                                .Render())
    <script>
        var data = window.chartData;
        function stockload(args) {
            args.stockChart.series[0].trendlines = [{ type: 'MovingAverage', enableTooltip: false }]

        }
        
   </script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace EJ2CoreSampleBrowser.Controllers.StockChart
{
    public partial class StockChartController : Controller
    {
        public IActionResult Default()
        {
            return View();
        }
    }
}

Customization of Trendline

The Fill and Width properties are used to customize the appearance of the trendline.

<script src="~/financial-data.js"></script>

            @(Html.EJS().StockChart("container").Title("AAPL Stock Price").IndicatorType(new List<Object>() { }).ExportType(new List<Object>() { }).SeriesType(new List<Object>() { })
                                                                .Series(sr =>
                                                                {
                                                                    sr.Type(Syncfusion.EJ2.Charts.ChartSeriesType.Candle).DataSource("data").Trendlines(tr => tr.Type(Syncfusion.EJ2.Charts.TrendlineTypes.MovingAverage).EnableTooltip(false).Fill("red").Width(2)).Add();
         })
        .Crosshair(cr=>cr.Enable(true).Line(line=>line.Color("green").Width(2)))
                                                                })
                                                                .Render())
                                                                
    <script>
        var data = window.chartData;
    </script>
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace EJ2CoreSampleBrowser.Controllers.StockChart
{
    public partial class StockChartController : Controller
    {
        public IActionResult Default()
        {
            return View();
        }
    }
}