-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
585 lines (525 loc) · 23.5 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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src='https://d3js.org/d3.v5.min.js'></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<style> circle {
stroke: black;
opacity: 0.2
}
.annotation {
opacity: 1;
position: absolute;
width: 120px;
height: 140px;
text-align: center;
background-color: white;
border: 3px solid green;
border-radius: 10px;
padding: 2px
}
</style>
<body onload='init()'>
<div class="page-header">
<h1>CS416 Project 2 - Week 9: <small>Narrative Visualization</small></h1>
</div>
<div class="row scatter">
<div id="div_template" class="col-sm-9">
</div>
<div class="col-sm-3">
<h3>Navigation:</h3>
<div class="btn-toolbar" role="toolbar" aria-label="...">
<div class="btn-group" role="group" aria-label="...">
<p>
<div id="button1" class="btn btn-primary" onclick="changeScene(1)">1</div>
<div id="button2" class="btn btn-default" onclick="changeScene(2)">2</div>
<div id="button3" class="btn btn-default" onclick="changeScene(3)">3</div>
<div id="button4" class="btn btn-default" onclick="changeScene(4)">4</div>
<div id="button5" class="btn btn-default" onclick="changeScene(5)">5</div>
<div id="button6" class="btn btn-default" onclick="changeScene(6)">6</div>
</p>
</div>
</div>
<div id="scene-desc" class="">
<p>Looking at the chart, we can see a clear relationship between life expectance and GDP per Capita.
People tends to have a lower life expectance in poor countries, and on the contrary the people that
live in wealthy countries tend to have a higher life expectancy.</p>
</div>
<h3>Year 2018</h3>
<div id="legend-group" class="col-sm-3">
<div class="legendTitle" style="font-size: 12px;">REGION</div>
<div id="legend"></div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
<script>
var margin = {left: 50, top: 20, right: 20, bottom: 20},
width = 800 - margin.left - margin.right,
height = width * 2 / 3;
var globalSceneNumber = 1;
var showGraph1 = true;
var showGraph2 = false;
var showToolTip = false;
var showAnnotation1 = true;
var showAnnotation2 = false;
var showAnnotation3 = false;
var showAnnotation4 = false;
var showYearSelection = false;
var regionSelect = "";
var showLegendSelection = false;
var highlightOpacity = 0.8;
var normalOpacity = 0.4;
function resetScene(sceneNumber) {
d3.select("#div_template").html("")
d3.select("#legend").html("")
d3.selectAll(".btn").attr("class", "btn btn-default")
d3.select("#button" + sceneNumber).attr("class", "btn btn-primary")
init()
}
function changeScene(sceneNumber) {
globalSceneNumber = sceneNumber;
if (globalSceneNumber === 1) {
showToolTip = false;
showGraph1 = true;
showGraph2 = false;
showAnnotation1 = true;
showAnnotation2 = false;
showAnnotation3 = false;
showAnnotation4 = false;
showYearSelection = false;
regionSelect = ""
showLegendSelection = false;
d3.select("#scene-desc")
.html("<p>Looking at the chart, we can see a clear relationship between life expectancy and GDP per Capita." +
"People tends to have a lower life expectance in poor countries, and on the contrary the people that live in wealthy countries tend to have a higher life expectancy.</p>")
resetScene(1);
}
if (globalSceneNumber === 2) {
showToolTip = false;
showGraph1 = true;
showGraph2 = false;
showAnnotation1 = false;
showAnnotation2 = true;
showAnnotation3 = false;
showAnnotation4 = false;
showYearSelection = false;
regionSelect = "Europe"
showLegendSelection = false;
d3.select("#scene-desc")
.html("<p>But there are still regional difference. For example Europe is different. " +
"It seems all European countries tend to have very higher life expectancy, though their GDP per Capita vary much.</p>")
d3.select("#div_template").html("")
resetScene(2)
}
if (globalSceneNumber === 3) {
showToolTip = true;
showGraph1 = true;
showGraph2 = false;
showAnnotation1 = false;
showAnnotation2 = false;
showAnnotation3 = false;
showAnnotation4 = false;
showYearSelection = true;
regionSelect = ""
showLegendSelection = true;
d3.select("#scene-desc")
.html("<h2>Explore with your mouse!</h2><br><p>" +
"Move your mouse over the legend and the circles.</p>")
resetScene(3)
}
if (globalSceneNumber === 4) {
showToolTip = false;
showGraph1 = false;
showGraph2 = true;
showAnnotation1 = false;
showAnnotation2 = false;
showAnnotation3 = true;
showAnnotation4 = false;
showYearSelection = false;
regionSelect = ""
showLegendSelection = false;
d3.select("#scene-desc")
.html("<p>Looking at the chart, we can see a clear relationship between life expectancy and Health Expenditure per Capita." +
"People tends to have a lower life expectance in countries with lower healthcare expenditure per capita vs country with higher rates.</p>")
resetScene(4);
}
if (globalSceneNumber === 5) {
showToolTip = false;
showGraph1 = false;
showGraph2 = true;
showAnnotation1 = false;
showAnnotation2 = false;
showAnnotation3 = false;
showAnnotation4 = true;
showYearSelection = false;
regionSelect = "Europe"
showLegendSelection = false;
d3.select("#scene-desc")
.html("<p>But there are still regional difference. For example Europe is different. " +
"It seems all European countries tend to have very higher life expectancy, though their Health Expenditure per Capita vary much.</p>")
d3.select("#div_template").html("")
resetScene(5)
}
if (globalSceneNumber === 6) {
showToolTip = true;
showGraph1 = false;
showGraph2 = true;
showAnnotation1 = false;
showAnnotation2 = false;
showAnnotation3 = false;
showAnnotation4 = false;
showYearSelection = true;
regionSelect = ""
showLegendSelection = true;
d3.select("#scene-desc")
.html("<h2>Explore with your mouse!</h2><br><p>" +
"Move your mouse over the legend and the circles.</p>")
resetScene(6)
}
}
async function init() {
//console.log('init function started');
// create a tooltip
var Tooltip = d3.select("#div_template")
.append("div")
.style("opacity", 0)
.style("position", "absolute")
.attr("class", "tooltip")
.style("width", "200px")
.style("height", "100px")
.style("text-align", "center")
.style("background-color", "white")
.style("border", "solid")
.style("border-width", "2px")
.style("border-radius", "2px")
.style("padding", "2px");
if (showAnnotation1) {
var Annotation1_1 = d3.select("#div_template")
.append("div")
.attr("class", "annotation")
.html("The <em>poorest</em> countries have <em>Lowest</em> life expectancy")
.style("left", "100px")
.style("top", "300px")
var Annotation1_2 = d3.select("#div_template")
.append("div")
.attr("class", "annotation")
.html("The <em>richest</em> countries have <em>Highest</em> life expectancy")
.style("left", "750px")
.style("top", "200px")
}
if (showAnnotation2) {
var Annotation2 = d3.select("#div_template")
.append("div")
.attr("class", "annotation")
.html("<em>European</em> countries have high life expectancy and GDP income")
.style("left", "750px")
.style("top", "250px")
}
if (showAnnotation3) {
var Annotation3_1 = d3.select("#div_template")
.append("div")
.attr("class", "annotation")
.html("The <em>lowest Healthcare Expenditure</em> countries have <em>Lowest</em> life expectancy")
.style("left", "400px")
.style("top", "300px")
var Annotation3_2 = d3.select("#div_template")
.append("div")
.attr("class", "annotation")
.html("The <em>highest Healthcare Expenditure</em> countries have <em>Highest</em> life expectancy")
.style("left", "150px")
.style("top", "50px")
}
if (showAnnotation4) {
var Annotation4 = d3.select("#div_template")
.append("div")
.attr("class", "annotation")
.html("<em>European</em> countries have high life expectancy and Healthcare expenditure per capita")
.style("left", "750px")
.style("top", "250px")
}
if (showGraph1) {
var mouseover = function (d) {
Tooltip
.style("opacity", 1)
d3.select(this)
.style("stroke", "green")
.style("opacity", 1)
};
var mousemove = function (d) {
Tooltip
.html("<em>" + d.COUNTRY + "</em><br/> Population: " + d.POP_TOTAL +"<br>" +
"Life Expectancy: " + d.LIFE_EXP_TOTAL + "<br/>" +
"GDP/capita: " + d.GDP_CAPITA + "<br/>" +
"Health Exp/capita: " + d.HEALTH_CAPITA)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 220) + "px")
};
var mouseleave = function (d) {
Tooltip
.style("opacity", 0)
d3.select(this)
.style("stroke", "black")
.style("opacity", normalOpacity)
};
var highlight = function (d) {
if ("" === regionSelect) {
return normalOpacity;
}
if (d === regionSelect) {
return highlightOpacity;
} else {
return 0.05;
}
}
var regions = ["Africa", "Asia", "Europe", "North America", "Oceania", "South America"];
var ys = d3.scaleLinear().domain([50, 90]).range([height, 0]);
var xs = d3.scaleLog().domain([100, 140000]).range([0, width]);
var rs = d3.scaleLog().domain([10000, 1500000000]).range([0, 15]);
var cs = d3.scaleOrdinal().domain(regions)
.range(["gold", "blue", "green", "red", "brown", "slateblue"])
data = await d3.csv('https://ard8.github.io/data-final-6.csv');
var svg = d3.select("#div_template").append("svg")
.attr("width", (width + margin.left + margin.right))
.attr("height", (height + margin.top + margin.bottom));
var wrapper = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")")
var circles = wrapper.selectAll("circle").data(data).enter().append("circle")
.attr("class", "countries")
.attr("cx", function (d) {
return xs(d.GDP_CAPITA);
})
.attr("cy", function (d) {
return ys(d.LIFE_EXP_TOTAL);
})
.attr("r", function (d) {
return rs(d.POP_TOTAL);
})
.attr("fill", function (d) {
return cs(d.REGION);
})
.style("opacity", function (d) {
return highlight(d.REGION);
})
wrapper.append("g").attr("transform", "translate(0,0)")
.call(d3.axisLeft(ys).ticks(5))
wrapper.append("g").attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(xs).tickValues([100, 1000, 10000, 100000, 1000000]).tickFormat(d3.format("~s")))
//Set up X axis label
wrapper.append("g")
.append("text")
.attr("class", "x title")
.attr("text-anchor", "end")
.style("font-size", "12px")
.attr("transform", "translate(" + width + "," + (height - 10) + ")")
.text("GDP per capita [USD] - Logarithmic scale");
// Set up y axis label
wrapper.append("g")
.append("text")
.attr("class", "y title")
.attr("text-anchor", "end")
.style("font-size", "12px")
.attr("transform", "translate(18, 0) rotate(-90)")
.text("Life Expectancy at Birth");
if (showToolTip) {
circles.on("mouseover", mouseover)
.on("mousemove", mousemove)
.on("mouseleave", mouseleave)
}
//Legend
var legendMargin = {left: 5, top: 10, right: 5, bottom: 10},
legendWidth = 160,
legendHeight = 270;
var svgLegend = d3.select("#legend").append("svg")
.attr("width", (legendWidth + legendMargin.left + legendMargin.right))
.attr("height", (legendHeight + legendMargin.top + legendMargin.bottom));
var legendWrapper = svgLegend.append("g").attr("class", "legendWrapper")
.attr("transform", "translate(" + legendMargin.left + "," + legendMargin.top + ")");
var rectSize = 16, //dimensions of the colored square
rowHeight = 22, //height of a row in the legend
maxWidth = 125; //widht of each row
//Create container per rect/text pair
var legend = legendWrapper.selectAll('.legendSquare')
.data(cs.range())
.enter().append('g')
.attr('class', 'legendSquare')
.attr("transform", function (d, i) {
return "translate(" + 0 + "," + (i * rowHeight) + ")";
})
//Append small squares to Legend
legend.append('rect')
.attr('width', rectSize)
.attr('height', rectSize)
.style('fill', function (d) {
return d;
});
//Append text to Legend
legend.append('text')
.attr('transform', 'translate(' + 25 + ',' + (rectSize / 2) + ')')
.attr("class", "legendText")
.style("font-size", "11px")
.attr("dy", ".35em")
.text(function (d, i) {
return cs.domain()[i];
});
// show legend
if (showLegendSelection) {
legend.style("cursor", "pointer")
.on("mouseover", selectLegend(0.02))
.on("mouseout", selectLegend(normalOpacity))
}
}
if (showGraph2) {
var mouseover = function (d) {
Tooltip
.style("opacity", 1)
d3.select(this)
.style("stroke", "green")
.style("opacity", 1)
};
var mousemove = function (d) {
Tooltip
.html("<em>" + d.COUNTRY + "</em><br/> Population: " + d.POP_TOTAL +"<br>" +
"Life Expectancy: " + d.LIFE_EXP_TOTAL + "<br/>" +
"GDP/capita: " + d.GDP_CAPITA + "<br/>" +
"Health Exp/capita: " + d.HEALTH_CAPITA)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 220) + "px")
};
var mouseleave = function (d) {
Tooltip
.style("opacity", 0)
d3.select(this)
.style("stroke", "black")
.style("opacity", normalOpacity)
};
var highlight = function (d) {
if ("" === regionSelect) {
return normalOpacity;
}
if (d === regionSelect) {
return highlightOpacity;
} else {
return 0.05;
}
}
var regions = ["Africa", "Asia", "Europe", "North America", "Oceania", "South America"];
var ys = d3.scaleLinear().domain([50, 90]).range([height, 0]);
var xs = d3.scaleLog().domain([30, 140000]).range([0, width]);
var rs = d3.scaleLog().domain([10000, 1500000000]).range([0, 15]);
var cs = d3.scaleOrdinal().domain(regions)
.range(["gold", "blue", "green", "red", "brown", "slateblue"])
data = await d3.csv('https://ard8.github.io/data-final-6.csv');
var svg = d3.select("#div_template").append("svg")
.attr("width", (width + margin.left + margin.right))
.attr("height", (height + margin.top + margin.bottom));
var wrapper = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")")
var circles = wrapper.selectAll("circle").data(data).enter().append("circle")
.attr("class", "countries")
.attr("cx", function (d) {
return xs(d.HEALTH_CAPITA);
})
.attr("cy", function (d) {
return ys(d.LIFE_EXP_TOTAL);
})
.attr("r", function (d) {
return rs(d.POP_TOTAL);
})
.attr("fill", function (d) {
return cs(d.REGION);
})
.style("opacity", function (d) {
return highlight(d.REGION);
})
wrapper.append("g").attr("transform", "translate(0,0)")
.call(d3.axisLeft(ys).ticks(5))
wrapper.append("g").attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(xs).tickValues([100, 1000, 10000, 100000]).tickFormat(d3.format("~s")))
//Set up X axis label
wrapper.append("g")
.append("text")
.attr("class", "x title")
.attr("text-anchor", "end")
.style("font-size", "12px")
.attr("transform", "translate(" + width + "," + (height - 10) + ")")
.text("Health Expenditure per capita [USD] - Logarithmic scale");
// Set up y axis label
wrapper.append("g")
.append("text")
.attr("class", "y title")
.attr("text-anchor", "end")
.style("font-size", "12px")
.attr("transform", "translate(18, 0) rotate(-90)")
.text("Life Expectancy at Birth");
if (showToolTip) {
circles.on("mouseover", mouseover)
.on("mousemove", mousemove)
.on("mouseleave", mouseleave)
}
//Legend
var legendMargin = {left: 5, top: 10, right: 5, bottom: 10},
legendWidth = 160,
legendHeight = 270;
var svgLegend = d3.select("#legend").append("svg")
.attr("width", (legendWidth + legendMargin.left + legendMargin.right))
.attr("height", (legendHeight + legendMargin.top + legendMargin.bottom));
var legendWrapper = svgLegend.append("g").attr("class", "legendWrapper")
.attr("transform", "translate(" + legendMargin.left + "," + legendMargin.top + ")");
var rectSize = 16, //dimensions of the colored square
rowHeight = 22, //height of a row in the legend
maxWidth = 125; //widht of each row
//Create container per rect/text pair
var legend = legendWrapper.selectAll('.legendSquare')
.data(cs.range())
.enter().append('g')
.attr('class', 'legendSquare')
.attr("transform", function (d, i) {
return "translate(" + 0 + "," + (i * rowHeight) + ")";
})
//Append small squares to Legend
legend.append('rect')
.attr('width', rectSize)
.attr('height', rectSize)
.style('fill', function (d) {
return d;
});
//Append text to Legend
legend.append('text')
.attr('transform', 'translate(' + 25 + ',' + (rectSize / 2) + ')')
.attr("class", "legendText")
.style("font-size", "11px")
.attr("dy", ".35em")
.text(function (d, i) {
return cs.domain()[i];
});
// show legend
if (showLegendSelection) {
legend.style("cursor", "pointer")
.on("mouseover", selectLegend(0.02))
.on("mouseout", selectLegend(normalOpacity))
}
}
//Decrease opacity of non selected circles when hovering in the legend
function selectLegend(opacity) {
return function (d, i) {
var chosen = cs.domain()[i];
wrapper.selectAll(".countries")
.filter(function (d) {
return d.REGION !== chosen;
})
.transition()
.style("opacity", opacity);
wrapper.selectAll(".countries")
.filter(function (d) {
return d.REGION === chosen;
})
.transition()
.style("opacity", highlightOpacity);
};
}
}
</script>
</body>
</html>