-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
executable file
·351 lines (307 loc) · 10.4 KB
/
index.html
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sexul candidaților</title>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
integrity="sha512-Rksm5RenBEKSKFjgI3a41vrjkw4EVPlJ3+OiI65vTjIdo9brlAacEuKOiQ5OFh7cOI1bkDwLqdLw3Zg0cRJAAQ=="
crossorigin="" />
<meta property="og:image"
content="https://pax.github.io/playground/sexul-candidatilor/sexul-candidatilor.jpg" />
<style type="text/css">
body,
html {
/* height: 100%; */
border: 0;
padding: 0;
font-family: sans-serif;
}
#map-container {
height: 100vh;
width: 100%;
}
.leaflet-container {
background-color: transparent;
}
.male {
background-color: blue;
color: white;
padding: 3px 5px;
border-right: 2px solid rgba(255, 255, 255, .5);
}
.female {
background-color: red;
color: white;
padding: 3px 5px;
}
.leaflet-popup-content {
font-size: 15px;
}
.leaflet-popup-content h3 {
margin: 2px 0;
font-size: 18px;
display: inline-block;
}
table.sexrendert {
font-size: 14px;
line-height: 1.2;
}
table.sexrendert td {
padding: 0;
}
table.sexrendert thead th {
text-align: center;
}
table.sexrendert tr td:not(.partid) {
text-align: right;
padding: 0 2px;
}
#kontrol {
position: absolute;
display: inline-block;
top: 1em;
right: 1em;
z-index: 118;
line-height: 1;
background-color: rgba(255, 255, 255, .75);
z-index: 99;
font-size: 18px;
max-width: 23em;
text-align: right;
}
#kontrol a {
display: inline-block;
padding: 1ex 1.5ex;
background-color: whitesmoke;
color: #444;
text-shadow: 1px 1px 0 white;
/* color: yellow; */
text-decoration: none;
margin: 1px 1px 0 0;
}
#kontrol a:hover {
color: whitesmoke;
background-color: #335;
text-shadow: 1px 1px 1px rgba(0, 0, 0, .2);
/* background-color: rgb(226, 225, 225); */
}
#kontrol a.active {
background-color: darkslategray;
color: white;
}
.pudmr .pudmr td,
.ppmp .ppmp td,
.ppmp .ppmp td,
.pplus .pplus td,
.pindependent .pindependent td,
.ppro-ro .ppro-ro td,
.ppnl .ppnl td,
.pusr .pusr td,
.ppsd .ppsd td {
background-color: Beige;
border-bottom: 1px solid MistyRose;
}
td.missingF{
background-color: darkgoldenrod; color: white;
}
td.missingM{
background-color: blue; color: white;
}
tr.missingF{
color: red;
}
tr.missingM{
color: blue;
}
</style>
</head>
<body>
<div id="map-wrapper">
<div id="map-container"></div>
</div>
<div id="kontrol">
<!-- <a href="#" id="zze1">1</a>
<a href="#" id="zze2">2</a>
<a href="#" id="zze3">3</a> -->
<div class="k_partide">
<a href="#" id="pUDMR" data-target="UDMR">UDMR</a><a href="#" id="pUSR" data-target="USR">USR</a><a href="#"
id="pPNL" data-target="PNL">PNL</a><a href="#" id="pPSD" data-target="PSD">PSD</a><a href="#" id="pPMP"
data-target="PMP">PMP</a><a href="#" id="pPLUS" data-target="PLUS">PLUS</a><a href="#" id="pPRM"
data-target="PRM">PRM</a><a href="#" id="pProRO" data-target="Pro RO">Pro RO</a><a href="#" id="pIndependent"
data-target="Independent">Independent</a> </div>
</div>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"
integrity="sha512-/Nsx9X4HebavoBvEBuyp3I7od5tA0UzAxs+j83KgC8PU0kgB4XiK4Lfe4y4cgBtaRJQEIFCW+oC506aPT2L1zw=="
crossorigin=""></script>
<script src="https://unpkg.com/[email protected]/dist/topojson.min.js"></script>
<script>
let map = L.map('map-container');
// map.setView([52.268, 4.998], 7);
map.setView([46, 25], 7);
/*
let bglayer_Positron = L.tileLayer('https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png', {
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> © <a href="http://cartodb.com/attributions">CartoDB</a>',
subdomains: 'abcd',
maxZoom: 19
});
let Stamen_TonerLite = L.tileLayer('https://stamen-tiles-{s}.a.ssl.fastly.net/toner-lite/{z}/{x}/{y}{r}.{ext}', {
attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> — Map data © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
subdomains: 'abcd',
minZoom: 0,
maxZoom: 20,
ext: 'png'
});
Stamen_TonerLite.addTo(map);
*/
//extend Leaflet to create a GeoJSON layer from a TopoJSON file
L.TopoJSON = L.GeoJSON.extend({
addData: function (data) {
var geojson, key;
if (data.type === "Topology") {
for (key in data.objects) {
if (data.objects.hasOwnProperty(key)) {
geojson = topojson.feature(data, data.objects[key]);
L.GeoJSON.prototype.addData.call(this, geojson);
}
}
return this;
}
L.GeoJSON.prototype.addData.call(this, data);
return this;
}
});
L.topoJson = function (data, options) {
return new L.TopoJSON(data, options);
};
//create an empty geojson layer
//with a style and a popup on click
var geojson = L.topoJson(null, {
style: shapeStyle,
onEachFeature: function (feature, layer) {
layer.bindPopup(renderUAT(feature))
}
}).addTo(map);
let queriedparty = ''
var elements = document.querySelectorAll("#kontrol a");
for (var i = 0; i < elements.length; i++) {
elements[i].addEventListener("click", function () {
queriedparty = this.getAttribute("data-target");
geojson.setStyle(shapeStylex)
resetActives()
this.classList.add('active');
});
}
function resetActives() {
document.getElementById("map-wrapper").className = 'p' + sluggify(queriedparty);
var elements2 = document.querySelectorAll("#kontrol a");
for (var ix = 0; ix < elements2.length; ix++) {
elements[ix].classList.remove('active');
}
}
function shapeStylex(feature) {
let hasparty = false
if (queriedparty && queriedparty != '') {
sxr0 = feature.properties.partide
if (queriedparty in sxr0 && sxr0[queriedparty]['partystats']) {
sxr = sxr0[queriedparty]['partystats']
hasparty = true
}
else {
sxr = feature.properties.uatstats
}
}
else {
sxr = feature.properties.uatstats
}
let colorr = hasparty ? 100 * (sxr['M'] - sxr['F']) / sxr['all'] + 150 : 55
return {
color: "whitesmoke",
opacity: .5,
weight: 1,
fillColor: 'hsl(' + colorr + ', 66%, 50%)',
fillOpacity: 0.9
}
}
function shapeStyle(feature) {
let sxr = feature.properties.uatstats
// let colorr = 50 * sxr['M'] / sxr['F'] + 50
let colorr = 100 * (sxr['M'] - sxr['F']) / sxr['all'] + 150
return {
color: "whitesmoke",
opacity: .5,
weight: 1,
fillColor: 'hsl(' + colorr + ', 66%, 50%)',
fillOpacity: 0.9
}
}
function shapeStyle3(feature) {
let sxr = feature.properties.uatstats
let colorr = 100 * (sxr.qM - sxr.qF) / (sxr.qM + sxr.qF) + 150
return {
color: "whitesmoke",
opacity: .5,
weight: 1,
fillColor: 'hsl(' + colorr + ', 66%, 50%)',
fillOpacity: 0.9
}
}
function renderUAT(feature) {
let sxr = feature.properties.uatstats
let idsex1 = 100 * (sxr.qM - sxr.qF) / (sxr.qM + sxr.qF)
let idsex2 = 100 * (sxr['M'] - sxr['F']) / sxr['all']
let idsex3 = 100 * (1 - sxr['F'] / sxr['M'])
let idsex4 = (sxr.qM - sxr.qF / sxr.qM)
let out = feature.properties.nume + ', <small>' + feature.properties.judet + '</small></div><div> <h3>' + idsex3.toFixed(1) + '%</h3> <span class="male">' + sxr['M'] + '♂</span><span class="female">' + sxr['F'] + '♀</span> <small><sup>' + idsex4.toFixed(1) + '</sup></small> '
xtbl = ''
xpartide = feature.properties.partide
for (var partyd in xpartide) {
if (xpartide.hasOwnProperty(partyd)) {
let sxp0 = xpartide[partyd]
let sxp1 = sxp0.partystats
// console.log(sxp1)
// let idsexp1 = 100 * (sxp1qM - sxp1qF) / (sxp1qM + sxp1qF)
if (sxp1 && 'M' in sxp1 && 'F' in sxp1) {
let idsexp2 = 100 * (sxp1['M'] - sxp1['F']) / sxp1['all']
let idsexp3 = 100 * (1 - sxp1['F'] / sxp1['M'])
let idsexp4 = (sxp1.qM - sxp1.qF / sxp1.qM)
hasF = sxp1.F ? '' : 'missing missingF';
hasM = sxp1.M ? '' : 'missing missingM';
xtbl += '<tr class="p' + sluggify(partyd) + ' ' + hasM + ' ' + hasF + '"><td class="partid">' + partyd + '</td><td class="' + hasF + '">' + sxp1.F + '</td><td class="' + hasM + '">' + sxp1.M + '</td><td>' + idsexp2.toFixed() + '</td><td>' + idsexp3.toFixed() + '</td></tr>'
}
// console.log(partyd + " -> " + xpartide[partyd]);
}
}
xtbl = '<table class="sexrendert"><thead><tr><th>- p -</th><th>F</th><th>M</th><th>%1</th><th>%2</th></tr></thead><tbody> ' + xtbl + ' </tbody></table>'
return out + ' <hr>' + xtbl
}
//fill: #317581;
//define a function to get and parse geojson from URL
async function getGeoData(url) {
let response = await fetch(url);
let data = await response.json();
// console.log(data)
return data;
}
//fetch the geojson and add it to our geojson layer
getGeoData('data/liste-candidati.topo.json').then(data => geojson.addData(data));
function sluggify(Text) {
return Text
.toLowerCase()
.replace(/[^\w ]+/g, '')
.replace(/ +/g, '-')
;
}
// Check that service workers are registered
if ('serviceWorker' in navigator) {
// Use the window load event to keep the page load performant
window.addEventListener('load', () => {
navigator.serviceWorker.register('assets/sw.js');
});
console.log('we have sw')
}
else {
console.log('sw ioc')
}
</script>
</body>
</html>