-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHans_vis.html
More file actions
549 lines (468 loc) · 16.9 KB
/
Hans_vis.html
File metadata and controls
549 lines (468 loc) · 16.9 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>D3 Example</title>
<script src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<link href="./themes/prism.css" rel="stylesheet" />
<link href="./themes/tas_style.css" rel="stylesheet" />
<script type="text/javascript" src="./d3/d3.v3.js" charset="utf-8"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.js"></script>
<script src="./lib/prism.js" charset="utf-8"></script>
<style type="text/css">
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis {
opacity: 0.5;
}
.button-bar {
margin-left: 15px;
cursor: pointer;
}
.selected {
font-weight: bold;
}
.reg{
border: 0 !important; /*Removes border*/
margin-left: 350px;
margin-bottom: :50px;
margin-top: 50px;
-webkit-appearance: none; /*Removes default chrome and safari style*/
-moz-appearance: none; /* Removes Default Firefox style*/
background: #0088cc;
width: 150px; /*Width of select dropdown to give space for arrow image*/
text-indent: 0.01px; /* Removes default arrow from firefox*/
text-overflow: ""; /*Removes default arrow from firefox*/ /*My custom style for fonts*/
color: #FFF;
border-radius: 15px;
padding: 5px;
box-shadow: inset 0 0 5px rgba(000,000,000, 0.5);
}
.gov{
border: 0 !important; /*Removes border*/
-webkit-appearance: none; /*Removes default chrome and safari style*/
-moz-appearance: none; /* Removes Default Firefox style*/
background: #0088cc;
width: 150px; /*Width of select dropdown to give space for arrow image*/
text-indent: 0.01px; /* Removes default arrow from firefox*/
text-overflow: ""; /*Removes default arrow from firefox*/ /*My custom style for fonts*/
color: #FFF;
border-radius: 15px;
padding: 5px;
box-shadow: inset 0 0 5px rgba(000,000,000, 0.5);
}
.slider{
width: 800px;
height: 25px;
cursor: pointer;
animate: 0.2s;
box-shadow: 1px 1px 1px #50555C;
background: grey;
border-radius: 14px;
border: 0px solid #000000;
}
</style>
</head>
<body>
<h3 id = "year_header" style = "font-family:verdana; color:blue; font-size:14px;">Year: 1900</h3>
<!-- Drop down menu for Regions -->
<select id="country" class="reg" onchange="dropdownChange(this.value)">
<option value="None">--Select Region--</option>
<option value="">All</option>
<option value="Africa">Africa</option>
<option value="America">Americas</option>
<option value="Asia">Asia</option>
<option value="Australia">Australia</option>
<option value="Europe">Europe</option>
<option value="Oceania">Oceania</option>
</select>
<!-- Drop down menu for Govt -->
<select id="Govt" class="gov" onchange="dropdownChange1(this.value)">
<option value="None">-- Select Govt--</option>
<option value="">All</option>
<option value="republic">republic</option>
<option value="islamic republic">islamic republic</option>
<option value="constitutional monarchy">constitutional monarchy</option>
<option value="presidential federal republic">presidential federal republic</option>
<option value="parliamentary monarchy">parliamentary monarchy</option>
<option value="federal republic">federal republic</option>
<option value="people's republic">people's republic</option>
<option value="socialist republic">socialist republic</option>
</select>
<script type="text/javascript" id = "demo_code">
var outerWidth = 1000;
var outerHeight = 450;
var marginLeft = 70;
var marginTop = 30;
var marginRight = 30;
var marginBottom = 70;
var innerWidth = 900 - marginLeft - marginRight;
var innerHeight = 450 - marginTop - marginBottom
var rMin = 2;
var rMax = 50;
var colorColumn = "area";
display_year = 1900
// define a function that filters data by year
function yearFilter(value){
return (value.Year == display_year)
}
regionSelect = '';
govtselect= "";
// define a function that filters data by Region
function regfilter(value){
if (regionSelect == '')
return value
else
s = value.Region
return (s.includes(regionSelect))
}
// define a function that filters data by Govt
function govtfilter(value){
if (govtselect == '')
return value
else
s = value.Government
return (s == govtselect)
}
// set the value to the user clicked value for region
function dropdownChange(value) {
regionSelect = value
}
// set the value to the user clicked value for Govt
function dropdownChange1(value)
{
govtselect = value
}
// create a var svg and append it to body
var svg = d3.select("body").append("svg")
.attr("width", outerWidth)
.attr("height", outerHeight)
.append("g")
.attr("transform", "translate(" + marginLeft + "," + marginTop + ")");
// set the scale and range
var xScale = d3.scale.log().range([0, innerWidth]);
var yScale = d3.scale.linear().range([innerHeight, 0]);
var rScale = d3.scale.sqrt().range([2, 50]); // sqrt scale for bubbles
var clrscle = d3.scale.ordinal().range(["red","yellow","blue","green","green","teal","green","pink"]); // setting the range of colors for regions
var yAxis = d3.svg.axis()
.scale(yScale)
.ticks(7)
.orient("left");
var xAxis = d3.svg.axis()
.scale(xScale)
.ticks(10,d3.format(",d")) // formatting to show logarithmic scale
.orient("bottom");
// set the Y domain to the min and max values of Life Expectancy from the dataset
yScale.domain([15.226,84.1])
// set the X domain to the min and max values of GDP from the dataset
xScale.domain([142,182668])
svg.append("g")
.attr("class", "axis")
.call(yAxis);
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," +innerHeight+ ")")
.call(xAxis);
// appending the text for displaying the Year
svg.append("text")
.attr("id", "newyear")
.attr("text-anchor","end")
.attr("x",650)
.attr("y",225)
.style("opacity",0.4)
.attr("font-size","250px")
.text(+display_year);
// text label for the y axis
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y",-50)
.attr("x",-200)
.style("text-anchor", "middle")
.attr("dy", "1em")
.attr("fill","grey")
.text("[Average Life Expectancy in Years]");
// text label for the x axis
svg.append("text")
.attr("x", outerWidth/2 -80)
.attr("y", innerHeight + 40 )
.style("text-anchor", "middle")
.attr("fill","grey")
.text("[GDP Per Capita in US $]");
// appeding rectangles to build the legend
svg.append("rect")
.attr("x", 800)
.attr("y", 200)
.attr("width", 20)
.attr("height", 20)
.attr("fill","pink")
.on("mouseover", function(d){ // using the mouseover functionality to add a filter selection feature
d3.select(this)
.transition()
.duration(1000)
regionSelect = "Oceania"
genviz()
})
.on("mouseout", function(d){
d3.select(this)
.transition()
.duration(1000)
regionSelect="" // using the mouseout feature to reset the regionSelect to display all regions
genviz()
});
svg.append("rect")
.attr("x", 800)
.attr("y", 170)
.attr("width", 20)
.attr("height", 20)
.attr("fill","blue")
.on("mouseover", function(d){
d3.select(this)
.transition()
.duration(1000)
regionSelect = "Europe"
genviz()
})
.on("mouseout", function(d){
d3.select(this)
.transition()
.duration(1000)
regionSelect=""
genviz()
});
svg.append("rect")
.attr("x", 800)
.attr("y", 140)
.attr("width", 20)
.attr("height", 20)
.attr("fill","teal")
.on("mouseover", function(d){
d3.select(this)
.transition()
.duration(1000)
regionSelect = "Australia"
genviz()
})
.on("mouseout", function(d){
d3.select(this)
.transition()
.duration(1000)
regionSelect=""
genviz()
});
svg.append("rect")
.attr("x", 800)
.attr("y", 110)
.attr("width", 20)
.attr("height", 20)
.attr("fill","red")
.on("mouseover", function(d){
d3.select(this)
.transition()
.duration(1000)
regionSelect = "Asia"
genviz()
})
.on("mouseout", function(d){
d3.select(this)
.transition()
.duration(1000)
regionSelect=""
genviz()
});
svg.append("rect")
.attr("x", 800)
.attr("y", 80)
.attr("width", 20)
.attr("height", 20)
.attr("fill","green")
.on("mouseover", function(d){
d3.select(this)
.transition()
.duration(1000)
regionSelect = "America"
genviz()
})
.on("mouseout", function(d){
d3.select(this)
.transition()
.duration(1000)
regionSelect=""
genviz()
});
svg.append("rect")
.attr("x", 800)
.attr("y", 50)
.attr("width", 20)
.attr("height", 20)
.attr("fill","Yellow")
.on("mouseover", function(d){
d3.select(this)
.transition()
.duration(1000)
regionSelect = "Africa"
genviz()
})
.on("mouseout", function(d){
d3.select(this)
.transition()
.duration(1000)
regionSelect=""
genviz()
});
// appending text to legend
svg.append("text")
.attr("x", 842)
.attr("y",63 )
.style("text-anchor", "middle")
.attr("fill","grey")
.text("Africa");
svg.append("text")
.attr("x", 852)
.attr("y",94 )
.style("text-anchor", "middle")
.attr("fill","grey")
.text("Americas");
svg.append("text")
.attr("x", 837)
.attr("y",125 )
.style("text-anchor", "middle")
.attr("fill","grey")
.text("Asia");
svg.append("text")
.attr("x", 850)
.attr("y",156 )
.style("text-anchor", "middle")
.attr("fill","grey")
.text("Australia");
svg.append("text")
.attr("x", 845)
.attr("y",187 )
.style("text-anchor", "middle")
.attr("fill","grey")
.text("Europe");
svg.append("text")
.attr("x", 848)
.attr("y",218 )
.style("text-anchor", "middle")
.attr("fill","grey")
.text("Oceania");
function genviz(){
// filter according to year
filtered_datset = dataset.filter(yearFilter);
// filter according to region
filtered_datset = filtered_datset.filter(regfilter);
//filter according to Govt
filtered_datset = filtered_datset.filter(govtfilter);
// setting the domain for coloring the bubbles
clrscle.domain(["Asia","Africa","Europe","North America","South America","Australia","Central America","Oceania"]);
rScale.domain(d3.extent(dataset, function (d){ return +d["Population"]; }));
var circles = svg.selectAll("circle").data(filtered_datset,function key(d){
return d.Country
});
// update functionality
circles
.transition()
.duration(1000)
.attr("cx", function (d){ return xScale(+d["GDP"]); })
.attr("cy", function (d){ return yScale(+d["LifeExp"]); })
.attr("r", function (d){ return rScale(+d["Population"]); }) // scaling the radius according to population
.attr("fill", function (d){ return clrscle(d["Region"]); })
.attr("stroke",'black')
.style("opacity", 0.9);
// enter functionality
circles.enter().append("circle")
.attr("cx", function (d){ return xScale(+d["GDP"]); })
.attr("cy", function (d){ return yScale(+d["LifeExp"]); })
.attr("r", function (d){ return rScale(+d["Population"]); })
.attr("fill", function (d){ return clrscle(d["Region"]); })
.attr("stroke",'black')
// To see label for countries when hovering mouse
.on("mouseover", function(d){
d3.select(this)
.transition()
.duration(1000)
.style("stroke-width", 2);
})
.on("mouseout", function(d){
d3.select(this)
.transition()
.duration(1000)
.style("stroke-width", 1);
})
// To Label the Countries(bubbles)
.append("title")
.text(function(d){
return "Country: " + d.Country;
});
// exit functionality when data is not present
circles.exit()
.transition()
.duration(1000)
.attr("r",0)
.remove();
d3.select("#year_header").text("Year: " + display_year)
d3.select("#newyear").text(display_year)
}
// Reading data from csv dataset
d3.csv("./data/Gapminder_All_Time.csv", function(data){
dataset = data;
genviz(); // calling the genviz function to generate the visualisation
});
</script>
<!-- creating the play and pause buttons -->
<br>
<div class = "button-bar" style="display: inline; padding-left: 2%;">
<a id = "play_button"><img src="./images/playbut.png" style="height: 32px; width: 32px"></a>
<a id = "pause_button"><img src="./images/pausebut.png" style="height: 32px; width: 32px"></a>
</div>
<!-- creating the slider -->
<div style="display: inline;">
<input class = "slider" type="range" Id="Year" value = 1900 min="1900" max="2015" oninput = "sliderchange(Year.value)" style="width: 800px">
</div>
<script type="text/javascript">
document.getElementById("pause_button").style.display = "none"; // initially hiding the pause button
d3.select("#play_button")
.on("click", function() {
// Set up the interval callback
playInterval = setInterval(function() {
// Increment the display year by 10 till 1950 and then by 1
if (display_year < 1950){
for(var i=display_year%10;i<10; i++)
{
display_year++;
}
}else{
display_year++;
}
$("#Year").val(display_year);
// Make the display_year loop around from max to min
if(display_year > 2015){
display_year = 1900;
}
genviz();
document.getElementById("play_button").style.display = "none"; // hiding the play button
document.getElementById("pause_button").style.display = "inline"; // displaying the pause button
}, 1000);
});
// A global variable for the interval listeneer so that it can be stopped elsewhere
var playInterval;
// Add an event listener to the play all button do that when it is clicked the visualisaiton iterates through the different years
d3.select("#pause_button")
.on("click", function() {
clearInterval(playInterval);
document.getElementById("play_button").style.display = "inline"; // displaying the play button
document.getElementById("pause_button").style.display = "none"; // hiding the pause button
});
function sliderchange(year)
{
display_year = year // set the display_year to the value from the slider
genviz();
d3.select("#year_header").text("Year: " + year)
}
</script>
</body>
</html>