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/src/utils/lineFitter.js
CHANGED
|
@@ -1,87 +1,87 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A class that fits a line to a series of points using least squares.
|
|
3
|
-
*/
|
|
4
|
-
export class LineFitter {
|
|
5
|
-
constructor() {
|
|
6
|
-
this.count = 0;
|
|
7
|
-
this.sumx = 0;
|
|
8
|
-
this.sumy = 0;
|
|
9
|
-
this.sumx2 = 0;
|
|
10
|
-
this.sumxy = 0;
|
|
11
|
-
this.minx = Number.MAX_VALUE;
|
|
12
|
-
this.maxx = Number.MIN_VALUE;
|
|
13
|
-
this._cachedSlope = null;
|
|
14
|
-
this._cachedIntercept = null;
|
|
15
|
-
this._cacheValid = false;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Adds a point to the line fitter.
|
|
20
|
-
* @param {number} x - The x-coordinate of the point.
|
|
21
|
-
* @param {number} y - The y-coordinate of the point.
|
|
22
|
-
*/
|
|
23
|
-
add(x, y) {
|
|
24
|
-
this.sumx += x;
|
|
25
|
-
this.sumy += y;
|
|
26
|
-
this.sumx2 += x * x;
|
|
27
|
-
this.sumxy += x * y;
|
|
28
|
-
if (x < this.minx) this.minx = x;
|
|
29
|
-
if (x > this.maxx) this.maxx = x;
|
|
30
|
-
this.count++;
|
|
31
|
-
this._cacheValid = false;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Calculates the slope of the fitted line.
|
|
36
|
-
* @returns {number} - The slope of the line.
|
|
37
|
-
*/
|
|
38
|
-
slope() {
|
|
39
|
-
if (!this._cacheValid) {
|
|
40
|
-
this._computeCoefficients();
|
|
41
|
-
}
|
|
42
|
-
return this._cachedSlope;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Calculates the y-intercept of the fitted line.
|
|
47
|
-
* @returns {number} - The y-intercept of the line.
|
|
48
|
-
*/
|
|
49
|
-
intercept() {
|
|
50
|
-
if (!this._cacheValid) {
|
|
51
|
-
this._computeCoefficients();
|
|
52
|
-
}
|
|
53
|
-
return this._cachedIntercept;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Returns the fitted value (y) for a given x.
|
|
58
|
-
* @param {number} x - The x-coordinate.
|
|
59
|
-
* @returns {number} - The corresponding y-coordinate on the fitted line.
|
|
60
|
-
*/
|
|
61
|
-
f(x) {
|
|
62
|
-
return this.slope() * x + this.intercept();
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Calculates the projection of the line for the future value.
|
|
67
|
-
* @returns {number} - The future value based on the fitted line.
|
|
68
|
-
*/
|
|
69
|
-
fo() {
|
|
70
|
-
return -this.intercept() / this.slope();
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Returns the scale (variance) of the fitted line.
|
|
75
|
-
* @returns {number} - The scale of the fitted line.
|
|
76
|
-
*/
|
|
77
|
-
scale() {
|
|
78
|
-
return this.slope();
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
_computeCoefficients() {
|
|
82
|
-
const denominator = this.count * this.sumx2 - this.sumx * this.sumx;
|
|
83
|
-
this._cachedSlope = (this.count * this.sumxy - this.sumx * this.sumy) / denominator;
|
|
84
|
-
this._cachedIntercept = (this.sumy - this._cachedSlope * this.sumx) / this.count;
|
|
85
|
-
this._cacheValid = true;
|
|
86
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* A class that fits a line to a series of points using least squares.
|
|
3
|
+
*/
|
|
4
|
+
export class LineFitter {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.count = 0;
|
|
7
|
+
this.sumx = 0;
|
|
8
|
+
this.sumy = 0;
|
|
9
|
+
this.sumx2 = 0;
|
|
10
|
+
this.sumxy = 0;
|
|
11
|
+
this.minx = Number.MAX_VALUE;
|
|
12
|
+
this.maxx = Number.MIN_VALUE;
|
|
13
|
+
this._cachedSlope = null;
|
|
14
|
+
this._cachedIntercept = null;
|
|
15
|
+
this._cacheValid = false;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Adds a point to the line fitter.
|
|
20
|
+
* @param {number} x - The x-coordinate of the point.
|
|
21
|
+
* @param {number} y - The y-coordinate of the point.
|
|
22
|
+
*/
|
|
23
|
+
add(x, y) {
|
|
24
|
+
this.sumx += x;
|
|
25
|
+
this.sumy += y;
|
|
26
|
+
this.sumx2 += x * x;
|
|
27
|
+
this.sumxy += x * y;
|
|
28
|
+
if (x < this.minx) this.minx = x;
|
|
29
|
+
if (x > this.maxx) this.maxx = x;
|
|
30
|
+
this.count++;
|
|
31
|
+
this._cacheValid = false;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Calculates the slope of the fitted line.
|
|
36
|
+
* @returns {number} - The slope of the line.
|
|
37
|
+
*/
|
|
38
|
+
slope() {
|
|
39
|
+
if (!this._cacheValid) {
|
|
40
|
+
this._computeCoefficients();
|
|
41
|
+
}
|
|
42
|
+
return this._cachedSlope;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Calculates the y-intercept of the fitted line.
|
|
47
|
+
* @returns {number} - The y-intercept of the line.
|
|
48
|
+
*/
|
|
49
|
+
intercept() {
|
|
50
|
+
if (!this._cacheValid) {
|
|
51
|
+
this._computeCoefficients();
|
|
52
|
+
}
|
|
53
|
+
return this._cachedIntercept;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Returns the fitted value (y) for a given x.
|
|
58
|
+
* @param {number} x - The x-coordinate.
|
|
59
|
+
* @returns {number} - The corresponding y-coordinate on the fitted line.
|
|
60
|
+
*/
|
|
61
|
+
f(x) {
|
|
62
|
+
return this.slope() * x + this.intercept();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Calculates the projection of the line for the future value.
|
|
67
|
+
* @returns {number} - The future value based on the fitted line.
|
|
68
|
+
*/
|
|
69
|
+
fo() {
|
|
70
|
+
return -this.intercept() / this.slope();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Returns the scale (variance) of the fitted line.
|
|
75
|
+
* @returns {number} - The scale of the fitted line.
|
|
76
|
+
*/
|
|
77
|
+
scale() {
|
|
78
|
+
return this.slope();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
_computeCoefficients() {
|
|
82
|
+
const denominator = this.count * this.sumx2 - this.sumx * this.sumx;
|
|
83
|
+
this._cachedSlope = (this.count * this.sumxy - this.sumx * this.sumy) / denominator;
|
|
84
|
+
this._cachedIntercept = (this.sumy - this._cachedSlope * this.sumx) / this.count;
|
|
85
|
+
this._cacheValid = true;
|
|
86
|
+
}
|
|
87
87
|
}
|