chartjs-plugin-trendline 3.2.0 → 3.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/copilot-instructions.md +40 -40
- package/.github/workflows/release.yml +64 -61
- package/.github/workflows/tests.yml +26 -26
- package/.prettierrc +5 -5
- package/CLAUDE.md +44 -44
- package/GEMINI.md +40 -40
- package/LICENSE +21 -21
- package/MIGRATION.md +126 -126
- package/README.md +166 -166
- package/babel.config.js +3 -3
- package/changelog.md +39 -39
- package/dist/chartjs-plugin-trendline.cjs +884 -885
- package/dist/chartjs-plugin-trendline.esm.js +882 -883
- package/dist/chartjs-plugin-trendline.js +890 -891
- package/dist/chartjs-plugin-trendline.min.js +8 -8
- package/dist/chartjs-plugin-trendline.min.js.map +1 -1
- package/example/barChart.html +165 -165
- package/example/barChartWithNullValues.html +168 -168
- package/example/barChart_label.html +174 -174
- package/example/exponentialChart.html +244 -244
- package/example/lineChart.html +210 -210
- package/example/lineChartProjection.html +261 -261
- package/example/lineChartTypeTime.html +190 -190
- package/example/scatterChart.html +136 -136
- package/example/scatterProjection.html +141 -141
- package/example/test-null-handling.html +59 -59
- package/index.html +215 -215
- package/jest.config.js +4 -4
- package/package.json +45 -40
- package/rollup.config.js +54 -54
- package/src/components/label.js +56 -56
- package/src/components/label.test.js +129 -129
- package/src/components/trendline.js +375 -375
- package/src/components/trendline.test.js +789 -789
- package/src/core/plugin.js +78 -79
- package/src/core/plugin.test.js +307 -0
- package/src/index.js +12 -12
- package/src/utils/drawing.js +125 -125
- package/src/utils/drawing.test.js +308 -308
- package/src/utils/exponentialFitter.js +146 -146
- package/src/utils/exponentialFitter.test.js +362 -362
- package/src/utils/lineFitter.js +86 -86
- package/src/utils/lineFitter.test.js +340 -340
package/MIGRATION.md
CHANGED
|
@@ -1,126 +1,126 @@
|
|
|
1
|
-
# Migration Guide - v3.2.0
|
|
2
|
-
|
|
3
|
-
## What Changed?
|
|
4
|
-
|
|
5
|
-
Version 3.2.0 introduces ES module (ESM) support as the primary distribution format while maintaining backward compatibility with CommonJS and browser usage.
|
|
6
|
-
|
|
7
|
-
## Breaking Changes
|
|
8
|
-
|
|
9
|
-
### 1. Package Distribution Format
|
|
10
|
-
|
|
11
|
-
The package now uses **dual package exports** with ESM as the default:
|
|
12
|
-
|
|
13
|
-
**Before (v3.x):**
|
|
14
|
-
- Single UMD bundle: `dist/chartjs-plugin-trendline.min.js`
|
|
15
|
-
- CommonJS-style package
|
|
16
|
-
|
|
17
|
-
**After (v3.2.x):**
|
|
18
|
-
- ESM build: `dist/chartjs-plugin-trendline.esm.js`
|
|
19
|
-
- CommonJS build: `dist/chartjs-plugin-trendline.cjs`
|
|
20
|
-
- UMD build (unminified): `dist/chartjs-plugin-trendline.js`
|
|
21
|
-
- UMD build (minified): `dist/chartjs-plugin-trendline.min.js`
|
|
22
|
-
|
|
23
|
-
### 2. Import Changes
|
|
24
|
-
|
|
25
|
-
#### Modern Bundlers (Vite, SvelteKit, Next.js, webpack 5)
|
|
26
|
-
No changes needed! Your bundler will automatically use the ESM version:
|
|
27
|
-
|
|
28
|
-
```javascript
|
|
29
|
-
import ChartJSTrendline from 'chartjs-plugin-trendline';
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
#### Node.js with CommonJS
|
|
33
|
-
The CommonJS build is automatically used when using `require()`:
|
|
34
|
-
|
|
35
|
-
```javascript
|
|
36
|
-
const ChartJSTrendline = require('chartjs-plugin-trendline');
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
#### Browser via CDN
|
|
40
|
-
The global export name has changed:
|
|
41
|
-
|
|
42
|
-
**Before:**
|
|
43
|
-
```html
|
|
44
|
-
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-trendline@3"></script>
|
|
45
|
-
<!-- Plugin auto-registered with Chart.js -->
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
**After:**
|
|
49
|
-
```html
|
|
50
|
-
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-trendline@3"></script>
|
|
51
|
-
<script>
|
|
52
|
-
// Option 1: Plugin auto-registers if Chart.js is available
|
|
53
|
-
// Option 2: Manual registration
|
|
54
|
-
Chart.register(ChartJSTrendline);
|
|
55
|
-
</script>
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
## Benefits
|
|
59
|
-
|
|
60
|
-
### For Modern Projects
|
|
61
|
-
- ✅ Better tree-shaking support
|
|
62
|
-
- ✅ Faster build times with Vite, SvelteKit, and modern bundlers
|
|
63
|
-
- ✅ Full ESM support
|
|
64
|
-
- ✅ Smaller bundle sizes (only import what you need)
|
|
65
|
-
|
|
66
|
-
### For Legacy Projects
|
|
67
|
-
- ✅ Backward compatible with CommonJS
|
|
68
|
-
- ✅ UMD builds still available for browser `<script>` tags
|
|
69
|
-
- ✅ Works with older webpack and Node.js versions
|
|
70
|
-
|
|
71
|
-
## Recommendations
|
|
72
|
-
|
|
73
|
-
### New Projects
|
|
74
|
-
Use ESM imports with modern bundlers (Vite, SvelteKit, Next.js, Nuxt):
|
|
75
|
-
|
|
76
|
-
```javascript
|
|
77
|
-
import ChartJSTrendline from 'chartjs-plugin-trendline';
|
|
78
|
-
import { Chart } from 'chart.js';
|
|
79
|
-
|
|
80
|
-
Chart.register(ChartJSTrendline);
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
### Legacy Projects
|
|
84
|
-
Continue using CommonJS `require()`:
|
|
85
|
-
|
|
86
|
-
```javascript
|
|
87
|
-
const ChartJSTrendline = require('chartjs-plugin-trendline');
|
|
88
|
-
const Chart = require('chart.js');
|
|
89
|
-
|
|
90
|
-
Chart.register(ChartJSTrendline);
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
### Browser CDN
|
|
94
|
-
Use the minified UMD build:
|
|
95
|
-
|
|
96
|
-
```html
|
|
97
|
-
<script src="https://cdn.jsdelivr.net/npm/chart.js@4"></script>
|
|
98
|
-
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-trendline@3"></script>
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
## Technical Details
|
|
102
|
-
|
|
103
|
-
### Package.json Configuration
|
|
104
|
-
|
|
105
|
-
```json
|
|
106
|
-
{
|
|
107
|
-
"type": "module",
|
|
108
|
-
"main": "./dist/chartjs-plugin-trendline.cjs",
|
|
109
|
-
"module": "./dist/chartjs-plugin-trendline.esm.js",
|
|
110
|
-
"exports": {
|
|
111
|
-
".": {
|
|
112
|
-
"import": "./dist/chartjs-plugin-trendline.esm.js",
|
|
113
|
-
"require": "./dist/chartjs-plugin-trendline.cjs"
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
This ensures:
|
|
120
|
-
- Modern bundlers use the ESM version
|
|
121
|
-
- Node.js `require()` uses the CommonJS version
|
|
122
|
-
- Maximum compatibility across environments
|
|
123
|
-
|
|
124
|
-
## Questions?
|
|
125
|
-
|
|
126
|
-
If you encounter any issues during migration, please [open an issue](https://github.com/Makanz/chartjs-plugin-trendline/issues) on GitHub.
|
|
1
|
+
# Migration Guide - v3.2.0
|
|
2
|
+
|
|
3
|
+
## What Changed?
|
|
4
|
+
|
|
5
|
+
Version 3.2.0 introduces ES module (ESM) support as the primary distribution format while maintaining backward compatibility with CommonJS and browser usage.
|
|
6
|
+
|
|
7
|
+
## Breaking Changes
|
|
8
|
+
|
|
9
|
+
### 1. Package Distribution Format
|
|
10
|
+
|
|
11
|
+
The package now uses **dual package exports** with ESM as the default:
|
|
12
|
+
|
|
13
|
+
**Before (v3.x):**
|
|
14
|
+
- Single UMD bundle: `dist/chartjs-plugin-trendline.min.js`
|
|
15
|
+
- CommonJS-style package
|
|
16
|
+
|
|
17
|
+
**After (v3.2.x):**
|
|
18
|
+
- ESM build: `dist/chartjs-plugin-trendline.esm.js`
|
|
19
|
+
- CommonJS build: `dist/chartjs-plugin-trendline.cjs`
|
|
20
|
+
- UMD build (unminified): `dist/chartjs-plugin-trendline.js`
|
|
21
|
+
- UMD build (minified): `dist/chartjs-plugin-trendline.min.js`
|
|
22
|
+
|
|
23
|
+
### 2. Import Changes
|
|
24
|
+
|
|
25
|
+
#### Modern Bundlers (Vite, SvelteKit, Next.js, webpack 5)
|
|
26
|
+
No changes needed! Your bundler will automatically use the ESM version:
|
|
27
|
+
|
|
28
|
+
```javascript
|
|
29
|
+
import ChartJSTrendline from 'chartjs-plugin-trendline';
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
#### Node.js with CommonJS
|
|
33
|
+
The CommonJS build is automatically used when using `require()`:
|
|
34
|
+
|
|
35
|
+
```javascript
|
|
36
|
+
const ChartJSTrendline = require('chartjs-plugin-trendline');
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
#### Browser via CDN
|
|
40
|
+
The global export name has changed:
|
|
41
|
+
|
|
42
|
+
**Before:**
|
|
43
|
+
```html
|
|
44
|
+
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-trendline@3"></script>
|
|
45
|
+
<!-- Plugin auto-registered with Chart.js -->
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
**After:**
|
|
49
|
+
```html
|
|
50
|
+
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-trendline@3"></script>
|
|
51
|
+
<script>
|
|
52
|
+
// Option 1: Plugin auto-registers if Chart.js is available
|
|
53
|
+
// Option 2: Manual registration
|
|
54
|
+
Chart.register(ChartJSTrendline);
|
|
55
|
+
</script>
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Benefits
|
|
59
|
+
|
|
60
|
+
### For Modern Projects
|
|
61
|
+
- ✅ Better tree-shaking support
|
|
62
|
+
- ✅ Faster build times with Vite, SvelteKit, and modern bundlers
|
|
63
|
+
- ✅ Full ESM support
|
|
64
|
+
- ✅ Smaller bundle sizes (only import what you need)
|
|
65
|
+
|
|
66
|
+
### For Legacy Projects
|
|
67
|
+
- ✅ Backward compatible with CommonJS
|
|
68
|
+
- ✅ UMD builds still available for browser `<script>` tags
|
|
69
|
+
- ✅ Works with older webpack and Node.js versions
|
|
70
|
+
|
|
71
|
+
## Recommendations
|
|
72
|
+
|
|
73
|
+
### New Projects
|
|
74
|
+
Use ESM imports with modern bundlers (Vite, SvelteKit, Next.js, Nuxt):
|
|
75
|
+
|
|
76
|
+
```javascript
|
|
77
|
+
import ChartJSTrendline from 'chartjs-plugin-trendline';
|
|
78
|
+
import { Chart } from 'chart.js';
|
|
79
|
+
|
|
80
|
+
Chart.register(ChartJSTrendline);
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Legacy Projects
|
|
84
|
+
Continue using CommonJS `require()`:
|
|
85
|
+
|
|
86
|
+
```javascript
|
|
87
|
+
const ChartJSTrendline = require('chartjs-plugin-trendline');
|
|
88
|
+
const Chart = require('chart.js');
|
|
89
|
+
|
|
90
|
+
Chart.register(ChartJSTrendline);
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Browser CDN
|
|
94
|
+
Use the minified UMD build:
|
|
95
|
+
|
|
96
|
+
```html
|
|
97
|
+
<script src="https://cdn.jsdelivr.net/npm/chart.js@4"></script>
|
|
98
|
+
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-trendline@3"></script>
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Technical Details
|
|
102
|
+
|
|
103
|
+
### Package.json Configuration
|
|
104
|
+
|
|
105
|
+
```json
|
|
106
|
+
{
|
|
107
|
+
"type": "module",
|
|
108
|
+
"main": "./dist/chartjs-plugin-trendline.cjs",
|
|
109
|
+
"module": "./dist/chartjs-plugin-trendline.esm.js",
|
|
110
|
+
"exports": {
|
|
111
|
+
".": {
|
|
112
|
+
"import": "./dist/chartjs-plugin-trendline.esm.js",
|
|
113
|
+
"require": "./dist/chartjs-plugin-trendline.cjs"
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
This ensures:
|
|
120
|
+
- Modern bundlers use the ESM version
|
|
121
|
+
- Node.js `require()` uses the CommonJS version
|
|
122
|
+
- Maximum compatibility across environments
|
|
123
|
+
|
|
124
|
+
## Questions?
|
|
125
|
+
|
|
126
|
+
If you encounter any issues during migration, please [open an issue](https://github.com/Makanz/chartjs-plugin-trendline/issues) on GitHub.
|
package/README.md
CHANGED
|
@@ -1,166 +1,166 @@
|
|
|
1
|
-
# chartjs-plugin-trendline
|
|
2
|
-
|
|
3
|
-
This plugin draws linear and exponential trendlines in your Chart.
|
|
4
|
-
It has been tested with Chart.js version 4.4.9.
|
|
5
|
-
|
|
6
|
-
## 📊 [View Live Examples](https://makanz.github.io/chartjs-plugin-trendline/)
|
|
7
|
-
|
|
8
|
-
See the plugin in action with interactive examples for different chart types.
|
|
9
|
-
|
|
10
|
-
## Installation
|
|
11
|
-
|
|
12
|
-
> **Note:** Version 3.2.0+ supports ES modules (ESM) for modern bundlers like Vite, SvelteKit, and Next.js. See [MIGRATION.md](./MIGRATION.md) for upgrade details.
|
|
13
|
-
|
|
14
|
-
### Modern Bundlers (Vite, SvelteKit, Next.js, webpack 5+)
|
|
15
|
-
|
|
16
|
-
Install via npm:
|
|
17
|
-
|
|
18
|
-
```bash
|
|
19
|
-
npm i chart.js chartjs-plugin-trendline
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
Import and register with Chart.js:
|
|
23
|
-
|
|
24
|
-
```js
|
|
25
|
-
import { Chart } from 'chart.js';
|
|
26
|
-
import ChartJSTrendline from 'chartjs-plugin-trendline';
|
|
27
|
-
|
|
28
|
-
Chart.register(ChartJSTrendline);
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
### Node.js (CommonJS)
|
|
32
|
-
|
|
33
|
-
```js
|
|
34
|
-
const { Chart } = require('chart.js');
|
|
35
|
-
const ChartJSTrendline = require('chartjs-plugin-trendline');
|
|
36
|
-
|
|
37
|
-
Chart.register(ChartJSTrendline);
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
### Browser (CDN)
|
|
41
|
-
|
|
42
|
-
Load Chart.js first, then the plugin which will automatically register itself:
|
|
43
|
-
|
|
44
|
-
```html
|
|
45
|
-
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.9/dist/chart.min.js"></script>
|
|
46
|
-
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-trendline/dist/chartjs-plugin-trendline.min.js"></script>
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
The plugin is now available globally as `ChartJSTrendline` and auto-registers with Chart.js.
|
|
50
|
-
|
|
51
|
-
## Configuration
|
|
52
|
-
|
|
53
|
-
To configure the trendline plugin you simply add a new config options to your dataset in your chart config.
|
|
54
|
-
|
|
55
|
-
### Linear Trendlines
|
|
56
|
-
|
|
57
|
-
For linear trendlines (straight lines), use the `trendlineLinear` configuration:
|
|
58
|
-
|
|
59
|
-
```javascript
|
|
60
|
-
{
|
|
61
|
-
trendlineLinear: {
|
|
62
|
-
colorMin: Color,
|
|
63
|
-
colorMax: Color,
|
|
64
|
-
lineStyle: string, // "dotted" | "solid" | "dashed" | "dashdot"
|
|
65
|
-
width: number,
|
|
66
|
-
xAxisKey: string, // optional
|
|
67
|
-
yAxisKey: string, // optional
|
|
68
|
-
projection: boolean, // optional
|
|
69
|
-
trendoffset: number, // optional, if > 0 skips first n elements, if < 0 uses last n elements
|
|
70
|
-
// optional
|
|
71
|
-
label: {
|
|
72
|
-
color: Color,
|
|
73
|
-
text: string,
|
|
74
|
-
display: boolean,
|
|
75
|
-
displayValue: boolean, // shows slope value
|
|
76
|
-
offset: number,
|
|
77
|
-
percentage: boolean,
|
|
78
|
-
font: {
|
|
79
|
-
family: string,
|
|
80
|
-
size: number,
|
|
81
|
-
}
|
|
82
|
-
},
|
|
83
|
-
// optional
|
|
84
|
-
legend: {
|
|
85
|
-
text: string,
|
|
86
|
-
strokeStyle: Color,
|
|
87
|
-
fillStyle: Color,
|
|
88
|
-
lineCap: string,
|
|
89
|
-
lineDash: number[],
|
|
90
|
-
lineWidth: number,
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
### Exponential Trendlines
|
|
97
|
-
|
|
98
|
-
For exponential trendlines (curves of the form y = a × e^(b×x)), use the `trendlineExponential` configuration:
|
|
99
|
-
|
|
100
|
-
```javascript
|
|
101
|
-
{
|
|
102
|
-
trendlineExponential: {
|
|
103
|
-
colorMin: Color,
|
|
104
|
-
colorMax: Color,
|
|
105
|
-
lineStyle: string, // "dotted" | "solid" | "dashed" | "dashdot"
|
|
106
|
-
width: number,
|
|
107
|
-
xAxisKey: string, // optional
|
|
108
|
-
yAxisKey: string, // optional
|
|
109
|
-
projection: boolean, // optional
|
|
110
|
-
trendoffset: number, // optional, if > 0 skips first n elements, if < 0 uses last n elements
|
|
111
|
-
// optional
|
|
112
|
-
label: {
|
|
113
|
-
color: Color,
|
|
114
|
-
text: string,
|
|
115
|
-
display: boolean,
|
|
116
|
-
displayValue: boolean, // shows exponential parameters (a, b)
|
|
117
|
-
offset: number,
|
|
118
|
-
font: {
|
|
119
|
-
family: string,
|
|
120
|
-
size: number,
|
|
121
|
-
}
|
|
122
|
-
},
|
|
123
|
-
// optional
|
|
124
|
-
legend: {
|
|
125
|
-
text: string,
|
|
126
|
-
strokeStyle: Color,
|
|
127
|
-
fillStyle: Color,
|
|
128
|
-
lineCap: string,
|
|
129
|
-
lineDash: number[],
|
|
130
|
-
lineWidth: number,
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
**Note:** Exponential trendlines work best with positive y-values. The equation fitted is y = a × e^(b×x), where:
|
|
137
|
-
- `a` is the coefficient (scaling factor)
|
|
138
|
-
- `b` is the growth rate (positive for growth, negative for decay)
|
|
139
|
-
|
|
140
|
-
## Examples
|
|
141
|
-
|
|
142
|
-
- [Linear Trendline Example](./example/lineChart.html)
|
|
143
|
-
- [Exponential Trendline Example](./example/exponentialChart.html)
|
|
144
|
-
- [Bar Chart with Trendline](./example/barChart.html)
|
|
145
|
-
- [Scatter Chart with Trendline](./example/scatterChart.html)
|
|
146
|
-
|
|
147
|
-
## Supported chart types
|
|
148
|
-
|
|
149
|
-
- bar
|
|
150
|
-
- line
|
|
151
|
-
- scatter
|
|
152
|
-
|
|
153
|
-
Both linear and exponential trendlines are supported for all chart types.
|
|
154
|
-
|
|
155
|
-
## Contributing
|
|
156
|
-
|
|
157
|
-
Pull requests and issues are always welcome.
|
|
158
|
-
For bugs and feature requests, [please create an issue](https://github.com/Makanz/chartjs-plugin-trendline/issues).
|
|
159
|
-
|
|
160
|
-
## License
|
|
161
|
-
|
|
162
|
-
chartjs-plugin-trendline.js is available under the [MIT license](http://opensource.org/licenses/MIT).
|
|
163
|
-
|
|
164
|
-
## Star History
|
|
165
|
-
|
|
166
|
-
[](https://star-history.com/#Makanz/chartjs-plugin-trendline&Date)
|
|
1
|
+
# chartjs-plugin-trendline
|
|
2
|
+
|
|
3
|
+
This plugin draws linear and exponential trendlines in your Chart.
|
|
4
|
+
It has been tested with Chart.js version 4.4.9.
|
|
5
|
+
|
|
6
|
+
## 📊 [View Live Examples](https://makanz.github.io/chartjs-plugin-trendline/)
|
|
7
|
+
|
|
8
|
+
See the plugin in action with interactive examples for different chart types.
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
> **Note:** Version 3.2.0+ supports ES modules (ESM) for modern bundlers like Vite, SvelteKit, and Next.js. See [MIGRATION.md](./MIGRATION.md) for upgrade details.
|
|
13
|
+
|
|
14
|
+
### Modern Bundlers (Vite, SvelteKit, Next.js, webpack 5+)
|
|
15
|
+
|
|
16
|
+
Install via npm:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm i chart.js chartjs-plugin-trendline
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Import and register with Chart.js:
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
import { Chart } from 'chart.js';
|
|
26
|
+
import ChartJSTrendline from 'chartjs-plugin-trendline';
|
|
27
|
+
|
|
28
|
+
Chart.register(ChartJSTrendline);
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Node.js (CommonJS)
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
const { Chart } = require('chart.js');
|
|
35
|
+
const ChartJSTrendline = require('chartjs-plugin-trendline');
|
|
36
|
+
|
|
37
|
+
Chart.register(ChartJSTrendline);
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Browser (CDN)
|
|
41
|
+
|
|
42
|
+
Load Chart.js first, then the plugin which will automatically register itself:
|
|
43
|
+
|
|
44
|
+
```html
|
|
45
|
+
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.9/dist/chart.min.js"></script>
|
|
46
|
+
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-trendline/dist/chartjs-plugin-trendline.min.js"></script>
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The plugin is now available globally as `ChartJSTrendline` and auto-registers with Chart.js.
|
|
50
|
+
|
|
51
|
+
## Configuration
|
|
52
|
+
|
|
53
|
+
To configure the trendline plugin you simply add a new config options to your dataset in your chart config.
|
|
54
|
+
|
|
55
|
+
### Linear Trendlines
|
|
56
|
+
|
|
57
|
+
For linear trendlines (straight lines), use the `trendlineLinear` configuration:
|
|
58
|
+
|
|
59
|
+
```javascript
|
|
60
|
+
{
|
|
61
|
+
trendlineLinear: {
|
|
62
|
+
colorMin: Color,
|
|
63
|
+
colorMax: Color,
|
|
64
|
+
lineStyle: string, // "dotted" | "solid" | "dashed" | "dashdot"
|
|
65
|
+
width: number,
|
|
66
|
+
xAxisKey: string, // optional
|
|
67
|
+
yAxisKey: string, // optional
|
|
68
|
+
projection: boolean, // optional
|
|
69
|
+
trendoffset: number, // optional, if > 0 skips first n elements, if < 0 uses last n elements
|
|
70
|
+
// optional
|
|
71
|
+
label: {
|
|
72
|
+
color: Color,
|
|
73
|
+
text: string,
|
|
74
|
+
display: boolean,
|
|
75
|
+
displayValue: boolean, // shows slope value
|
|
76
|
+
offset: number,
|
|
77
|
+
percentage: boolean,
|
|
78
|
+
font: {
|
|
79
|
+
family: string,
|
|
80
|
+
size: number,
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
// optional
|
|
84
|
+
legend: {
|
|
85
|
+
text: string,
|
|
86
|
+
strokeStyle: Color,
|
|
87
|
+
fillStyle: Color,
|
|
88
|
+
lineCap: string,
|
|
89
|
+
lineDash: number[],
|
|
90
|
+
lineWidth: number,
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Exponential Trendlines
|
|
97
|
+
|
|
98
|
+
For exponential trendlines (curves of the form y = a × e^(b×x)), use the `trendlineExponential` configuration:
|
|
99
|
+
|
|
100
|
+
```javascript
|
|
101
|
+
{
|
|
102
|
+
trendlineExponential: {
|
|
103
|
+
colorMin: Color,
|
|
104
|
+
colorMax: Color,
|
|
105
|
+
lineStyle: string, // "dotted" | "solid" | "dashed" | "dashdot"
|
|
106
|
+
width: number,
|
|
107
|
+
xAxisKey: string, // optional
|
|
108
|
+
yAxisKey: string, // optional
|
|
109
|
+
projection: boolean, // optional
|
|
110
|
+
trendoffset: number, // optional, if > 0 skips first n elements, if < 0 uses last n elements
|
|
111
|
+
// optional
|
|
112
|
+
label: {
|
|
113
|
+
color: Color,
|
|
114
|
+
text: string,
|
|
115
|
+
display: boolean,
|
|
116
|
+
displayValue: boolean, // shows exponential parameters (a, b)
|
|
117
|
+
offset: number,
|
|
118
|
+
font: {
|
|
119
|
+
family: string,
|
|
120
|
+
size: number,
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
// optional
|
|
124
|
+
legend: {
|
|
125
|
+
text: string,
|
|
126
|
+
strokeStyle: Color,
|
|
127
|
+
fillStyle: Color,
|
|
128
|
+
lineCap: string,
|
|
129
|
+
lineDash: number[],
|
|
130
|
+
lineWidth: number,
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
**Note:** Exponential trendlines work best with positive y-values. The equation fitted is y = a × e^(b×x), where:
|
|
137
|
+
- `a` is the coefficient (scaling factor)
|
|
138
|
+
- `b` is the growth rate (positive for growth, negative for decay)
|
|
139
|
+
|
|
140
|
+
## Examples
|
|
141
|
+
|
|
142
|
+
- [Linear Trendline Example](./example/lineChart.html)
|
|
143
|
+
- [Exponential Trendline Example](./example/exponentialChart.html)
|
|
144
|
+
- [Bar Chart with Trendline](./example/barChart.html)
|
|
145
|
+
- [Scatter Chart with Trendline](./example/scatterChart.html)
|
|
146
|
+
|
|
147
|
+
## Supported chart types
|
|
148
|
+
|
|
149
|
+
- bar
|
|
150
|
+
- line
|
|
151
|
+
- scatter
|
|
152
|
+
|
|
153
|
+
Both linear and exponential trendlines are supported for all chart types.
|
|
154
|
+
|
|
155
|
+
## Contributing
|
|
156
|
+
|
|
157
|
+
Pull requests and issues are always welcome.
|
|
158
|
+
For bugs and feature requests, [please create an issue](https://github.com/Makanz/chartjs-plugin-trendline/issues).
|
|
159
|
+
|
|
160
|
+
## License
|
|
161
|
+
|
|
162
|
+
chartjs-plugin-trendline.js is available under the [MIT license](http://opensource.org/licenses/MIT).
|
|
163
|
+
|
|
164
|
+
## Star History
|
|
165
|
+
|
|
166
|
+
[](https://star-history.com/#Makanz/chartjs-plugin-trendline&Date)
|
package/babel.config.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export default {
|
|
2
|
-
presets: [['@babel/preset-env', {targets: {node: 'current'}}]],
|
|
3
|
-
};
|
|
1
|
+
export default {
|
|
2
|
+
presets: [['@babel/preset-env', {targets: {node: 'current'}}]],
|
|
3
|
+
};
|
package/changelog.md
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
# Changelog 3.1.0
|
|
2
|
-
|
|
3
|
-
### New Features
|
|
4
|
-
- Added exponential trendline support with `trendlineExponential` configuration
|
|
5
|
-
- Exponential trendlines fit curves of the form y = a × e^(b×x)
|
|
6
|
-
- All existing styling options (colors, width, lineStyle, projection, etc.) work with exponential trendlines
|
|
7
|
-
- Added comprehensive test coverage for exponential functionality
|
|
8
|
-
- Added exponential trendline example (exponentialChart.html)
|
|
9
|
-
|
|
10
|
-
### Improvements
|
|
11
|
-
- Updated README with exponential trendline documentation
|
|
12
|
-
- Enhanced package description to mention exponential support
|
|
13
|
-
|
|
14
|
-
# Changelog 3.0.0
|
|
15
|
-
|
|
16
|
-
### Breaking Changes
|
|
17
|
-
- Updated to version 3.0.0-beta.1 with modular code structure
|
|
18
|
-
|
|
19
|
-
### New Features
|
|
20
|
-
- Added trendline offset setting (`trendoffset`)
|
|
21
|
-
- Added two new example files (barChartWithNullValues.html, scatterProjection.html)
|
|
22
|
-
|
|
23
|
-
### Bug Fixes
|
|
24
|
-
- Fixed trendline accuracy and boundary calculations
|
|
25
|
-
- Corrected trendline rendering, projection, and data accuracy
|
|
26
|
-
- Fixed trendline data processing issues
|
|
27
|
-
|
|
28
|
-
### Testing & CI
|
|
29
|
-
- Added comprehensive unit tests for all components (trendline, label, drawing utils, lineFitter)
|
|
30
|
-
- Added GitHub Actions workflow for automated testing on PRs and main branch
|
|
31
|
-
- Updated Node.js version to 22 in CI
|
|
32
|
-
|
|
33
|
-
### Code Quality
|
|
34
|
-
- Split code into modular structure with separate components
|
|
35
|
-
- Added Copilot instructions for development
|
|
36
|
-
- Updated Chart.js dependency to version 4.4.9
|
|
37
|
-
|
|
38
|
-
### Documentation
|
|
39
|
-
- Updated README with new features
|
|
1
|
+
# Changelog 3.1.0
|
|
2
|
+
|
|
3
|
+
### New Features
|
|
4
|
+
- Added exponential trendline support with `trendlineExponential` configuration
|
|
5
|
+
- Exponential trendlines fit curves of the form y = a × e^(b×x)
|
|
6
|
+
- All existing styling options (colors, width, lineStyle, projection, etc.) work with exponential trendlines
|
|
7
|
+
- Added comprehensive test coverage for exponential functionality
|
|
8
|
+
- Added exponential trendline example (exponentialChart.html)
|
|
9
|
+
|
|
10
|
+
### Improvements
|
|
11
|
+
- Updated README with exponential trendline documentation
|
|
12
|
+
- Enhanced package description to mention exponential support
|
|
13
|
+
|
|
14
|
+
# Changelog 3.0.0
|
|
15
|
+
|
|
16
|
+
### Breaking Changes
|
|
17
|
+
- Updated to version 3.0.0-beta.1 with modular code structure
|
|
18
|
+
|
|
19
|
+
### New Features
|
|
20
|
+
- Added trendline offset setting (`trendoffset`)
|
|
21
|
+
- Added two new example files (barChartWithNullValues.html, scatterProjection.html)
|
|
22
|
+
|
|
23
|
+
### Bug Fixes
|
|
24
|
+
- Fixed trendline accuracy and boundary calculations
|
|
25
|
+
- Corrected trendline rendering, projection, and data accuracy
|
|
26
|
+
- Fixed trendline data processing issues
|
|
27
|
+
|
|
28
|
+
### Testing & CI
|
|
29
|
+
- Added comprehensive unit tests for all components (trendline, label, drawing utils, lineFitter)
|
|
30
|
+
- Added GitHub Actions workflow for automated testing on PRs and main branch
|
|
31
|
+
- Updated Node.js version to 22 in CI
|
|
32
|
+
|
|
33
|
+
### Code Quality
|
|
34
|
+
- Split code into modular structure with separate components
|
|
35
|
+
- Added Copilot instructions for development
|
|
36
|
+
- Updated Chart.js dependency to version 4.4.9
|
|
37
|
+
|
|
38
|
+
### Documentation
|
|
39
|
+
- Updated README with new features
|
|
40
40
|
- Added agent instructions for development workflow
|