-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
473 lines (415 loc) · 19.5 KB
/
index.html
File metadata and controls
473 lines (415 loc) · 19.5 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
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
<html>
<head>
<link href="src/style.css" rel="stylesheet" />
<script src="src/d3.v7.js"></script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Bitter:ital,wght@0,100..900;1,100..900&family=Roboto+Mono&display=swap" rel="stylesheet">
</head>
<body>
<section>
<h2>Quiet Conflicts in 2023</h2>
<p>Explore where violence is across the globe, and how it may be causing people to migrate as a result.</p>
<p class="small-text">Filter violence overall, or pick specific countries for more detail from the 30 countries with the most violent events in 2023.</p>
</section>
<div class="overall">
<div class="interactive">
<div>
<div class="dropdown-set">
<h5> Conflict Scale </h5>
<select class="dropdown" id="conflict_drop"></select>
</div>
<div class="dropdown-set">
<h5> Region </h5>
<select class="dropdown" id="region_drop"></select>
</div>
<div class="dropdown-set">
<h5> Violence Type </h5>
<select class="dropdown" id="event_type_drop"></select>
</div>
</div>
<div id="bubble"></div>
</div>
<div class ="interactive">
<div class = "info_box" id="tooltip_loc">
<h4>Country Information</h4>
</div>
<div class = "sub_box" id="migration">
<h4>Net Migration</h4>
<p class="tiny-text">The dashed line is average net migration (excluding outliers)</p>
</div>
<div class = "sub_box" id="sub_event_type">
<h4 id = "ve-country">Types of Violent Events</h4>
</div>
</div>
</div>
<div>
<p>Violent Events data drawn from the ACLED Conflict Database: <a href='https://acleddata.com/data/'> ACLED </a></p>
<p>Migration & Population numbers drawn from the World Bank databank: <a href='https://databank.worldbank.org/databases/migration'>World Bank</a></p>
</div>
<script>
// Contains:
// Primary Chart and dropdown interactions
// selection modules for secondary charts
// Data import and filtration
// Dropdown encodings
const conflict_list = ['All', 'Exclude Ukraine/Palestine']
const region_list = ['All', 'Africa','Americas','Asia','Europe','Middle East'];
const event_type_list = ['All','Battles','Explosions/Remote violence','Riots', 'Strategic developments','Violence against civilians'];
async function getData(url) {
const response = await fetch(url);
const json = await response.json();
return json
};
const bubbleUrl = "site_data/acledArray.json";
// INTERACTIVE DATA SORTING
function dataSort (data, region_sel, event_sel, conflict_sel) {
/*
Sorts the data by working through each of the 3 filters
Reformats the data so it can be sorted by the heirarchy properly
Returns the data as an object
*/
const event_sort = event_sel;
const region_sort = region_sel;
const conflict_sort = conflict_sel;
let filter_data = [];
// Check to ensure data is in a functional form
console.log("Data Before Filtration: ", typeof(data), data);
if (conflict_sort !== 'All') {
filter_data = data.filter(data => data.conflict_size == 'Rest of the World');
} else {
filter_data = data;
};
if (event_sort !== 'All') {
filter_data = filter_data.filter(data => data.event_type == event_sort);
} else {
filter_data = filter_data;
};
if (region_sort !== 'All') {
filter_data = filter_data.filter(data => data.Region == region_sort);
} else {
filter_data = filter_data;
};
let country_agg = Object.groupBy(filter_data, (({ Country }) => Country));
let out_data = [];
for (const [key, value] of Object.entries(country_agg)) {
new_obj = {};
new_obj.Country = key;
new_obj.Region = value[0].Region;
new_obj.violent_events = 0;
for (let events of value) {
new_obj.violent_events = new_obj.violent_events + events.violent_events
};
out_data.push(new_obj);
};
return out_data;
};
// Gives the set number of events for a bubble on the side of the screen
const tooltip = d3.select("#tooltip_loc")
.append("div")
.style("opacity", 0)
.attr("class", "tooltip");
const showTooltip = function(event, d) {
tooltip
.style("opacity", 1)
.html(d.data.Country + " had <b>" + d.data.violent_events + "</b> violent events in 2023 of the selected type")
.style("left", (event.x))
.style("top", (event.y));
d3.select(this).attr("fill-opacity", 1);
}
const moveTooltip = function(event, d) {
tooltip
.style("left", (event.x)/2 + "px")
.style("top", (event.y)/2+30 + "px")
}
const hideTooltip = function(event, d) {
d3.select(this).attr("fill-opacity", .6);
}
// Chart Constants
const width = 800;
const height = 600;
const margin = 1;
const pack = d3.pack()
.size([width - margin * 2, height - margin * 2])
.padding(3);
const color = d3.scaleOrdinal(d3.schemeTableau10);
const bubble = d3
.select("#bubble")
.append("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [-margin, -margin, width, height])
.attr("style", "max-width: 100%; height: auto; font: 10px sans-serif;")
.attr("text-anchor", "middle");
function addLegend(data) {
// Legend hard-coded to leave all regions in when the chart is filtered
const legend_list = ['Africa','Americas','Asia','Europe','Middle East'];
bubble.selectAll("legend_squares")
.data(legend_list)
.enter()
.append("rect")
.attr("x", 10 )
.attr("y", function(d,i){ return 450 + i*25})
.attr("width", 8)
.attr("height", 8)
.style("fill", d => color(d));
bubble.selectAll("legend_text")
.data(legend_list)
.enter()
.append("text")
.attr('class', 'legend-text')
.attr("x", 60)
.attr("y", function(d,i){ return 455 + i*25})
.text(function(d){ return d})
.style('font-family', "Roboto Mono, monospace")
.style("alignment-baseline", "middle");
}
// Conflict Dropdown
d3.select("#conflict_drop")
.selectAll('myOptions')
.data(conflict_list)
.enter()
.append('option')
.text(function (d) { return d; })
.attr("value", function (d) { return d; })
// Region Dropdown
d3.select("#region_drop")
.selectAll('myOptions')
.data(region_list)
.enter()
.append('option')
.text(function (d) { return d; })
.attr("value", function (d) { return d; })
//Event Type Dropdown
d3.select("#event_type_drop")
.selectAll('myOptions')
.data(event_type_list)
.enter()
.append('option')
.text(function (d) { return d; })
.attr("value", function (d) { return d; })
// Very similar, but bubbleupdate gets variables for dropdown selection and leaves the legend untouched
function firstBubble(url) {getData(url).then(data => {bubbleChart(data, 'All', 'All', 'All')})};
function bubbleUpdate(url, regSel, evSel, cSel) {getData(url).then(data => {bubbleChart(data, regSel, evSel, cSel)})};
// Creates the first bubble chart and sets the legend
firstBubble(bubbleUrl);
let legend = false;
/////////////////// THE BUBBLE CHART /////////////////////////////////////////////////
function bubbleChart(data, region_sel, event_sel, conflict_sel) {
// Create new data with the selection
console.log("Update Sel:",region_sel, event_sel, conflict_sel);
const out_data = dataSort(data, region_sel, event_sel, conflict_sel);
// generate the correct data format
const out_root = pack(d3.hierarchy({children: out_data})
.sum(d => d.violent_events));
// clear the old chart
bubble.selectAll("circle").remove();
bubble.selectAll("#c-label").remove();
const bubbleArray = bubble.selectAll("g")
.data(out_root.leaves(), function(d) { return d ? d.data.Country : this.id;})
.join(
enter => {
enter.append("g")
.attr("transform", d => `translate(${d.x},${d.y})`)
},
update => {
update.attr("transform", d => `translate(${d.x},${d.y})`)},
exit => {
exit.remove()}
);
const circles = bubble.selectAll("g")
.append("circle")
.transition().duration(1000)
.attr("fill-opacity", 0.6)
.attr("fill", d => color(d.data.Region))
.attr("r", d => d.r)
.attr("Country", d => d.data.Country)
.attr("violent_events", d => d.data.violent_events);
const text = bubble.selectAll('g')
.append("text")
.attr('class', 'c-label')
.attr('color', '#555657')
.text(d => d.data.Country)
.attr("x", 0)
.style('font-family', "Roboto Mono, monospace")
.attr("y", 0);
bubble.selectAll('circle')
.on("mouseover", showTooltip)
.on("mousemove", moveTooltip)
.on("mouseleave", hideTooltip);
d3.selectAll('circle').on('click', function(event,d) {
const countryClicked = d.data.Country;
console.log("Clicked country", countryClicked);
migCreator(barUrl, countryClicked);
barCreator(barUrl, countryClicked);
});
if (legend === false) {
addLegend(out_root.leaves());
legend = true;
}
}
// Interactive Selection
d3.select("#conflict_drop").on("change", function(event,d) {
// recover the option that has been chosen
const conflict_sel = d3.select(this).property("value");
const region_sel = d3.select('#region_drop').property("value");
const event_sel = d3.select('#event_type_drop').property("value");
// run the updateChart function with this selected option
bubbleUpdate(bubbleUrl, region_sel, event_sel, conflict_sel);
});
d3.select("#region_drop").on("change", function(event,d) {
// recover the option that has been chosen
const conflict_sel = d3.select('#conflict_drop').property("value");
const region_sel = d3.select(this).property("value");
const event_sel = d3.select('#event_type_drop').property("value");
// run the updateChart function with this selected option
bubbleUpdate(bubbleUrl, region_sel, event_sel, conflict_sel);
});
d3.select("#event_type_drop").on("change", function(event,d) {
// recover the option that has been chosen
const conflict_sel = d3.select('#conflict_drop').property("value");
const region_sel = d3.select('#region_drop').property("value");
const event_sel = d3.select(this).property("value");
// run the updateChart function with this selected option
bubbleUpdate(bubbleUrl, region_sel, event_sel, conflict_sel);
});
</script>
<script>
//////// Migration and Violent Event Chart Scripts /////////
// data sources
const migUrl = 'site_data/wbFinal.csv';
const barUrl = 'site_data/acled.csv';
async function getBarData(url) {
const data = await d3.csv(url);
const result = data
return result };
function sortBarData(data, barFilter) {
filter_data = data.filter(d => d.Country === barFilter);
return filter_data };
// set the dimensions and margins of the graphs
const bar_margin = {top: 10, right: 10, bottom: 40, left: 140},
bar_width = 400 - bar_margin.left - bar_margin.right,
bar_height = 180 - bar_margin.top - bar_margin.bottom;
const mig_margin = {top: 10, right: 60, bottom: 40, left: 60},
mig_width = 400 - mig_margin.left - mig_margin.right,
mig_height = 180 - mig_margin.top - mig_margin.bottom;
// append the svg objects to the body of the page
const barSvg = d3.select("#sub_event_type")
.append("svg")
.attr("width", bar_width + bar_margin.left + bar_margin.right)
.attr("height", bar_height + bar_margin.top + bar_margin.bottom)
.append("g")
.attr("transform", `translate(${bar_margin.left}, ${bar_margin.top})`);
const migSvg = d3.select("#migration")
.append("svg")
.attr("width", mig_width + mig_margin.left + mig_margin.right)
.attr("height", mig_height + mig_margin.top + mig_margin.bottom)
.append("g")
.attr("transform", `translate(${mig_margin.left}, ${mig_margin.top})`);
const barCreator = function(barURL, barFilter) { d3.csv(barUrl).then(function barChart(data) {
// Get correct data and clear old chart
sorted_data = sortBarData(data, barFilter);
console.log("Bar Data:",sorted_data)
barSvg.selectAll("g").remove();
barSvg.selectAll("rect").remove();
console.log("Country for Bar: ", sorted_data[0].Country);
// X axis code
const x = d3.scaleLinear()
//Sets the width of the bar chart to the largest violent event count for the country
.domain([0, Math.max(...sorted_data.map(row => Number(row.violent_events)))])
.range([ 0, bar_width]);
barSvg.append("g")
.attr("transform", `translate(0, ${bar_height})`)
.call(d3.axisBottom(x))
.selectAll("text")
.attr("transform", "translate(-10,0)rotate(-45)")
.style("text-anchor", "end");
// Y axis code
const y = d3.scaleBand()
.range([ 0, bar_height ])
.domain(sorted_data.map(d => d.event_type))
.padding(.1);
barSvg.append("g")
.call(d3.axisLeft(y))
//Bars
barSvg.selectAll("myRect")
.data(sorted_data)
.join("rect")
.attr("x", x(0) )
.attr("y", d => y(d.event_type))
.attr("width", d => x(d.violent_events))
.attr("height", y.bandwidth())
.attr("fill", "#A51303")
// Flexible label
d3.select('#ve-country')
.html("Types of Violent Events in " + sorted_data[0].Country);
})};
const migCreator = function(migURL, migFilter) { d3.csv(migUrl).then(function barChart(data) {
sorted_data = sortBarData(data, migFilter);
console.log("Mig Data:",sorted_data)
migSvg.selectAll("g").remove();
migSvg.selectAll("rect").remove();
migSvg.selectAll("line").remove();
migSvg.selectAll("text").remove();
// X axis code
const x = d3.scaleBand()
.range([ 0, mig_width ])
.domain(sorted_data.map(d => d.Country))
.padding(0.2);
migSvg.append("g")
.attr("transform", `translate(0, ${mig_height})`)
.call(d3.axisBottom(x))
.selectAll("text")
.style("text-anchor", "center");
// Add Y axis
const y = d3.scaleLinear()
// adjust this based on the min/max of migration per 1000 to cover outliers
.domain([
Math.min(-4, sorted_data.map(row => Number(row.mg_per_1000))),
Math.max(sorted_data[0].mg_per_1000, 4)])
.range([ mig_height, 0]);
migSvg.append("g")
.call(d3.axisLeft(y));
// Chooses color for bar based on net migration
function pickColor() {
if (sorted_data[0].mg_per_1000 > 0) {
return '#0395A5'; }
else {
return '#A51303';
}}
barColor = pickColor();
//Bars
migSvg.selectAll("myRect")
.data(sorted_data)
.join("rect")
.attr("x", d => x(d.Country)*2)
.attr("y", d => d.mg_per_1000 < 0 ? y(0) : y(d.mg_per_1000))
.attr("width", x.bandwidth()/2)
.attr("height", d => d.mg_per_1000 < 0 ? y(d.mg_per_1000) - y(0) : y(0) - y(d.mg_per_1000))
.attr("fill", barColor);
// Y axis label
migSvg.append("text")
.attr('x', -120)
.attr('y', -30)
.attr('font-size', '.6em')
.attr('transform', 'rotate(-90)')
.text("Net Migration per 1000 People");
// 0 baseline
migSvg.append("line")
.style("stroke", "black")
.attr("x1", 0)
.attr("y1", y(0))
.attr("x2", 280)
.attr("y2", y(0));
// Mean Migration Line
migSvg.append("line")
.style("stroke", "black")
.style("stroke-dasharray", ("3, 3"))
.attr("x1", 0)
.attr("y1", (2*y(0)-y(0.7105)))
.attr("x2", 280)
.attr("y2", (2*y(0)-y(0.7105)));
})};
</script>
</body>
</html>