Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/Formular.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Daten werden nach der Berechnung als JSON-String gespeichert:
"height": "180",
"bmi": 23.1,
"category": "Normalgewicht",
"timestamp": "2025-02-23T14:30:45.123Z"
"timestamp": "2025-02-23T14:30:45.123Z" // new Date().toISOString()
}
```

Expand Down
48 changes: 5 additions & 43 deletions src/barchart/script.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
const ctx = document.getElementById("bmiChart");

/*
Height is profile data.
Later this can come from:
- localStorage
- backend API
- user profile page
*/
const USER_HEIGHT_CM = 170; // <-- change once, used everywhere
const USER_HEIGHT_M = USER_HEIGHT_CM / 100;

let bmiHistory = JSON.parse(localStorage.getItem("bmiHistory")) || [];
const bmiData = JSON.parse(localStorage.getItem("bmiData")) || [];

function getBMIColor(bmi) {
if (bmi < 18.5) return "#74c0fc"; // Underweight
Expand All @@ -22,12 +11,12 @@ function getBMIColor(bmi) {
const bmiChart = new Chart(ctx, {
type: "bar",
data: {
labels: bmiHistory.map((e) => e.date),
labels: bmiData?.map((e) => e.timestamp),
datasets: [
{
label: "BMI",
data: bmiHistory.map((e) => e.bmi),
backgroundColor: bmiHistory.map((e) => getBMIColor(e.bmi)),
data: bmiData.map((e) => e.bmi),
backgroundColor: bmiData.map((e) => getBMIColor(e.bmi)),
borderRadius: 6,
},
],
Expand All @@ -54,31 +43,4 @@ const bmiChart = new Chart(ctx, {
},
},
},
});

function addBMI() {
const weight = parseFloat(document.getElementById("weight").value);

if (!weight) {
alert("Please enter weight");
return;
}

const bmi = +(weight / (USER_HEIGHT_M * USER_HEIGHT_M)).toFixed(1);

const entry = {
date: new Date().toLocaleDateString(),
bmi,
};

bmiHistory.push(entry);

localStorage.setItem("bmiHistory", JSON.stringify(bmiHistory));

bmiChart.data.labels.push(entry.date);
bmiChart.data.datasets[0].data.push(entry.bmi);
bmiChart.data.datasets[0].backgroundColor.push(getBMIColor(entry.bmi));
bmiChart.update();

document.getElementById("weight").value = "";
}
});
6 changes: 3 additions & 3 deletions src/formular/formular.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ function saveToLocalStorage(age, date, weight, height, bmi, category) {
timestamp: new Date().toISOString(),
};

let bmiHistory = JSON.parse(localStorage.getItem("bmiData") || "[]");
bmiHistory.push(newdata);
localStorage.setItem("bmiData", JSON.stringify(bmiHistory));
let bmiData = JSON.parse(localStorage.getItem("bmiData") || "[]");
bmiData.push(newdata);
localStorage.setItem("bmiData", JSON.stringify(bmiData));
}


Expand Down
File renamed without changes.