-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move class LineFitter to separate file
- Loading branch information
Showing
10 changed files
with
63 additions
and
62 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"), { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"), { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |