-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathmap6.html
417 lines (349 loc) · 15.2 KB
/
map6.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Earthquakes in Japan</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://fonts.googleapis.com/css?family=Titillium+Web|Oswald" rel="stylesheet">
<link href="https://api.mapbox.com/mapbox-gl-js/v2.8.1/mapbox-gl.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css">
<link rel="stylesheet" href="css/c3.min.css">
<style>
body {
margin: 0;
padding: 0;
font-family: 'Titillium Web', serif;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
#info {
z-index: 1000;
position: absolute;
width: 500px;
top: 1%;
bottom: 1%;
left: .5%;
padding: 40px 15px 900px 15px;
background: #343a40;
color: white;
}
/* social media icons */
#info a {
color: lightgray;
}
#info a:hover {
color: white;
opacity: 0.3;
}
#title {
font-size: 25px;
font-family: 'Oswald', sans-serif;
}
/* social media icons */
#title span {
font-size: 14px;
float: right;
margin-right: 10px;
}
#desc {
text-align: center;
color: lightgray;
font-size: medium;
font-weight: bold;
margin-bottom: 0px;
}
#earthquake-count {
margin: 0;
text-align: center;
color: orange;
font-size: 50px;
}
#earthquake-chart {
top: 20px;
}
#footer {
position: absolute;
bottom: 0px;
margin-bottom: 20px;
margin-right: 10px;
font-size: 13px;
line-height: 150%;
color: lightgray;
}
/* the layout of the legend panel */
#legend {
position: absolute;
bottom: 0;
right: 0;
width: 110px;
background: #fff;
margin-right: 20px;
margin-bottom: 40px;
padding: 10px 20px 10px 10px;
border-radius: 3px;
text-align: center;
font-family: 'Open Sans', sans-serif;
}
/* each line for a break */
.break {
position: relative;
margin: 0px;
padding: 0px;
}
/* basic style for a dot/circle */
.dot {
display: inline-block;
margin-top: 5px;
border-radius: 50%;
opacity: 0.6;
}
/* the label for the dot */
.dot-label {
position: absolute;
top: 0;
right: 0;
font-style: italic;
}
</style>
<script src="https://api.mapbox.com/mapbox-gl-js/v2.8.1/mapbox-gl.js"></script>
<script src="js/d3.js"></script>
<script src="js/c3.min.js"></script>
</head>
<body>
<div id="info">
<div id="title">
Earthquakes in Japan - September 2017
<span><a href="https://twitter.com/UW" target="_blank"><i class="bi bi-twitter"></i></a></span>
<span><a href="https://github.com/jakobzhao/geog458" target="_blank"><i class="bi bi-github"></i></a></span>
<span id="reset"><a href="#">reset</a></span>
</div>
<div id="count" class="card">
<h5 id="desc"> # Earthquakes in the displaying region</h5>
<p id="earthquake-count"></p>
</div>
<div id="earthquake-chart"></div>
<div id="footer">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum
dolor sit amet,
consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit
in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet,
consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit
in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.
</div>
</div>
<div id="map"></div>
<div id="legend"></div>
<script>
// assign the access token
mapboxgl.accessToken =
'pk.eyJ1IjoiamFrb2J6aGFvIiwiYSI6ImNpcms2YWsyMzAwMmtmbG5icTFxZ3ZkdncifQ.P9MBej1xacybKcDN_jehvw';
// declare the map object
let map = new mapboxgl.Map({
container: 'map', // container ID
style: 'mapbox://styles/mapbox/dark-v10',
zoom: 5, // starting zoom
minZoom: 5,
center: [138, 38] // starting center
});
// declare the coordinated chart as well as other variables.
let earthquakeChart = null,
magnitude = {},
numEarthquakes = 0;
// create a few constant variables.
const grades = [4, 5, 6],
colors = ['rgb(208,209,230)', 'rgb(103,169,207)', 'rgb(1,108,89)'],
radii = [5, 15, 20];
// create the legend object and anchor it to the html element with id legend.
const legend = document.getElementById('legend');
//set up legend grades content and labels
let labels = ['<strong>Magnitude</strong>'],
vbreak;
//iterate through grades and create a scaled circle and label for each
for (var i = 0; i < grades.length; i++) {
vbreak = grades[i];
// you need to manually adjust the radius of each dot on the legend
// in order to make sure the legend can be properly referred to the dot on the map.
dot_radii = 2 * radii[i];
labels.push(
'<p class="break"><i class="dot" style="background:' + colors[i] + '; width: ' + dot_radii +
'px; height: ' +
dot_radii + 'px; "></i> <span class="dot-label" style="top: ' + dot_radii / 2 + 'px;">' + vbreak +
'</span></p>');
}
const source =
'<p style="text-align: right; font-size:10pt">Source: <a href="https://earthquake.usgs.gov/earthquakes/">USGS</a></p>';
// join all the labels and the source to create the legend content.
legend.innerHTML = labels.join('') + source;
// define the asynchronous function to load geojson data.
async function geojsonFetch() {
// Await operator is used to wait for a promise.
// An await can cause an async function to pause until a Promise is settled.
let response;
response = await fetch('assets/earthquakes.geojson');
earthquakes = await response.json();
//load data to the map as new layers.
//map.on('load', function loadingData() {
map.on('load', () => { //simplifying the function statement: arrow with brackets to define a function
// when loading a geojson, there are two steps
// add a source of the data and then add the layer out of the source
map.addSource('earthquakes', {
type: 'geojson',
data: earthquakes
});
map.addLayer({
'id': 'earthquakes-point',
'type': 'circle',
'source': 'earthquakes',
'minzoom': 5,
'paint': {
// increase the radii of the circle as mag value increases
'circle-radius': {
'property': 'mag',
'stops': [
[grades[0], radii[0]],
[grades[1], radii[1]],
[grades[2], radii[2]]
]
},
// change the color of the circle as mag value increases
'circle-color': {
'property': 'mag',
'stops': [
[grades[0], colors[0]],
[grades[1], colors[1]],
[grades[2], colors[2]]
]
},
'circle-stroke-color': 'white',
'circle-stroke-width': 1,
'circle-opacity': 0.6
}
},
'waterway-label' // make the thematic layer above the waterway-label layer.
);
// click on each dot to view magnitude in a popup
map.on('click', 'earthquakes-point', (event) => {
new mapboxgl.Popup()
.setLngLat(event.features[0].geometry.coordinates)
.setHTML(`<strong>Magnitude:</strong> ${event.features[0].properties.mag}`)
.addTo(map);
});
// the coordinated chart relevant operations
// found the the magnitudes of all the earthquakes in the displayed map view.
magnitudes = calEarthquakes(earthquakes, map.getBounds());
// enumerate the number of earthquakes.
numEarthquakes = magnitudes[4] + magnitudes[5] + magnitudes[6];
// update the content of the element earthquake-count.
document.getElementById("earthquake-count").innerHTML = numEarthquakes;
// add "mag" to the beginning of the x variable - the magnitude, and "#" to the beginning of the y variable - the number of earthquake of similar magnitude.
x = Object.keys(magnitudes);
x.unshift("mag")
y = Object.values(magnitudes);
y.unshift("#")
// generate the chart
earthquakeChart = c3.generate({
size: {
height: 350,
width: 460
},
data: {
x: 'mag',
columns: [x, y],
type: 'bar', // make a bar chart.
colors: {
'#': (d) => {
return colors[d["x"]];
}
},
onclick: function (
d) { // update the map and sidebar once the bar is clicked.
let floor = parseInt(x[1 + d["x"]]),
ceiling = floor + 1;
// combine two filters, the first is ['>=', 'mag', floor], the second is ['<', 'mag', ceiling]
// the first indicates all the earthquakes with magnitude greater than floor, the second indicates
// all the earthquakes with magnitude smaller than the ceiling.
map.setFilter('earthquakes-point',
['all',
['>=', 'mag', floor],
['<', 'mag', ceiling]
]);
}
},
axis: {
x: { //magnitude
type: 'category',
},
y: { //count
tick: {
values: [10, 20, 30, 40]
}
}
},
legend: {
show: false
},
bindto: "#earthquake-chart" //bind the chart to the place holder element "earthquake-chart".
});
});
//load data to the map as new layers.
//map.on('load', function loadingData() {
map.on('idle', () => { //simplifying the function statement: arrow with brackets to define a function
magnitudes = calEarthquakes(earthquakes, map.getBounds());
numEarthquakes = magnitudes[4] + magnitudes[5] + magnitudes[6];
document.getElementById("earthquake-count").innerHTML = numEarthquakes;
x = Object.keys(magnitudes);
x.unshift("mag")
y = Object.values(magnitudes);
y.unshift("#")
// after finishing each map reaction, the chart will be rendered in case the current bbox changes.
earthquakeChart.load({
columns: [x, y]
});
});
}
// call the geojson loading function
geojsonFetch();
function calEarthquakes(currentEarthquakes, currentMapBounds) {
let magnitudesClasses = {
4: 0,
5: 0,
6: 0
};
currentEarthquakes.features.forEach(function (d) { // d indicate a feature of currentEarthquakes
// contains is a spatial operation to determine whether a point within a bbox or not.
if (currentMapBounds.contains(d.geometry.coordinates)) {
// if within, the # of the earthquake in the same magnitude increase by 1.
magnitudesClasses[Math.floor(d.properties.mag)] += 1;
}
})
return magnitudesClasses;
}
// capture the element reset and add a click event to it.
const reset = document.getElementById('reset');
reset.addEventListener('click', event => {
// this event will trigger the map fly to its origin location and zoom level.
map.flyTo({
zoom: 5,
center: [138, 38]
});
// also remove all the applied filters
map.setFilter('earthquakes-point', null)
});
</script>
</body>
</html>