Skip to content

Commit

Permalink
Move class LineFitter to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
Makanz committed Jun 23, 2024
1 parent e25772f commit 28bc846
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 62 deletions.
2 changes: 1 addition & 1 deletion dist/chartjs-plugin-trendline.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/chartjs-plugin-trendline.min.js.LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*!
* chartjs-plugin-trendline.js
* Version: 2.1.0
* Version: 2.1.1
*
* Copyright 2024 Marcus Alsterfjord
* Released under the MIT license
Expand Down
2 changes: 1 addition & 1 deletion example/barChart.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>BarChart Example</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.umd.js"></script>
<script src="./../src/chartjs-plugin-trendline.js"></script>
<script src="./../dist/chartjs-plugin-trendline.min.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function(event) {
// Bar chart
Expand Down
2 changes: 1 addition & 1 deletion example/barChartWithNullValues.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>BarChart Example</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.umd.js"></script>
<script src="./../src/chartjs-plugin-trendline.js"></script>
<script src="./../dist/chartjs-plugin-trendline.min.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function(event) {
// Bar chart
Expand Down
2 changes: 1 addition & 1 deletion example/lineChart.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>LineChart Example</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.umd.js"></script>
<script src="./../src/chartjs-plugin-trendline.js"></script>
<script src="./../dist/chartjs-plugin-trendline.min.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function(event) {
new Chart(document.getElementById("line-chart"), {
Expand Down
2 changes: 1 addition & 1 deletion example/lineChartProjection.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>XYlineChart Projection Example</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.umd.js"></script>
<script src="./../src/chartjs-plugin-trendline.js"></script>
<script src="./../dist/chartjs-plugin-trendline.min.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function(event) {
new Chart(document.getElementById("line-chart"), {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"homepage": "https://github.com/Makanz/chartjs-plugin-trendline#readme",
"devDependencies": {
"webpack": "^5.92.1",
"webpack": "^5.90.0",
"webpack-cli": "^5.1.4"
}
}
57 changes: 3 additions & 54 deletions src/chartjs-plugin-trendline.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/*!
* chartjs-plugin-trendline.js
* Version: 2.1.0
* Version: 2.1.1
*
* Copyright 2024 Marcus Alsterfjord
* Released under the MIT license
* https://github.com/Makanz/chartjs-plugin-trendline/blob/master/README.md
*
* Mod by: vesal: accept also xy-data so works with scatter
*/
import { LineFitter } from './classes/lineFitter.js';

const pluginTrendlineLinear = {
id: 'chartjs-plugin-trendline',
afterDatasetsDraw: (chartInstance) => {
Expand Down Expand Up @@ -176,59 +178,6 @@ const addFitter = (datasetMeta, ctx, dataset, xScale, yScale) => {
}
};

class LineFitter {
constructor() {
this.count = 0;
this.sumX = 0;
this.sumX2 = 0;
this.sumXY = 0;
this.sumY = 0;
this.minx = 1e100;
this.maxx = -1e100;
this.maxy = -1e100;
}

add(x, y) {
x = parseFloat(x);
y = parseFloat(y);

this.count++;
this.sumX += x;
this.sumX2 += x * x;
this.sumXY += x * y;
this.sumY += y;
if (x < this.minx) this.minx = x;
if (x > this.maxx) this.maxx = x;
if (y > this.maxy) this.maxy = y;
}

f(x) {
x = parseFloat(x);

let det = this.count * this.sumX2 - this.sumX * this.sumX;
let offset = (this.sumX2 * this.sumY - this.sumX * this.sumXY) / det;
let scale = (this.count * this.sumXY - this.sumX * this.sumY) / det;
return offset + x * scale;
}

fo() {
let det = this.count * this.sumX2 - this.sumX * this.sumX;
let offset = (this.sumX2 * this.sumY - this.sumX * this.sumXY) / det;
let scale = (this.count * this.sumXY - this.sumX * this.sumY) / det;

// Get x when y = 0
let xo = -offset / scale;
return xo;
}

scale() {
let det = this.count * this.sumX2 - this.sumX * this.sumX;
let scale = (this.count * this.sumXY - this.sumX * this.sumY) / det;

return scale;
}
}

// If we're in the browser and have access to the global Chart obj, register plugin automatically
if (typeof window !== 'undefined' && window.Chart) {
if (window.Chart.hasOwnProperty('register')) {
Expand Down
52 changes: 52 additions & 0 deletions src/classes/lineFitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
export class LineFitter {
constructor() {
this.count = 0;
this.sumX = 0;
this.sumX2 = 0;
this.sumXY = 0;
this.sumY = 0;
this.minx = 1e100;
this.maxx = -1e100;
this.maxy = -1e100;
}

add = (x, y) => {
x = parseFloat(x);
y = parseFloat(y);

this.count++;
this.sumX += x;
this.sumX2 += x * x;
this.sumXY += x * y;
this.sumY += y;
if (x < this.minx) this.minx = x;
if (x > this.maxx) this.maxx = x;
if (y > this.maxy) this.maxy = y;
};

f = (x) => {
x = parseFloat(x);

let det = this.count * this.sumX2 - this.sumX * this.sumX;
let offset = (this.sumX2 * this.sumY - this.sumX * this.sumXY) / det;
let scale = (this.count * this.sumXY - this.sumX * this.sumY) / det;
return offset + x * scale;
};

fo = () => {
let det = this.count * this.sumX2 - this.sumX * this.sumX;
let offset = (this.sumX2 * this.sumY - this.sumX * this.sumXY) / det;
let scale = (this.count * this.sumXY - this.sumX * this.sumY) / det;

// Get x when y = 0
let xo = -offset / scale;
return xo;
};

scale = () => {
let det = this.count * this.sumX2 - this.sumX * this.sumX;
let scale = (this.count * this.sumXY - this.sumX * this.sumY) / det;

return scale;
};
}

0 comments on commit 28bc846

Please sign in to comment.