-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
169 lines (154 loc) · 6.14 KB
/
Copy pathscript.js
File metadata and controls
169 lines (154 loc) · 6.14 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
document.addEventListener("DOMContentLoaded", function () {
// Initialize map
var map = L.map("map", {
scrollWheelZoom: false
}).setView([51.505, -0.09], 4);
map.createPane("labels");
map.getPane("labels").style.zIndex = 450;
map.getPane("labels").style.pointerEvents = "none";
// Add tile layer without labels
L.tileLayer(
"https://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png",
{
attribution: "©OpenStreetMap, ©CartoDB",
}
).addTo(map);
// Add a label layer in a separate pane for better control
var positronLabels = L.tileLayer(
"https://{s}.basemaps.cartocdn.com/light_only_labels/{z}/{x}/{y}.png",
{
attribution: "©OpenStreetMap, ©CartoDB",
pane: "labels",
}
).addTo(map);
// Load GeoJSON and add interactivity
var geoJson = L.geoJson(euData, {
// Ensure that `euData` is correctly referenced
style: function (features) {
return {
fillColor: getColor(features.properties.NAME), // Adjust color based on some property
weight: 1,
opacity: 1,
color: "white",
dashArray: "3",
fillOpacity: 0.5,
};
},
onEachFeature: function (features, layer) {
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
click: function (e) {
selectCountry(e, features.properties.NAME); // Pass country name on click
},
});
},
}).addTo(map);
// Function to highlight the feature on mouseover
function highlightFeature(e) {
var layer = e.target;
layer.setStyle({
weight: 1,
color: "#666",
dashArray: "",
fillOpacity: 0.8,
});
if (!L.Browser.ie && !L.Browser.opera && !L.Browser.edge) {
layer.bringToFront();
}
}
// Function to reset highlight on mouseout
function resetHighlight(e) {
var layer = e.target;
layer.setStyle({
weight: 1,
color: "white",
dashArray: "3",
fillOpacity: 0.5,
});
}
// Function to select a country and update dropdown
function selectCountry(e, countryName) {
var select = document.getElementById("countrySelect");
for (var i = 0; i < select.options.length; i++) {
if (select.options[i].text === countryName) {
select.selectedIndex = i;
select.dispatchEvent(new Event("change"));
break;
}
}
}
// Fetching data and updating charts
function fetchData() {
var country = document.getElementById("countrySelect").value;
var year = document.getElementById("yearSelect").value;
var product = document.getElementById("productSelect").value;
var url = `server.php?country=${encodeURIComponent(
country
)}&year=${encodeURIComponent(year)}&product=${encodeURIComponent(product)}`;
fetch(url)
.then((response) => response.json())
.then((data) => {
updateBarChart(data.monthlyData);
updatePieChart(data.categoryData);
})
.catch((error) => console.error("Error:", error));
}
document.getElementById("countrySelect").addEventListener("change", fetchData);
document.getElementById("yearSelect").addEventListener("change", fetchData);
document.getElementById("productSelect").addEventListener("change", fetchData);
// Ensure that the page is fully loaded before initializing map and fetching data
window.onload = function () {
fetchData();
};
// Color picker function for the map
function getColor(d) {
return d === 'Croatia' ||
d === 'France' ||
d === 'Italy' ||
d === 'Portugal' ||
d === 'Slovenia' ||
d === 'Spain' ||
d === 'Switzerland'
? '#34a853'
: d === 'Germany' || d === 'Belgium' || d === 'Netherlands'
? '#f18d00'
: d === 'Czech Republic' ||
d === 'Greece' ||
d === 'Hungary' ||
d === 'Poland'
? '#673ab7'
: d === 'United Kingdom'
? '#e91e63'
: d === 'Denmark' ||
d === 'Sweden' ||
d === 'Estonia' ||
d === 'Finland' ||
d === 'Latvia' ||
d === 'Lithuania' ||
d === 'Norway'
? '#4a80f5'
: '#bdbdbd'
}
// Create overlay layers
var AntwerpenP = L.marker([51.29999, 4.30758]).bindPopup("Antwerpen Production");
var WroclavP = L.marker([51.11862, 16.99842]).bindPopup("Wrocłav Productions");
var LyonP = L.marker([45.75555, 4.76737]).bindPopup("Lyon Productions");
var AntwerpenDC = L.marker([51.2999, 4.30758]).bindPopup("Antwerpen Distribution Center");
var WroclavDC = L.marker([51.11862, 16.99842]).bindPopup("Wrocłav Distribution Center");
var LyonDC = L.marker([45.75555, 4.76737]).bindPopup("Lyon Distribution Center");
var BirminghamDC = L.marker([52.42853, -1.89877]).bindPopup("Birmingham Distribution Center");
var GoteborgDC = L.marker([57.72338, 11.85666]).bindPopup("Göteborg Distribution Center");
var Plants = L.layerGroup([AntwerpenP, WroclavP, LyonP]);
var DistC = L.layerGroup([AntwerpenDC, WroclavDC, LyonDC, BirminghamDC, GoteborgDC]);
var overlayMaps = {
"Distribution Centers": DistC,
"Production Plants": Plants,
};
// Add the layer control element to map
var layerControl = L.control.layers(null, overlayMaps);
layerControl.addTo(map);
// Adjust the z-index of the layer control to ensure it appears on top
var layerControlContainer = layerControl.getContainer();
layerControlContainer.style.zIndex = "1000";
});