feat: Improve heatmap by handling future dates

Add logic to include days between today and month's end
Makes future dates transparent in the heatmap display
Ensures tooltips are not shown for future dates
This commit is contained in:
Kumi 2024-12-25 22:29:43 +01:00
parent f3d6154259
commit c04232e39a
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -20,6 +20,27 @@ fetch("/mood/statistics/heatmap/values/")
fetch("/mood/statistics/heatmap/?start=" + start.toISOString().split("T")[0] + "&end=" + end.toISOString().split("T")[0])
.then((response) => response.json())
.then((data) => {
// Add days between today and the last day of the month
// so that we can make them transparent
const today = new Date();
const lastDay = new Date(today.getFullYear(), today.getMonth() + 1, 0);
const days = lastDay.getDate() - today.getDate();
for (let i = 1; i <= days; i++) {
const date = new Date(today.getFullYear(), today.getMonth(), today.getDate() + i);
const formattedDate = dayjs(date).format("YYYY-MM-DD");
if (data.find(o => o["date"] === formattedDate)) {
continue;
}
data.push({
date: dayjs(date).format("YYYY-MM-DD"),
count: -Infinity,
average: -Infinity,
});
}
cal.paint({
itemSelector: "#mood-count-heatmap",
data: {
@ -78,6 +99,10 @@ fetch("/mood/statistics/heatmap/values/")
return `${date_str}<br>Mood Count: ${obj.count}<br>Average Mood: N/A`
}
if (average === -Infinity) {
return
}
const key = Object.keys(moodOptions).reduce((a, b) => Math.abs(a - average) < Math.abs(b - average) ? a : b);
const mood = moodOptions[key];