diff --git a/docs/Formular.md b/docs/Formular.md index bd80621..3bfe596 100644 --- a/docs/Formular.md +++ b/docs/Formular.md @@ -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() } ``` diff --git a/src/barchart/script.js b/src/barchart/script.js index 62c3f8d..23efb32 100644 --- a/src/barchart/script.js +++ b/src/barchart/script.js @@ -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 @@ -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, }, ], @@ -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 = ""; -} +}); \ No newline at end of file diff --git a/src/formular/formular.js b/src/formular/formular.js index c05c979..0b11826 100644 --- a/src/formular/formular.js +++ b/src/formular/formular.js @@ -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)); } diff --git a/src/app.html b/src/index.html similarity index 100% rename from src/app.html rename to src/index.html