Skip to content

Commit 8da98f2

Browse files
committed
Add population tooltip
1 parent 2248026 commit 8da98f2

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

app.js

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,34 @@ const tooltipElement = document.createElement("div");
6060
tooltipElement.className = "tooltip";
6161
document.body.appendChild(tooltipElement);
6262

63+
function showTooltip(e, tooltip) {
64+
tooltipElement.innerHTML = tooltip;
65+
tooltipElement.style.maxWidth = window.innerWidth / 2 + "px";
66+
if (e.pageX + 20 < window.innerWidth - tooltipElement.getBoundingClientRect().width) {
67+
tooltipElement.style.left = e.pageX + 20 + "px";
68+
tooltipElement.style.right = '';
69+
} else {
70+
tooltipElement.style.left = '';
71+
tooltipElement.style.right = window.innerWidth - e.pageX + 20 + "px";
72+
}
73+
if (e.pageY + 20 < window.innerHeight - tooltipElement.getBoundingClientRect().height) {
74+
tooltipElement.style.top = e.pageY + 20 + "px";
75+
tooltipElement.style.bottom = '';
76+
} else {
77+
tooltipElement.style.top = '';
78+
tooltipElement.style.bottom = window.innerHeight - e.pageY + 20 + "px";
79+
}
80+
tooltipElement.style.opacity = 1;
81+
}
82+
83+
function registerTooptip(element, tooltip){
84+
element.addEventListener("pointerover", e => showTooltip(e, tooltip));
85+
element.addEventListener("pointerdown", e => showTooltip(e, tooltip));
86+
element.addEventListener("pointermove", e => showTooltip(e, tooltip));
87+
element.addEventListener("pointerout", () => (tooltipElement.style.opacity = 0));
88+
}
89+
90+
6391
// ------------------------------------------------------------
6492
// Dropdown für Jahr/Zeitraum vorbereiten
6593
function populateYearSelect() {
@@ -109,6 +137,12 @@ async function fetchPopulationData() {
109137
const res = await fetch("population.json");
110138
if (!res.ok) throw new Error("Fehler beim Laden der Bevölkerungsdaten");
111139
populationData = await res.json();
140+
141+
for (let element of document.getElementsByClassName("country-item")) {
142+
const population = Object.values(populationData.countries[element.dataset.code].subdivisions).reduce((a, b) => a + b, 0);
143+
registerTooptip(element, `<span class="tooltip-title">${(population / 1e6).toFixed(1)} Mio. Einwohner</span>\n`);
144+
}
145+
112146
}
113147

114148
// ------------------------------------------------------------
@@ -319,28 +353,7 @@ function renderCalendar(fromDate, toDate, stats) {
319353
cell.style.fontWeight = stats[key].off ? "bold" : "regular";
320354

321355
// tooltip
322-
const show = (e) => {
323-
tooltipElement.innerHTML = stats[key].tooltip;
324-
tooltipElement.style.maxWidth = window.innerWidth / 2 + "px";
325-
if (e.pageX + 10 < window.innerWidth - tooltipElement.getBoundingClientRect().width) {
326-
tooltipElement.style.left = e.pageX + 10 + "px";
327-
tooltipElement.style.right = '';
328-
} else {
329-
tooltipElement.style.left = '';
330-
tooltipElement.style.right = window.innerWidth - e.pageX + 10 + "px";
331-
}
332-
if (e.pageY + 10 < window.innerHeight - tooltipElement.getBoundingClientRect().height) {
333-
tooltipElement.style.top = e.pageY + 10 + "px";
334-
tooltipElement.style.bottom = '';
335-
} else {
336-
tooltipElement.style.top = '';
337-
tooltipElement.style.bottom = window.innerHeight - e.pageY + 10 + "px";
338-
}
339-
tooltipElement.style.opacity = 1;
340-
};
341-
cell.addEventListener("pointerover", show);
342-
cell.addEventListener("pointerdown", show);
343-
cell.addEventListener("pointerout", () => (tooltipElement.style.opacity = 0));
356+
registerTooptip(cell, stats[key].tooltip);
344357

345358
}
346359

0 commit comments

Comments
 (0)