11# chartjs-plugin-trendline
22
3- This plugin draws an linear trendline in your Chart.
3+ This plugin draws linear and exponential trendlines in your Chart.
44It has been tested with Chart.js version 4.4.9.
55
66## 📊 [ View Live Examples] ( https://makanz.github.io/chartjs-plugin-trendline/ )
@@ -35,6 +35,10 @@ ChartJS.plugins.register(chartTrendline);
3535
3636To configure the trendline plugin you simply add a new config options to your dataset in your chart config.
3737
38+ ### Linear Trendlines
39+
40+ For linear trendlines (straight lines), use the ` trendlineLinear ` configuration:
41+
3842``` javascript
3943{
4044 trendlineLinear: {
@@ -51,7 +55,7 @@ To configure the trendline plugin you simply add a new config options to your da
5155 color: Color,
5256 text: string,
5357 display: boolean,
54- displayValue: boolean,
58+ displayValue: boolean, // shows slope value
5559 offset: number,
5660 percentage: boolean,
5761 font: {
@@ -72,12 +76,65 @@ To configure the trendline plugin you simply add a new config options to your da
7276}
7377```
7478
79+ ### Exponential Trendlines
80+
81+ For exponential trendlines (curves of the form y = a × e^(b×x)), use the ` trendlineExponential ` configuration:
82+
83+ ``` javascript
84+ {
85+ trendlineExponential: {
86+ colorMin: Color,
87+ colorMax: Color,
88+ lineStyle: string, // "dotted" | "solid" | "dashed" | "dashdot"
89+ width: number,
90+ xAxisKey: string, // optional
91+ yAxisKey: string, // optional
92+ projection: boolean, // optional
93+ trendoffset: number, // optional, if > 0 skips first n elements, if < 0 uses last n elements
94+ // optional
95+ label: {
96+ color: Color,
97+ text: string,
98+ display: boolean,
99+ displayValue: boolean, // shows exponential parameters (a, b)
100+ offset: number,
101+ font: {
102+ family: string,
103+ size: number,
104+ }
105+ },
106+ // optional
107+ legend: {
108+ text: string,
109+ strokeStyle: Color,
110+ fillStyle: Color,
111+ lineCap: string,
112+ lineDash: number[],
113+ lineWidth: number,
114+ }
115+ }
116+ }
117+ ```
118+
119+ ** Note:** Exponential trendlines work best with positive y-values. The equation fitted is y = a × e^(b×x), where:
120+ - ` a ` is the coefficient (scaling factor)
121+ - ` b ` is the growth rate (positive for growth, negative for decay)
122+
123+ ## Examples
124+
125+ - [ Linear Trendline Example] ( ./example/lineChart.html )
126+ - [ Exponential Trendline Example] ( ./example/exponentialChart.html )
127+ - [ Bar Chart with Trendline] ( ./example/barChart.html )
128+ - [ Scatter Chart with Trendline] ( ./example/scatterChart.html )
129+
75130## Supported chart types
76131
77132- bar
78133- line
79134- scatter
80135
136+ Both linear and exponential trendlines are supported for all chart types.
137+
81138## Contributing
82139
83140Pull requests and issues are always welcome.
0 commit comments