-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
202 lines (169 loc) · 5.83 KB
/
main.js
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
var selectedMap;
var selectedMapPop;
// Karte darstellen
function makemap(mapid) {
let map = L.map(mapid, {
center: [12, 15],
zoom: 2,
layers: L.tileLayer.provider('CartoDB.Voyager'),
maxZoom: 6,
minZoom: 2,
fadeAnimation: false
});
return map
}
// Layer Control mit iconLayer Plugin
function makeLayerControl (map) {
new L.Control.IconLayers(
[
{
title: 'Karte',
layer: L.tileLayer.provider('CartoDB.Voyager'),
icon: 'plugins/cartodb_positron.png'
},
{
title: 'Satellit',
layer: L.tileLayer.grayscale('https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', {attribution: "Tiles © Esri"
}),
icon: 'plugins/here_satelliteday.png'
}
], {
position: 'topleft',
maxLayersInRow: 1
}
).addTo(map);
}
// --- KARTE PRODUKTION TOTAL ---
// Karte Produktion total mit Funktion erzeugen
let mapproduction = makemap("mapproduction");
makeLayerControl (mapproduction);
// Choropletenfarbe bestimmen
function getProductionColor(prod) { //Funktion mit JS ternary operator (wie if eine conditional Abfrage)
return prod > 1000000 ? '#DF0000' :
prod > 100000 ? '#FA5D20' :
prod > 50000 ? '#FABB00' :
prod > 30000 ? '#FAEB20' :
prod > 10000 ? '#FAFF56' :
'#E8FF9C';
};
function productionStyle(feature) {
return { //Style Funktion
fillColor: getProductionColor(feature.properties.coffe_production),
weight: 1,
opacity: 1,
color: 'grey',
dashArray: '1',
fillOpacity: 0.7,
attribution: "Data: <a href=\"https://ourworldindata.org/grapher/coffee-bean-production\">Our world in Data/FAO</a>"
};
};
// Hovereffekt
function highlightFeature(e) {
let layer = e.target; //gehovertes Element
layer.setStyle({
weight: 1,
color: 'black',
dashArray: '1',
fillOpacity: 1
});
if (!L.Browser.ie && !L.Browser.opera && !L.Browser.edge) {
layer.bringToFront(); //Hoverhervorhebung browserspezifisch auf erste Ebene überlagern
}
info.update(layer.feature.properties);
infoPop.update(layer.feature.properties);
}
function resetHighlight(e) { // nach dem Hovern Style auf Ursprung zurücksetzen
selectedMap.resetStyle(e.target);
info.update();
infoPop.update();
}
function onEachFeature(feature, layer) {
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
});
}
selectedMap = L.geoJson(coffeeProducerCountries, {
style: productionStyle,
onEachFeature: onEachFeature
}).addTo(mapproduction);
// Wert anzeigen bei Hover
let info = L.control();
info.onAdd = function (mapid) {
this._div = L.DomUtil.create('class', 'infobox'); // create a div with a class "info"
this.update();
return this._div;
};
info.update = function (feature) {// method that we will use to update the control based on feature properties passed
this._div.innerHTML = '<h5>Kaffeeproduktion 2018</h5>' + (feature ? // Conditional ob Hover
'<b>' + feature.formal_de + '</b><br />' + feature.coffe_production + ' Tonnen'
: 'Bewege die Maus über ein Land');
};
info.addTo(mapproduction);
// --- KARTE PRODUKTION/ KOPF ---
// Karte Produktion pro Kopf mit Funktion erzeugen
let mapproductionperson = makemap("mapproductionperson");
makeLayerControl (mapproductionperson);
// Choropletenfarbe bestimmen
function getProductionColorPop(prod, pop) { //Funktion mit JS ternary operator (wie if eine conditional Abfrage)
return prod > 1000000 ? '#DF0000' :
((prod / pop)*1000) > 10 ? '#FA5D20' :
((prod / pop)*1000) > 5 ? '#FABB00' :
((prod / pop)*1000) > 2 ? '#FAEB20' :
((prod / pop)*1000) > 0.5 ? '#FAFF56' :
'#E8FF9C';
};
function productionStylePop(feature) {
return { //Style Funktion
fillColor: getProductionColorPop(feature.properties.coffe_production, feature.properties.pop_est),
weight: 1,
opacity: 1,
color: 'grey',
dashArray: '1',
fillOpacity: 0.7,
attribution: "Data: <a href=\"https://ourworldindata.org/grapher/coffee-bean-production\">Our world in Data/FAO</a>"
};
};
// Hovereffekt
function highlightFeaturePop(e) {
let layer = e.target; //gehovertes Element
layer.setStyle({
weight: 1,
color: 'black',
dashArray: '1',
fillOpacity: 1
});
if (!L.Browser.ie && !L.Browser.opera && !L.Browser.edge) {
layer.bringToFront(); //Hoverhervorhebung browserspezifisch auf erste Ebene überlagern
}
info.update(layer.feature.properties);
infoPop.update(layer.feature.properties);
}
function resetHighlightPop(e) { // nach dem Hovern Style auf Ursprung zurücksetzen
selectedMapPop.resetStyle(e.target);
info.update();
infoPop.update();
}
function onEachFeaturePop(feature, layer) {
layer.on({
mouseover: highlightFeaturePop,
mouseout: resetHighlightPop,
});
}
selectedMapPop = L.geoJson(coffeeProducerCountries, {
style: productionStylePop,
onEachFeature: onEachFeaturePop
}).addTo(mapproductionperson);
// Wert anzeigen bei Hover
let infoPop = L.control();
infoPop.onAdd = function () {
this._div = L.DomUtil.create('class', 'infobox'); // create a div with a class "info"
this.update();
return this._div;
};
infoPop.update = function (feature) {// method that we will use to update the control based on feature properties passed
this._div.innerHTML = '<h5>Kaffeeproduktion 2018 <br>pro EW</h5>' + (feature ? // Conditional ob Hover
'<b>' + feature.formal_de + '</b><br />' + Math.round((feature.coffe_production/feature.pop_est)*10000)/10 + ' Kilo/Einwohner'
: 'Bewege die Maus über ein Land');
};
infoPop.addTo(mapproductionperson);