chartjs-plugin-trendline
v3.3.2
Published
Trendline for Chart.js
Downloads
161,643
Readme
chartjs-plugin-trendline
This plugin draws linear and exponential trendlines in your Chart. It has been tested with Chart.js version 4.6.1.
📊 View Live Examples
See the plugin in action with interactive examples for different chart types.
Installation
Note: Version 3.2.0+ supports ES modules (ESM) for modern bundlers like Vite, SvelteKit, and Next.js. See MIGRATION.md for upgrade details.
Modern Bundlers (Vite, SvelteKit, Next.js, webpack 5+)
Install via npm:
npm i chart.js chartjs-plugin-trendlineImport and register with Chart.js:
import { Chart } from 'chart.js';
import ChartJSTrendline from 'chartjs-plugin-trendline';
Chart.register(ChartJSTrendline);Node.js (CommonJS)
const { Chart } = require('chart.js');
const ChartJSTrendline = require('chartjs-plugin-trendline');
Chart.register(ChartJSTrendline);Browser (CDN)
Load Chart.js first, then the plugin which will automatically register itself:
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.umd.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-trendline/dist/chartjs-plugin-trendline.min.js"></script>The plugin is now available globally as ChartJSTrendline and auto-registers with Chart.js.
Configuration
To configure the trendline plugin you simply add a new config options to your dataset in your chart config.
Linear Trendlines
For linear trendlines (straight lines), use the trendlineLinear configuration:
{
trendlineLinear: {
colorMin: Color,
colorMax: Color,
lineStyle: string, // "dotted" | "solid" | "dashed" | "dashdot"
width: number,
xAxisKey: string, // optional
yAxisKey: string, // optional
projection: boolean, // optional
fillColor: Color, // optional, fills between the trendline and the bottom of the chart area
trendoffset: number, // optional, if > 0 skips the first n elements, if < 0 excludes the last n
// optional
label: {
color: Color,
text: string,
display: boolean,
displayValue: boolean, // shows slope value
offset: number,
percentage: boolean, // prints slope × 100; meaningful when x has a unit
font: {
family: string,
size: number,
}
},
// optional
legend: {
display: boolean, // false suppresses the entry; any legend object adds one otherwise
text: string,
strokeStyle: Color,
color: Color, // alias for strokeStyle
fillStyle: Color,
lineCap: string,
lineDash: number[],
lineWidth: number, // defaults to width
}
}
}Exponential Trendlines
For exponential trendlines (curves of the form y = a × e^(b×x)), use the trendlineExponential configuration:
{
trendlineExponential: {
colorMin: Color,
colorMax: Color,
lineStyle: string, // "dotted" | "solid" | "dashed" | "dashdot"
width: number,
xAxisKey: string, // optional
yAxisKey: string, // optional
projection: boolean, // optional
fillColor: Color, // optional, fills between the trendline and the bottom of the chart area
trendoffset: number, // optional, if > 0 skips the first n elements, if < 0 excludes the last n
// optional
label: {
color: Color,
text: string,
display: boolean,
displayValue: boolean, // shows exponential parameters (a, b)
offset: number,
font: {
family: string,
size: number,
}
},
// optional
legend: {
display: boolean, // false suppresses the entry; any legend object adds one otherwise
text: string,
strokeStyle: Color,
color: Color, // alias for strokeStyle
fillStyle: Color,
lineCap: string,
lineDash: number[],
lineWidth: number, // defaults to width
}
}
}Note: Exponential trendlines work best with positive y-values. The equation fitted is y = a × e^(b×x), where:
ais the coefficient (scaling factor)bis the growth rate (positive for growth, negative for decay)
The fitted curve is sampled when it is drawn, so it follows y = a × e^(b×x) on a linear axis as well as on a logarithmic one (where the curve is a straight line). All samples are taken from valid points with y > 0; non-positive values are ignored by the fit.
Dataset-level options
These sit on the dataset, next to data and label, not inside the trendline config:
{
data: [...],
alwaysShowTrendline: true, // keep the trendline when the dataset is hidden from the legend
order: number, // draw order; 0 draws last, on top of the others
}order also orders the trendlines themselves, so a dataset that draws on top has its
trendline drawn on top.
Examples
- Linear Trendline Example — labels, legend entries and gradients
- Bar Chart with Trendline
- Bar Chart with Label — slope in the label
- Bar Chart with Null Values — missing points are skipped
- Exponential Trendline Example — on a logarithmic axis
- Exponential Trendline on a Linear Axis — the curve is visible
- Line Chart Projection — projection, trendoffset, nulls, line styles
- Time Series Line Chart — a time scale
- Scatter Chart with Trendline
- Scatter Projection
- Custom Axis Keys —
xAxisKey/yAxisKey - Fill and Legend Toggle —
fillColorandalwaysShowTrendline - Slope in Percent and Trend Offset —
percentageandtrendoffset
Supported chart types
- bar
- line
- scatter
Both linear and exponential trendlines are supported for all chart types.
Contributing
Pull requests and issues are always welcome. For bugs and feature requests, please create an issue.
License
chartjs-plugin-trendline.js is available under the MIT license.
