-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
359 lines (325 loc) · 13.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
<!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>
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
<script src="https://d3js.org/d3-geo-projection.v2.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
<style> circle {
stroke: black;
opacity: 0.2
}
.annotation {
opacity: 1;
position: absolute;
width: 120px;
height: 100px;
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 through here:</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>
</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</h3>
<div class="row align-items-center">
<div class="col-sm-2"><p id="value-time"></p></div>
<div class="col-sm">
<div id="slider-time"></div>
</div>
</div>
<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/jquery@1.12.4/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/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 golbalSceneNumber = 1;
var showToolTip = false;
var showAnnotation1 = true;
var showAnnotation2 = false;
var showYearSelection = false;
var regionSelect = "";
var showLegendSelection = false;
var highlightOpacity = 0.7;
var normalOpacity = 0.2;
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) {
golbalSceneNumber = sceneNumber;
if (golbalSceneNumber === 1) {
showToolTip = false;
showAnnotation1 = true
showAnnotation2 = 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 (golbalSceneNumber === 2) {
showToolTip = false;
showAnnotation1 = false;
showAnnotation2 = 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 GDP per Capita vary much.</p>")
d3.select("#div_template").html("")
resetScene(2)
}
if (golbalSceneNumber === 3) {
showToolTip = true;
showAnnotation1 = false;
showAnnotation2 = false;
showYearSelection = true;
regionSelect = ""
showLegendSelection = true;
d3.select("#scene-desc")
.html("<h2>Now use your mouse to explore!</h2><br><p>" +
"Try move your mouse over the legend and the circles.</p>")
resetScene(3)
}
if (golbalSceneNumber === 4) {
showToolTip = false;
showAnnotation1 = true
showAnnotation2 = 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(4);
}
}
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", "10px")
}
if (showAnnotation2) {
var Annotation2 = d3.select("#div_template")
.append("div")
.attr("class", "annotation")
.html("<em>African</em> countries all very low life expectancy and GDP income")
.style("left", "550px")
.style("top", "250px")
}
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.Population +"<br>" +
"Life Expectancy: " + d.LifeExp + "<br/>" +
"GDP/capita: " + d.GDP)
.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([0, 100]).range([height, 0]);
var xs = d3.scaleLog().domain([100, 100000]).range([0, width]);
var rs = d3.scaleLog().domain([30000, 1600000000]).range([0, 15]);
var cs = d3.scaleOrdinal().domain(regions)
.range(["gold", "blue", "green", "red", "brown", "slateblue"])
data = await d3.csv('./data3.csv');
console.log(data);
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);
})
.attr("cy", function (d) {
return ys(d.LifeExp);
})
.attr("r", function (d) {
return rs(Number(d.Population));
})
.attr("fill", function (d) {
return cs(d.Region);
})
.style("opacity", function (d) {
return highlight(d.Region);
})
if (showToolTip) {
circles.on("mouseover", mouseover)
.on("mousemove", mousemove)
.on("mouseleave", mouseleave)
}
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([10, 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("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");
//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))
// .on("click", selectLegend(0.02));
}
//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>