-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
68 lines (57 loc) · 2.13 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
document.getElementById('dataForm').addEventListener('submit', function(event) {
event.preventDefault();
const dataInput = document.getElementById('dataPoints').value.trim();
if (!dataInput) return;
const dataPoints = dataInput.split('\n').map(point => point.split(',').map(Number));
const xs = dataPoints.map(point => point[0]);
const ys = dataPoints.map(point => point[1]);
const { slope, intercept } = calculateBestFitLine(xs, ys);
const regressionPoints = xs.map(x => ({ x, y: slope * x + intercept }));
drawChart(dataPoints, regressionPoints);
});
function calculateBestFitLine(xs, ys) {
const n = xs.length;
const sumX = xs.reduce((a, b) => a + b, 0);
const sumY = ys.reduce((a, b) => a + b, 0);
const sumXY = xs.reduce((sum, x, i) => sum + x * ys[i], 0);
const sumX2 = xs.reduce((sum, x) => sum + x * x, 0);
const slope = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX * sumX);
const intercept = (sumY - slope * sumX) / n;
return { slope, intercept };
}
function drawChart(dataPoints, regressionPoints) {
const ctx = document.getElementById('myChart').getContext('2d');
new Chart(ctx, {
type: 'scatter',
data: {
datasets: [
{
label: 'Data Points',
data: dataPoints.map(point => ({ x: point[0], y: point[1] })),
backgroundColor: 'blue',
},
{
label: 'Best Fit Line',
data: regressionPoints,
type: 'line',
borderColor: 'red',
fill: false,
showLine: true,
},
],
},
options: {
responsive: true,
scales: {
x: {
type: 'linear',
position: 'bottom',
},
y: {
type: 'linear',
position: 'left',
},
},
},
});
}