-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
510 lines (424 loc) · 17.7 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<title>US Presidential Elections 2004-2024</title>
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous" />
<!-- Leaflet CSS -->
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=" crossorigin="" />
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css2?family=Halant:ital,wght@0,400;0,800;1,800&display=swap"
rel="stylesheet" />
<link href="https://fonts.googleapis.com/css2?family=Nunito Sans:ital,wght@0,400;0,800;1,800&display=swap"
rel="stylesheet" />
<style>
body {
background: #f8f9fa;
font-family: "Halant";
font-weight: 400;
font-size: 100%;
}
footer {
position: fixed;
bottom: 0;
width: 100%;
height: 3rem;
}
h1 {
font-weight: 900;
color: rgb(61, 61, 61);
font-family: "Nunito Sans";
font-variant: small-caps;
font-size: 50px;
letter-spacing: 2px;
word-spacing: 2px;
}
h2 {
font-weight: 500;
color: rgb(77, 77, 77);
font-family: "Halant";
font-style: italic;
}
h3 {
font-weight: 400;
color: rgb(77, 77, 77);
font-family: "Halant";
}
p {
line-height: 1.7rem;
font-family: "Nunito Sans";
}
/* The slider itself */
.slidecontainer {
width: 20%;
}
.slider {
width: 100%;
height: 15px;
border-radius: 5px;
background: #b1cef8;
outline: none;
opacity: 0.7;
-webkit-transition: .2s;
transition: opacity .2s;
}
.slider:hover {
opacity: 1;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 25px;
height: 25px;
border-radius: 50%;
background: #0d6efd;
cursor: pointer;
}
.slider::-moz-range-thumb {
width: 25px;
height: 25px;
border-radius: 50%;
border-color: #313131;
background: #0d6efd;
cursor: pointer;
}
#map {
/* height is set in JS */
background: #2d2f31;
height: 100%;
}
.legend {
width: 400px;
height: 90px;
font-size: 1rem;
border-radius: 8px;
max-width: 250px;
font-family: "Halant";
padding: 1rem;
text-align: left;
background: #f8f9fa;
color: rgba(54, 54, 54, 0.8);
box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
}
.legend h2 {
font-size: 1.5em;
font-weight: normal;
color: #00AEF3;
font-style: normal;
}
.legend h3 {
font-size: 1.5em;
font-weight: normal;
color: #E81B23;
}
a:hover {
color: rgb(130, 131, 132);
text-decoration: none;
}
.leaflet-container {
font-family: "Halant", sans-serif;
}
.leaflet-popup-content-wrapper {
background: rgb(62, 62, 62);
color: rgb(244, 244, 244);
border: none;
padding: 10px;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
}
.leaflet-popup-content p,
.leaflet-popup-content a {
line-height: 1.6rem;
font-size: 1.1rem;
font-weight: 400;
}
.leaflet-popup-content h3 {
font-size: 1.8rem;
font-weight: 400;
}
/* Custom Tool tips */
.leaflet-tooltip-own {
background: rgba(58, 58, 58, 0.955);
color: rgb(244, 244, 244);
border: none;
font-size: 1rem;
border-radius: 5px;
padding: 10px;
font-family: "Nunito Sans", sans-serif;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
}
.leaflet-tooltip-left:before {
right: 0;
margin-right: -12px;
border-left-color: rgba(0, 0, 0, 0.4);
}
.leaflet-tooltip-right:before {
left: 0;
margin-left: -12px;
border-right-color: rgba(0, 0, 0, 0.4);
}
/* Small devices (landscape phones, 576px and up) */
@media (min-width: 576px) {}
/* Medium devices (tablets, 768px and up) */
@media (min-width: 768px) {}
/* Large devices (desktops, 992px and up) */
@media (min-width: 992px) {}
/* Extra large devices (large desktops, 1200px and up) */
@media (min-width: 1200px) {}
</style>
</head>
<body>
<div class="container-fluid">
<header class="row bg-light text-grey p-2">
<div class="col-8">
<h1>Four More Years</h1>
<h2>US Presidential Elections 2004-2024</h2>
</div>
<div class="col-4 align-self-center">
<p class="d-flex justify-content-end my-auto">
<a class="btn btn-primary" data-bs-toggle="offcanvas" href="#offcanvasExample" role="button"
aria-controls="offcanvasExample">
Map Info and data
</a>
</p>
</div>
</header>
<!-- ui is outside of container-fluid and will be dynamically added to map -->
<div id="sliderId" class="slidecontainer">
<!-- slider does not work great in continous, so added steps to make it move every 4 years -->
<input type="range" min="2004" max="2024" value="2004" class="slider" id="myRange" step="4">
<h3><span id="demo"></span></h3>
</div>
<section class="row">
<div class="p-0">
<div id="map"></div>
</div>
</section>
<footer class="row bg-light text-grey p-2">
<p class="text-center"></p>
</footer>
</div>
<div class="offcanvas offcanvas-start bg-light text-grey" tabindex="-1" id="offcanvasExample"
aria-labelledby="offcanvasExampleLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasExampleLabel">Map Info and data</h5>
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<h3 class="py-2">About this map</h3>
<p>
This interactive map shows US Presidential Election results from 2004 to 2024 by state. Detailed
election results data
is provided for each state when hovering over it.
</p>
<p>Use the slider on the top-left side of the map to inspect the results for each election year.</p>
<h3 class="py-2">Data</h3>
<p>
Election data from 2004 to 2020 was collected from the
<a href="https://doi.org/10.7910/DVN/42MVDX">MIT Election Data and Science Lab</a>.
</p>
<p>
Election data for 2024 and the Electoral College seats results were collected from the
<a href="https://uselectionatlas.org">Dave Leip's Atlas of U.S Presidential Elections</a>.
</p>
<p>
The final data compilation by the author can be accessed
<a
href="https://github.com/alexmunozviso/Election-Map-2004-2024/blob/main/data/us_election2004_2024.csv">here</a>.
</p>
<hr />
<ul class="list-unstyled">
<li>
authored by
<a href="https://github.com/alexmunozviso">Àlex Muñoz Viso</a>
in fulfilment of the requirements for the final project of the <a
href="https://github.com/newmapsplus">New Maps Plus</a> graduate certificate in digital mapping.
</li>
<li>More information about this project can be found <a
href="https://github.com/alexmunozviso/Election-Map-2004-2024">here</a>.
</li>
</ul>
</div>
</div>
<!-- legend is outside of container-fluid and will be dynamically added to map -->
<div class="legend d-flex flex-column px-3 py-2" id="legend"></div>
<!-- end ui-controls -->
<!-- Add Bootstrap -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"></script>
<!-- then Leaflet JS -->
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin=""></script>
<!-- then Simple Statistics -->
<script src="https://unpkg.com/[email protected]/dist/simple-statistics.min.js"></script>
<script>
var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
output.innerHTML = slider.value;
slider.oninput = function () {
output.innerHTML = this.value;
}
// set global variables for header, map container, and footer
const header = document.querySelector("header");
const mapContainer = document.querySelector("#map");
const footer = document.querySelector("footer");
// set map height to fill window
mapContainer.style.height =
window.innerHeight - header.offsetHeight - footer.offsetHeight + "px";
// initial Leaflet map options
const options = {
zoomSnap: 0.1,
center: [40.00, -100.00],
zoom: 4,
zoomControl: false,
};
// create Leaflet map and apply options
const map = L.map("map", options);
new L.control.zoom({ position: "bottomright" }).addTo(map);
// request a basemap tile layer and add to the map
L.tileLayer(
'https://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}{r}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions">CARTO</a>',
subdomains: 'abcd',
maxZoom: 20,
}
).addTo(map);
fetch("data/us_50m_states_election2004_2024.geojson")
.then(function (result) { return result.json() })
.then(function (data) {
const dataLayer = L.geoJson(data, {
style: function (feature) {
let props = feature.properties
let winner = props["y2004"]
if (winner === "Republican") {
return {
fillColor: "#E81B23",
color: "Black",
weight: 1.5,
fillOpacity: 1,
};
} else {
return {
fillColor: "#00AEF3",
color: "Black",
weight: 1.5,
fillOpacity: 1,
};
}
},
onEachFeature: function (feature, layer) {
let props = feature.properties
let repPopularVote = props[`repVotes2004`]
let repPlectoralVote = props[`repEleCol2004`]
let demPopularVote = props[`demVotes2004`]
let demElectoralVote = props[`demEleCol2004`]
layer.on("mouseover", function (e) {
layer.setStyle({ color: 'yellow', weight: 5 })
})
layer.on("mouseout", function (e) {
layer.setStyle({ color: 'black', weight: 1.5 })
})
layer.bindTooltip(`
<h3>${props["NAME"]}</h3>
Democratic Popular Vote: ${demPopularVote.toLocaleString()} <br>
Republican Popular Vote: ${repPopularVote.toLocaleString()} <br>
Democratic Electoral Seats: ${demElectoralVote.toLocaleString()} <br>
Republican Electoral Seats: ${repPlectoralVote.toLocaleString()}`)
}
}).addTo(map)
//draw the first legend when initiating the map
drawLegend("2004")
updateMap(dataLayer)
})
// declare the function to remove the legend before updating it
function removeLegend() {
const legendDiv = document.querySelectorAll('.legend')
legendDiv.forEach(function (div) {
div.remove()
})
}
function drawLegend(year) {
const electionWinnerJson = {
2004: {
"demCandidate": "John Kerry 251",
"RepCandidate": "George W. Bush 286"
}, 2008: {
"demCandidate": "Barack Obama 365",
"RepCandidate": "John McCain 173"
}, 2012: {
"demCandidate": "Barack Obama 332",
"RepCandidate": "Mitt Romney 206"
}, 2016: {
"demCandidate": "Hillary Clinton 227",
"RepCandidate": "Donald Trump 304"
}, 2020: {
"demCandidate": "Joe Biden 306",
"RepCandidate": "Donald Trump 232"
}, 2024: {
"demCandidate": "Kamala Harris 226",
"RepCandidate": "Donald Trump 312"
},
}
// call the function to get rid of the prior legend before drawing the next one after using the slider
removeLegend()
let legend = L.control({ position: "topright" })
let data = electionWinnerJson[year]
let dem = data["demCandidate"]
let rep = data["RepCandidate"]
legend.onAdd = function () {
const div = L.DomUtil.create("div", "legend")
div.innerHTML = `<h2>${dem}</i></h2>
<h3>${rep}</h3>`
return div
}
legend.addTo(map)
}
function updateMap(leafletObj) {
const sliderValue = document.getElementById("sliderId")
sliderValue.addEventListener("input", function (e) {
let sliderValue = e.srcElement.value
const dataLayer = leafletObj.eachLayer(function (layer) {
let props = layer.feature.properties
let winner = props[`y${sliderValue}`]
let repPopularVote = props[`repVotes${sliderValue}`]
let repPlectoralVote = props[`repEleCol${sliderValue}`]
let demPopularVote = props[`demVotes${sliderValue}`]
let demElectoralVote = props[`demEleCol${sliderValue}`]
layer.on("mouseover", function (e) {
layer.setStyle({ color: 'yellow', weight: 5 })
})
layer.on("mouseout", function (e) {
layer.setStyle({ color: 'black', weight: 1.5 })
})
layer.bindTooltip(`
<h3>${props["NAME"]}</h3>
Democratic Popular Vote: ${demPopularVote.toLocaleString()} <br>
Republican Popular Vote: ${repPopularVote.toLocaleString()} <br>
Democratic Electoral Seats: ${demElectoralVote.toLocaleString()} <br>
Republican Electoral Seats: ${repPlectoralVote.toLocaleString()}`)
if (winner === "Republican") {
layer.setStyle({
fillColor: "#E81B23",
color: "Black",
weight: 1.5,
fillOpacity: 1,
})
} else {
layer.setStyle({
fillColor: "#00AEF3",
color: "Black",
weight: 1.5,
fillOpacity: 1,
})
}
})
drawLegend(sliderValue)
})
}
</script>
</body>
</html>