Trendlines in JavaScript Charts
Trendlines help identify patterns, direction, and overall trends in numerical data. They project the general movement of data values and are widely used in analytics, forecasting, and financial charts. Trendlines can be added to Cartesian series types such as Line, Column, Scatter, Area, Candle, and Hilo (excluding bar series). Multiple trendlines can be added to a single series based on the analysis requirements.
The Chart supports six trendline types: Linear, Exponential, Logarithmic, Polynomial, Power, and Moving Average.
Trendline types
-
Linear: Displays a straight best-fit line for data with a constant rate of change. -
Exponential: Displays accelerating growth or decay and requires positive Y values. -
Logarithmic: Displays rapid early change that gradually levels out and requires positive X values. -
Polynomial: Displays a curved fit for data with multiple changes in direction. -
Power: Displays a power-law relationship and requires positive X and Y values. -
MovingAverage: Smooths short-term fluctuations using a configurable period.
Add trendline objects to the trendlines collection of a supported Cartesian series. Supported series include Line, Column, Scatter, Area, Candle, and Hilo; bar series are not supported. Multiple trendlines can be configured for a single series.
Linear
A linear trendline is a straight, best-fit line used to describe data with a constant rate of increase or decrease. Set the trendline type to Linear.
var data = [];
var yValue = [7.66, 8.03, 8.41, 8.97, 8.77, 8.20, 8.16, 7.89, 8.68, 9.48, 10.11, 11.36, 12.34, 12.60, 12.95, 13.91, 16.21, 17.50, 22.72, 28.14, 31.26, 31.39, 32.43, 35.52, 36.36,
41.33, 43.12, 45.00, 47.23, 48.62, 46.60, 45.28, 44.01, 45.17, 41.20, 43.41, 48.32, 45.65, 46.61, 53.34, 58.53];
var point;
var i; var j = 0;
for (i = 1973; i <= 2013; i++) {
point = { x: i, y: yValue[j] };
data.push(point);
j++;
}
var chart = new ej.charts.Chart({
primaryXAxis: {
title: 'Months',
},
primaryYAxis: {
title: 'Rupees against Dollars',
interval: 5
},
tooltip:{enable:true},
chartArea: { border: { width: 0 } },
series: [{
dataSource: data,
xName: 'x', yName: 'y',
name: 'Apple Inc',
fill: '#0066FF',
//Series type as scatter
type: 'Scatter',
trendlines: [{ type: 'Linear' }]
}],
title: 'Historical Indian Rupee Rate (INR USD)'
}, '#element');<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Animation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>Exponential
An exponential trendline displays a curved pattern useful when data rises or falls at increasing rates. Exponential trendlines cannot be generated if the dataset includes zero or negative values.
Set the trendline type to Exponential.
var data = [];
var yValue = [7.66, 8.03, 8.41, 8.97, 8.77, 8.20, 8.16, 7.89, 8.68, 9.48, 10.11, 11.36, 12.34, 12.60, 12.95, 13.91, 16.21, 17.50, 22.72, 28.14, 31.26, 31.39, 32.43, 35.52, 36.36,
41.33, 43.12, 45.00, 47.23, 48.62, 46.60, 45.28, 44.01, 45.17, 41.20, 43.41, 48.32, 45.65, 46.61, 53.34, 58.53];
var point;
var i; var j = 0;
for (i = 1973; i <= 2013; i++) {
point = { x: i, y: yValue[j] };
data.push(point);
j++;
}
var chart = new ej.charts.Chart({
primaryXAxis: {
title: 'Months',
},
primaryYAxis: {
title: 'Rupees against Dollars',
interval: 5
},
tooltip:{enable:true},
chartArea: { border: { width: 0 } },
series: [{
dataSource: data,
xName: 'x', yName: 'y',
name: 'Apple Inc',
fill: '#0066FF',
type: 'Scatter',
trendlines: [
{ type: 'Exponential',enableTooltip:true,marker:{visible:true} }
]
}],
title: 'Historical Indian Rupee Rate (INR USD)'
}, '#element');<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Animation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>Logarithmic
A logarithmic trendline is a best-fit curved line suitable when the data increases or decreases quickly and then stabilizes. The X values used by the logarithmic calculation must be greater than zero; Y values can be positive or negative.
Set the trendline type to Logarithmic.
var data = [];
var yValue = [7.66, 8.03, 8.41, 8.97, 8.77, 8.20, 8.16, 7.89, 8.68, 9.48, 10.11, 11.36, 12.34, 12.60, 12.95, 13.91, 16.21, 17.50, 22.72, 28.14, 31.26, 31.39, 32.43, 35.52, 36.36,
41.33, 43.12, 45.00, 47.23, 48.62, 46.60, 45.28, 44.01, 45.17, 41.20, 43.41, 48.32, 45.65, 46.61, 53.34, 58.53];
var point;
var i; var j = 0;
for (i = 1973; i <= 2013; i++) {
point = { x: i, y: yValue[j] };
data.push(point);
j++;
}
var chart = new ej.charts.Chart({
primaryXAxis: {
title: 'Months',
},
primaryYAxis: {
title: 'Rupees against Dollars',
interval: 5
},
tooltip:{enable:true},
chartArea: { border: { width: 0 } },
series: [{
dataSource: data,
xName: 'x', yName: 'y',
name: 'Apple Inc',
fill: '#0066FF',
//Series type as scatter
type: 'Scatter',
trendlines: [{ type: 'Logarithmic' }]
}],
title: 'Historical Indian Rupee Rate (INR USD)'
}, '#element');<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Animation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>Polynomial
A polynomial trendline is useful when data fluctuates. It uses a curved line that can model more complex datasets.
Set the trendline type to Polynomial. Use polynomialOrder to define the degree of the polynomial. Verify the supported order range and default value against the installed Chart version before changing this property.
var data = [];
var yValue = [7.66, 8.03, 8.41, 8.97, 8.77, 8.20, 8.16, 7.89, 8.68, 9.48, 10.11, 11.36, 12.34, 12.60, 12.95, 13.91, 16.21, 17.50, 22.72, 28.14, 31.26, 31.39, 32.43, 35.52, 36.36,
41.33, 43.12, 45.00, 47.23, 48.62, 46.60, 45.28, 44.01, 45.17, 41.20, 43.41, 48.32, 45.65, 46.61, 53.34, 58.53];
var point;
var i; var j = 0;
for (i = 1973; i <= 2013; i++) {
point = { x: i, y: yValue[j] };
data.push(point);
j++;
}
var chart = new ej.charts.Chart({
primaryXAxis: {
title: 'Months',
},
primaryYAxis: {
title: 'Rupees against Dollars',
interval: 5
},
tooltip:{enable:true},
chartArea: { border: { width: 0 } },
series: [{
dataSource: data,
xName: 'x', yName: 'y',
name: 'Apple Inc',
fill: '#0066FF',
//Series type as scatter
type: 'Scatter',
trendlines: [{ type: 'Polynomial' }]
}],
title: 'Historical Indian Rupee Rate (INR USD)'
}, '#element');<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Animation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>Power
A power trendline is ideal for datasets where measurements increase at a constant rate. It displays a curved line that best fits exponential growth or decay patterns.
Set the trendline type to Power.
var powerData= [
{ x: 1, y: 10 }, { x: 2, y: 50 },{ x: 3, y: 80 }, { x: 4, y: 110 },
{ x: 5, y: 180 }, { x: 6, y: 220 }, { x: 7, y: 300 }, { x: 8, y: 370 }, { x: 9, y: 490 }, { x: 10, y: 500 }
];
var chart = new ej.charts.Chart({
primaryXAxis: {
title: 'Months',
},
primaryYAxis: {
title: 'Rupees against Dollars',
interval: 50
},
tooltip:{enable:true},
chartArea: { border: { width: 0 } },
series: [{
dataSource: powerData,
xName: 'x', yName: 'y',
name: 'Apple Inc',
fill: '#0066FF',
//Series type as scatter
type: 'Scatter',
trendlines: [{ type: 'Power' }]
}],
title: 'Historical Indian Rupee Rate (INR USD)'
}, '#element');<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Animation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>Moving average
A moving average trendline smooths short-term fluctuations to reveal the underlying trend. The period property specifies the number of data points used to calculate each average.
Set the trendline type to MovingAverage.
var data = [];
var yValue = [7.66, 8.03, 8.41, 8.97, 8.77, 8.20, 8.16, 7.89, 8.68, 9.48, 10.11, 11.36, 12.34, 12.60, 12.95, 13.91, 16.21, 17.50, 22.72, 28.14, 31.26, 31.39, 32.43, 35.52, 36.36,
41.33, 43.12, 45.00, 47.23, 48.62, 46.60, 45.28, 44.01, 45.17, 41.20, 43.41, 48.32, 45.65, 46.61, 53.34, 58.53];
var point;
var i; var j = 0;
for (i = 1973; i <= 2013; i++) {
point = { x: i, y: yValue[j] };
data.push(point);
j++;
}
var chart = new ej.charts.Chart({
primaryXAxis: {
title: 'Months',
},
primaryYAxis: {
title: 'Rupees against Dollars',
interval: 5
},
tooltip:{enable:true},
chartArea: { border: { width: 0 } },
series: [{
dataSource: data,
xName: 'x', yName: 'y',
name: 'Apple Inc',
fill: '#0066FF',
//Series type as scatter
type: 'Scatter',
trendlines: [{ type: 'MovingAverage' }]
}],
title: 'Historical Indian Rupee Rate (INR USD)'
}, '#element');<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Animation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>Customize a trendline
Customize trendline appearance using fill for color, width for line thickness, and dashArray for the dash pattern.
var data = [];
var yValue = [7.66, 8.03, 8.41, 8.97, 8.77, 8.20, 8.16, 7.89, 8.68, 9.48, 10.11, 11.36, 12.34, 12.60, 12.95, 13.91, 16.21, 17.50, 22.72, 28.14, 31.26, 31.39, 32.43, 35.52, 36.36,
41.33, 43.12, 45.00, 47.23, 48.62, 46.60, 45.28, 44.01, 45.17, 41.20, 43.41, 48.32, 45.65, 46.61, 53.34, 58.53];
var point;
var i; var j = 0;
for (i = 1973; i <= 2013; i++) {
point = { x: i, y: yValue[j] };
data.push(point);
j++;
}
var chart = new ej.charts.Chart({
primaryXAxis: {
title: 'Months',
},
primaryYAxis: {
title: 'Rupees against Dollars',
interval: 5
},
tooltip:{enable:true},
chartArea: { border: { width: 0 } },
series: [{
dataSource: data,
xName: 'x', yName: 'y',
name: 'Apple Inc',
fill: '#0066FF',
//Series type as scatter
type: 'Scatter',
trendlines: [{ type: 'MovingAverage', fill: 'red', width:2 }]
}],
title: 'Historical Indian Rupee Rate (INR USD)'
}, '#element');<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Animation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>Forecasting
Use forwardForecast and backwardForecast with supported regression trendline types. Forecast lengths are interpreted in X-axis interval units. Moving-average forecasting support should be verified against the installed Chart version.
Trendline forecasting extends the rendered trendline beyond the existing data range. Forecast values use the interval units of the series X-axis.
Forward forecasting
Use the forwardForecast property to extend the trendline into the future.
var data = [];
var yValue = [7.66, 8.03, 8.41, 8.97, 8.77, 8.20, 8.16, 7.89, 8.68, 9.48, 10.11, 11.36, 12.34, 12.60, 12.95, 13.91, 16.21, 17.50, 22.72, 28.14, 31.26, 31.39, 32.43, 35.52, 36.36,
41.33, 43.12, 45.00, 47.23, 48.62, 46.60, 45.28, 44.01, 45.17, 41.20, 43.41, 48.32, 45.65, 46.61, 53.34, 58.53];
var point;
var i; var j = 0;
for (i = 1973; i <= 2013; i++) {
point = { x: i, y: yValue[j] };
data.push(point);
j++;
}
var chart = new ej.charts.Chart({
primaryXAxis: {
title: 'Months',
},
primaryYAxis: {
title: 'Rupees against Dollars',
interval: 5
},
tooltip:{enable:true},
chartArea: { border: { width: 0 } },
series: [{
dataSource: data,
xName: 'x', yName: 'y',
name: 'Apple Inc',
fill: '#0066FF',
//Series type as scatter
type: 'Scatter',
trendlines: [{
type: 'Linear',
//forward forecast value
forwardForecast:5
}]
}],
title: 'Historical Indian Rupee Rate (INR USD)'
}, '#element');<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Animation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>Backward forecasting
Use the backwardForecast property to extend the trendline before the existing data range.
var data = [];
var yValue = [7.66, 8.03, 8.41, 8.97, 8.77, 8.20, 8.16, 7.89, 8.68, 9.48, 10.11, 11.36, 12.34, 12.60, 12.95, 13.91, 16.21, 17.50, 22.72, 28.14, 31.26, 31.39, 32.43, 35.52, 36.36,
41.33, 43.12, 45.00, 47.23, 48.62, 46.60, 45.28, 44.01, 45.17, 41.20, 43.41, 48.32, 45.65, 46.61, 53.34, 58.53];
var point;
var i; var j = 0;
for (i = 1973; i <= 2013; i++) {
point = { x: i, y: yValue[j] };
data.push(point);
j++;
}
var chart = new ej.charts.Chart({
primaryXAxis: {
title: 'Months',
},
primaryYAxis: {
title: 'Rupees against Dollars',
interval: 5
},
tooltip:{enable:true},
chartArea: { border: { width: 0 } },
series: [{
dataSource: data,
xName: 'x', yName: 'y',
name: 'Apple Inc',
fill: '#0066FF',
//Series type as scatter
type: 'Scatter',
trendlines: [{
type: 'Linear',
//backward forecast value
backwardForecast: 5
}]
}],
title: 'Historical Indian Rupee Rate (INR USD)'
}, '#element');<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Animation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>Show or hide a trendline
Use the visible property to show or hide the trendline. The default value is true.
var data = [];
var yValue = [7.66, 8.03, 8.41, 8.97, 8.77, 8.20, 8.16, 7.89, 8.68, 9.48, 10.11, 11.36, 12.34, 12.60, 12.95, 13.91, 16.21, 17.50, 22.72, 28.14, 31.26, 31.39, 32.43, 35.52, 36.36,
41.33, 43.12, 45.00, 47.23, 48.62, 46.60, 45.28, 44.01, 45.17, 41.20, 43.41, 48.32, 45.65, 46.61, 53.34, 58.53];
var point;
var i; var j = 0;
for (i = 1973; i <= 2013; i++) {
point = { x: i, y: yValue[j] };
data.push(point);
j++;
}
var chart = new ej.charts.Chart({
primaryXAxis: {
title: 'Months',
},
primaryYAxis: {
title: 'Rupees against Dollars',
interval: 5
},
tooltip:{enable:true},
chartArea: { border: { width: 0 } },
series: [{
dataSource: data,
xName: 'x', yName: 'y',
name: 'Apple Inc',
fill: '#0066FF',
//Series type as scatter
type: 'Scatter',
trendlines: [{ visible: false }]
}],
title: 'Historical Indian Rupee Rate (INR USD)'
}, '#element');<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Animation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<div id="element"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>Troubleshooting
- If a trendline is not displayed, verify that it is configured inside a supported series and that
visibleis notfalse. - If an Exponential trendline is missing, verify that all mapped Y values are greater than zero.
- If a Logarithmic trendline is missing, verify that all mapped X values are greater than zero.
- If a Power trendline is missing, verify that all mapped X and Y values are greater than zero.
- If a Moving Average trendline starts later than expected, verify that enough source points exist for the configured
period. - If forecasting appears misaligned, verify the X-axis interval and the values assigned to
forwardForecastandbackwardForecast. - If a trendline legend item or tooltip is missing, verify
name,legendShape, andenableTooltip.