feat: improve error management when data are missing

This commit is contained in:
Alexis Fourmaux 2026-05-12 23:24:31 +02:00
parent 319821ce59
commit 5f15002462
4 changed files with 95 additions and 43 deletions

View file

@ -1,4 +1,36 @@
const PERIOD_FORMATS = {
hour: { hour: "2-digit", minute: "2-digit", day: "2-digit", month: "short" },
day: { day: "2-digit", month: "short" },
week: { day: "2-digit", month: "short" },
month: { month: "short", year: "numeric" },
};
function formatPeriod(isoString, granularity) {
const date = new Date(isoString);
const opts = PERIOD_FORMATS[granularity] ?? PERIOD_FORMATS.day;
return date.toLocaleDateString("fr-FR", opts);
}
function showMessage(message) {
document.getElementById("consumption-chart").hidden = true;
const msg = document.getElementById("chart-message");
msg.textContent = message;
msg.hidden = false;
}
function hideMessage() {
document.getElementById("consumption-chart").hidden = false;
document.getElementById("chart-message").hidden = true;
}
function renderChart(points, granularity) {
if (!points || points.length === 0){
showMessage("Aucune donnée à afficher sur cette période");
return null;
}
hideMessage();
const canvas = document.getElementById("consumption-chart");
const labels = points.map((p) => formatPeriod(p.period, granularity));
@ -17,7 +49,7 @@ function renderChart(points, granularity) {
data,
backgroundColor: accent + 'cc',
borderColor: accent,
borderWidth: '1px'
borderWidth: 1
},
],
},
@ -43,17 +75,4 @@ function renderChart(points, granularity) {
});
return chart;
}
const PERIOD_FORMATS = {
hour: { hour: "2-digit", minute: "2-digit", day: "2-digit", month: "short" },
day: { day: "2-digit", month: "short" },
week: { day: "2-digit", month: "short" },
month: { month: "short", year: "numeric" },
};
function formatPeriod(isoString, granularity) {
const date = new Date(isoString);
const opts = PERIOD_FORMATS[granularity] ?? PERIOD_FORMATS.day;
return date.toLocaleDateString("fr-FR", opts);
}