Skip to content
This repository has been archived by the owner on Nov 28, 2023. It is now read-only.

Commit

Permalink
Add linear regression
Browse files Browse the repository at this point in the history
  • Loading branch information
AOx0 committed Nov 11, 2023
1 parent d6b2535 commit 105de70
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
1 change: 1 addition & 0 deletions templates/hello.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<script src="/htmx.js"></script>
<script src="/alpine.js" defer></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src=" https://cdn.jsdelivr.net/npm/regression@2.0.1/dist/regression.min.js "></script>
<script src="/script.js"></script>
<style>
path:hover {
Expand Down
51 changes: 40 additions & 11 deletions templates/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ const CATEGORIAS = [
"Violación",
];

function linear_regression(data) {
let result = []

// Se espera que data llegue como un vector [entero]
let data_xy = data.map((e, i) => {
return [i+1, e];
});

const my_regression = regression.linear(
data_xy
);

result = my_regression.points.map(([x, y]) => {
return y;
});

return result;
}

function init_draw_pinned_chart(num, data, pinned) {

const dat = {
Expand All @@ -62,17 +81,31 @@ function init_draw_pinned_chart(num, data, pinned) {
.then((json) => {
const ctx = document.getElementById(`pinned-${num}`);


var totales = json["valores"].reduce((acc, array) => acc.map((sum, i) => sum + array[i]), new Array(json["valores"][0].length).fill(0));
var pr_totales = totales.map(function (curr) {
return curr/json["valores"].length;
});
const regresion = linear_regression(pr_totales);

let datasets = json["valores"].map((e, i) => {return {
label: NOMBRES[pinned[i]-1],
data: e,
borderWidth: 1
};});

datasets.splice(0, 0, {
label: "",
data: regresion,
borderWidth: 1,
pointRadius: 0,
});

if( window.myBar===undefined) {
window.myBar = new Chart(ctx, {
type: 'line',
data: {
labels: json["meses"],
datasets: json["valores"].map((e, i) => {return {
label: NOMBRES[pinned[i]-1],
data: e,
borderWidth: 1
};})
datasets: datasets,
},
options: {
scales: {
Expand All @@ -83,11 +116,7 @@ function init_draw_pinned_chart(num, data, pinned) {
}
});
} else {
window.myBar.data.datasets = json["valores"].map((e, i) => {return {
label: NOMBRES[pinned[i]-1],
data: e,
borderWidth: 1
};});
window.myBar.data.datasets = datasets;
window.myBar.data.labels = json["meses"];
window.myBar.update();
}
Expand Down

0 comments on commit 105de70

Please sign in to comment.