-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
65 lines (56 loc) · 2.06 KB
/
script.js
File metadata and controls
65 lines (56 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
document.addEventListener("DOMContentLoaded", function () {
const mapObject = document.getElementById("map");
const svgDoc = mapObject.contentDocument;
const tooltip = document.getElementById("tooltip");
// Function to fetch data from the database
async function fetchData() {
try {
const response = await fetch("./get_districts.php");
const data = await response.json();
console.log(data);
updateMapColors(data);
} catch (error) {
console.error("Error fetching data:", error);
}
}
// Function to update the colors of the map districts
function updateMapColors(data) {
const colors = Array.from({ length: 1000 }, (_, index) => ({
value: index,
color: index === 0 ? "#141414" : "#ffea00",
}));
data.forEach((district) => {
let districtShape = svgDoc.getElementById(district.id);
if (!districtShape) {
const districtName = district.name.toLowerCase().replace(/\s/g, "-");
districtShape = svgDoc.getElementById(districtName);
}
if (districtShape) {
const colorObj = colors.find(
(colorData) => colorData.value === parseInt(district.value)
);
const color = colorObj ? colorObj.color : colors[0].color;
districtShape.style.fill = color;
}
});
}
// Add event listeners for hover and mousemove on each district
mapObject.addEventListener("mouseover", function (event) {
const district = event.target;
tooltip.textContent = `${district.getAttribute(
"data-name"
)}: ${district.getAttribute("data-value")}`;
tooltip.style.display = "block";
tooltip.style.left = event.pageX + 10 + "px";
tooltip.style.top = event.pageY + 10 + "px";
});
mapObject.addEventListener("mousemove", function (event) {
tooltip.style.left = event.pageX + 10 + "px";
tooltip.style.top = event.pageY + 10 + "px";
});
mapObject.addEventListener("mouseout", function () {
tooltip.style.display = "none";
});
fetchData();
setInterval(fetchData, 60000); // 1 minute interval (60000 milliseconds)
});