Skip to content

Commit ef8aac5

Browse files
committed
Add population tooltip
1 parent 2248026 commit ef8aac5

File tree

1 file changed

+42
-22
lines changed

1 file changed

+42
-22
lines changed

app.js

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,41 @@ 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+
66+
if (window.innerWidth < 1000) {
67+
// better for smaller displays
68+
tooltipElement.style.maxWidth = window.innerWidth * 2 / 3 + "px";
69+
tooltipElement.style.left = e.pageX - tooltipElement.getBoundingClientRect().width * e.pageX / window.innerWidth + "px";
70+
} else {
71+
tooltipElement.style.maxWidth = window.innerWidth / 2 + "px";
72+
if (e.pageX + 20 < window.innerWidth - tooltipElement.getBoundingClientRect().width) {
73+
tooltipElement.style.left = e.pageX + 20 + "px";
74+
tooltipElement.style.right = '';
75+
} else {
76+
tooltipElement.style.left = '';
77+
tooltipElement.style.right = window.innerWidth - e.pageX + 20 + "px";
78+
}
79+
}
80+
if (e.pageY + 20 < window.innerHeight - tooltipElement.getBoundingClientRect().height) {
81+
tooltipElement.style.top = e.pageY + 20 + "px";
82+
tooltipElement.style.bottom = '';
83+
} else {
84+
tooltipElement.style.top = '';
85+
tooltipElement.style.bottom = window.innerHeight - e.pageY + 20 + "px";
86+
}
87+
tooltipElement.style.opacity = 1;
88+
}
89+
90+
function registerTooptip(element, tooltip){
91+
element.addEventListener("pointerover", e => showTooltip(e, tooltip));
92+
element.addEventListener("pointerdown", e => showTooltip(e, tooltip));
93+
element.addEventListener("pointermove", e => showTooltip(e, tooltip));
94+
element.addEventListener("pointerout", () => (tooltipElement.style.opacity = 0));
95+
}
96+
97+
6398
// ------------------------------------------------------------
6499
// Dropdown für Jahr/Zeitraum vorbereiten
65100
function populateYearSelect() {
@@ -109,6 +144,12 @@ async function fetchPopulationData() {
109144
const res = await fetch("population.json");
110145
if (!res.ok) throw new Error("Fehler beim Laden der Bevölkerungsdaten");
111146
populationData = await res.json();
147+
148+
for (let element of document.getElementsByClassName("country-item")) {
149+
const population = Object.values(populationData.countries[element.dataset.code].subdivisions).reduce((a, b) => a + b, 0);
150+
registerTooptip(element, `<span class="tooltip-title">${(population / 1e6).toFixed(1)} Mio. Einwohner</span>\n`);
151+
}
152+
112153
}
113154

114155
// ------------------------------------------------------------
@@ -319,28 +360,7 @@ function renderCalendar(fromDate, toDate, stats) {
319360
cell.style.fontWeight = stats[key].off ? "bold" : "regular";
320361

321362
// 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));
363+
registerTooptip(cell, stats[key].tooltip);
344364

345365
}
346366

0 commit comments

Comments
 (0)