summaryrefslogtreecommitdiff
path: root/trendline.js
diff options
context:
space:
mode:
authorMinteck <contact@minteck.org>2022-06-27 17:02:30 +0200
committerMinteck <contact@minteck.org>2022-06-27 17:02:30 +0200
commiteed86d556ccba5d6bfdc8795990f188d0c573fc7 (patch)
tree3494d9fb204550eb4eba0321b808f771fb6c94e2 /trendline.js
parentd2a4546fdb55ac8dba8d41d3a7dad39e68555c81 (diff)
downloadbits-client-eed86d556ccba5d6bfdc8795990f188d0c573fc7.tar.gz
bits-client-eed86d556ccba5d6bfdc8795990f188d0c573fc7.tar.bz2
bits-client-eed86d556ccba5d6bfdc8795990f188d0c573fc7.zip
Update
Diffstat (limited to 'trendline.js')
-rw-r--r--trendline.js104
1 files changed, 52 insertions, 52 deletions
diff --git a/trendline.js b/trendline.js
index 7e0d7ad..48e2522 100644
--- a/trendline.js
+++ b/trendline.js
@@ -1,53 +1,53 @@
-function getAvg(arr) {
- const total = arr.reduce((acc, c) => acc + c, 0);
- return total / arr.length;
-}
-
-function getSum(arr) {
- return arr.reduce((acc, c) => acc + c, 0);
-}
-
-function trendline(data, xKey, yKey) {
- const xData = data.map((value) => value[xKey]);
- const yData = data.map((value) => value[yKey]);
-
- // average of X values and Y values
- const xMean = getAvg(xData);
- const yMean = getAvg(yData);
-
- // Subtract X or Y mean from corresponding axis value
- const xMinusxMean = xData.map((val) => val - xMean);
- const yMinusyMean = yData.map((val) => val - yMean);
-
- const xMinusxMeanSq = xMinusxMean.map((val) => Math.pow(val, 2));
-
- const xy = [];
- for (let x = 0; x < data.length; x++) {
- xy.push(xMinusxMean[x] * yMinusyMean[x]);
- }
-
- // const xy = xMinusxMean.map((val, index) => val * yMinusyMean[index]);
-
- const xySum = getSum(xy);
-
- // b1 is the slope
- const b1 = xySum / getSum(xMinusxMeanSq);
- // b0 is the start of the slope on the Y axis
- const b0 = yMean - b1 * xMean;
-
- return {
- slope: b1,
- yStart: b0,
- calcY: (x) => b0 + b1 * x,
- };
-}
-
-const averageDelta = ([x,...xs]) => {
- if (x === undefined)
- return NaN
- else
- return xs.reduce(
- ([acc, last], x) => [acc + (x - last), x],
- [0, x]
- ) [0] / xs.length
+function getAvg(arr) {
+ const total = arr.reduce((acc, c) => acc + c, 0);
+ return total / arr.length;
+}
+
+function getSum(arr) {
+ return arr.reduce((acc, c) => acc + c, 0);
+}
+
+function trendline(data, xKey, yKey) {
+ const xData = data.map((value) => value[xKey]);
+ const yData = data.map((value) => value[yKey]);
+
+ // average of X values and Y values
+ const xMean = getAvg(xData);
+ const yMean = getAvg(yData);
+
+ // Subtract X or Y mean from corresponding axis value
+ const xMinusxMean = xData.map((val) => val - xMean);
+ const yMinusyMean = yData.map((val) => val - yMean);
+
+ const xMinusxMeanSq = xMinusxMean.map((val) => Math.pow(val, 2));
+
+ const xy = [];
+ for (let x = 0; x < data.length; x++) {
+ xy.push(xMinusxMean[x] * yMinusyMean[x]);
+ }
+
+ // const xy = xMinusxMean.map((val, index) => val * yMinusyMean[index]);
+
+ const xySum = getSum(xy);
+
+ // b1 is the slope
+ const b1 = xySum / getSum(xMinusxMeanSq);
+ // b0 is the start of the slope on the Y axis
+ const b0 = yMean - b1 * xMean;
+
+ return {
+ slope: b1,
+ yStart: b0,
+ calcY: (x) => b0 + b1 * x,
+ };
+}
+
+const averageDelta = ([x,...xs]) => {
+ if (x === undefined)
+ return NaN
+ else
+ return xs.reduce(
+ ([acc, last], x) => [acc + (x - last), x],
+ [0, x]
+ ) [0] / xs.length
} \ No newline at end of file