Features / Charts / Add Trendlines To Charts
In This Topic
In This Topic

### What is a Trendline?

A trendline, also called as a line of best fit, is an additional line in a chart that indicates the slope (trend) in a particular data series to help users in quickly analyzing the overall trend (even when there are ups and downs in your data points).

### Usage Scenario

Adding trendlines to charts can prove to be a great analytical tool for your organization that you can use to show data movements over a period of time or simply depict correlation between two variables.

• Deriving trends and predicting the future values.
• Analyzing stock, trading, financial and sales data.
• Assisting in trend analysis for creating a data analytics strategy.

Example: The following screenshot depicts the annual sales record for three different products- Mobile Phones, Laptops, and Tablets, via using different types of trendlines in the chart. ### Types of Trendlines

The following table describes the various types of the trendlines supported while working with charts in SpreadJS.

Trendline Type Description Example

Linear Trendline

A linear trendline is a best fit straight line that shows how values in a data series increase or decrease at a steady rate and provides future approximations for the data in the chart. The data is linear, if the data pattern resembles a line.

Equation -:  y=mx + b

where m is the slope and b is the intercept. Exponential Trendline

An exponential trendline is a best-fit curved line that is most useful when data values rise(increase) or fall(decrease) at increasingly higher rates, and then level out.

Equation -: y=cebx

where c and b are constants and e is the base of the natural logarithm.

Note: This trendline can only be used with positive numbers. Users cannot create an exponential trendline if the data contains zero or negative values. Logarithmic Trendline

A logarithmic trendline is a best-fit curved line that illustrates how the data values increase or decrease quickly and then level out. This type of trendline can be used with both positive and negative data values.

Equation-: y=c ln x + b

where c and b are constants and ln is the natural logarithm function. Polynomial Trendline

A polynomial trendline is a curved line that illustrates fluctuations in the data values. This type of trendline is based upon the order property and helps users in analyzing the gains or losses over large data sets (for example- stock, trading and finance applications).

Equation-: y=b + c1x + c2x + c3x + ...

where c1, c2, c3, are constants.

For this trendline, the default value is 2. The order should be a positive integer within the range 2-6. If the order value lies outside the range, then the result would be as described in the table below:

 Order Values of Polynomial Description Value <2 (Less than 2) Will be treated as 2. Value >6 (greater than 6) Will be treated as 6. Decimal value Users need to use Math.floor convert the decimal value to integer. Other types of values The trendline will not be displayed. Power Trendline

A power trendline is a curved line that can be used to compare measurements which increase at a specific rate.

Note - This trendline can only be used with positive numbers. Users cannot create a power trendline if the data contains zero or negative values.

Equation-: y=cxb

where c and b are constants. Moving Average Trendline

A moving average trendline is a trend line that reduces the fluctuations in the trend line to a show smoother pattern. This type of trendline is based upon the period property and is often used to show the trend by considering an average of the specified number of periods.

Equation-: Pm = Pm-1 + (ym - ym - n) / n

The default value of this trendline is 2. The average of the second and third data points is used as the second point in the trendline, and so on. The max period is the length of data set.

For example - if there are 10 data sets in the plot, then the max period is 9, so if the length of data set less than 3, the trendline of Moving Average Series will not be displayed.

The period should be a positive integer of 2-max period. If it is not, then the result would be as described in the table below:

 Period Values Description Value <2 (Less than 2) Will be treated as 2. Value > max period (greater than max period) Will be treated as max period Decimal value Users need to use Math.floor to convert the decimal value to integer Other types of values Trendline will not be displayed Apart from the above six types of trendlines, SpreadJS also provides support for custom names for trendlines. If a user doesn't set the trendline name, a built-in name will be given. The template should be something like this:

• For Moving Average Trendline: '{period number}period MovingAverage({series name})'. Example: '3period MovingAverage(Bears)'.
• For other types of Trendlines: '{trendline type}({series name})'. Example: 'Logarithmic(Bears)'.

### Supported Chart Types

The following chart types are supported while adding trendlines in charts-:

• Column Charts
• Bar Charts
• Line Charts
• Scatter Charts
• Area Charts

Note: The following limitation must be kept in mind while adding trendlines to the charts:

•  Trendlines cannot be added to stacked chart types, like StackedColumn chart. If users try to add or change an existing chart type to the chart types that don't support trendlines, then the trendline will not be displayed.

### Using Code

The following example code depicts the annual sales records using different trendlines in the chart.

JavaScript
Copy Code
```var activeSheet = spread.getActiveSheet();
activeSheet.suspendPaint();
// Prepare data for chart
activeSheet.setValue(0, 1, "Y-2016");
activeSheet.setValue(0, 2, "Y-2017");
activeSheet.setValue(0, 3, "Y-2018");
activeSheet.setValue(1, 0, "Mobile Phones");
activeSheet.setValue(2, 0, "Laptops");
activeSheet.setValue(3, 0, "Tablets");
for (var r = 1; r <= 3; r++)
{
for (var c = 1; c <= 3; c++) {
activeSheet.setValue(r, c, parseInt(Math.random() * 5000));
}
}

// Adding TrendLines on series i.e. Mobile Phones
var series1 = chart_columnClustered.series().get(0);
series1.trendlines = [
{
type: 2,
forward: 2,
name: "MovingAverage",
style: { color: "blue"
},
period: 3 // only used for Moving Average Trendline
}];
// Adding TrendLines on series i.e. Laptops
var series2 = chart_columnClustered.series().get(1);
series2.trendlines = [
{
type: 2,
forward: 2,
name: "MovingAverage",
style:
{
color: "red"
},
period: 3
}];
// Adding TrendLines on series i.e. Tablets
var series3 = chart_columnClustered.series().get(2);
series3.trendlines =
[
{
type: 2,
forward: 2,
name: "MovingAverage",
style: {color: "green"
},
period: 3
}];
chart_columnClustered.series().set(0, series1);
chart_columnClustered.series().set(1, series2);
chart_columnClustered.series().set(2, series3);
// Configure Chart Title
var title = chart_columnClustered.title();
title.text = "Annual Sales Record";
title.fontFamily = "Cambria";
title.fontSize = 28;
title.color = "Red";
chart_columnClustered.title(title);
activeSheet.resumePaint();
```