-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
executable file
·633 lines (525 loc) · 17.6 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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v3.min.js"></script>
<script type="text/javascript" src="./d3/d3.v4.js" charset="utf-8"></script>
<script type="text/javascript" src="jquery.js"></script>
<style type="text/css">
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
.Area{
width: 180px;
height: 34px;
overflow: hidden;
background: url(new_arrow.png) no-repeat right #ddd;
border-radius: 14px;
border: 1px solid #ccc;
}
/*dropdown menu for Government wise filtering*/
.Government{
width: 180px;
height: 34px;
overflow: hidden;
border-radius: 14px;
background: url(new_arrow.png) no-repeat right #ddd;
border: 1px solid #ccc;
}
</style>
<Title> Assignment2 </Title>
</head>
<body>
<div id="buttons">
<input name="previousButton" type="button" value="Previous Year" onclick="ChangeYear(-1)" />
<input name="nextButton" type="button" value="Next Year" onclick="ChangeYear(1)" />
<input name="Select Year" type="button" value="Select Year" onclick="Select()" />
<input name="text" type="text" id="text"/>
<input name="AnimationButton" type="button" value="Show Me The Change From 1900 to 2016" onclick="Animation()" />
<input name="Stop" type="button" value="Stop!" onclick="stopRunning()" />
</div>
<div>
<!-- our new feature dropbox -->
<select id="country" class="Area" onchange="dropdownChange(this.value)">
<option value="Empty">--Region--</option>
<option value="Asia">Asia</option>
<option value="Europe">Europe</option>
<option value="Africa">Africa</option>
<option value="North America">North America</option>
<option value="Central America">Central America</option>
<option value="South America">South America</option>
<option value="Oceania">Oceania</option>
<option value="Australia">Australia</option>
</select>
<select id="Govt" class="Government" onchange="dropdownChange1(this.value)">
<option value="Empty">-- Government--</option>
<option value="islamic republic">islamic Republic</option>
<option value="republic">republic</option>
<option value="monarchy">constitutional monarchy</option>
<option value="external territory">external territory</option>
<option value="federal republic">federal republic</option>
<option value="absolute monarchy">absolute monarchy</option>
<option value="people's republic">people's republic</option>
<option value="socialist republic">socialist republic</option>
<option value="parliamentary federal republic">parliamentary federal republic</option>
<input name="AnimationButton" type="button" value="Go with the filtered countries" onclick="Animation()" />
<input name="Stop" type="button" value="Stop!" onclick="stopRunning()" />
</select>
</div>
<script type = "text/javascript" >
var Data = "Gapminder_All_Time.csv";
var regionFilter = "Empty";
var governmentFilter = "Empty";
//margin for main svg
var margin = {top: 50, right: 20, bottom: 50, left: 50},
width = 1000 - margin.left - margin.right,
height = 600 - margin.top - margin.bottom;
//Width and height
var outer_width = 500;
var outer_height = 400;
var svg_width = outer_width - 30;
var svg_height = outer_height - 30;
// the tab for showing the information of the selected circle
var informationTab = d3.select("body").append("div")
.attr("class", "informationTab")
.style("opacity", 1);
// the main svg
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// svg for bar charts
var svg1 = d3.select("body")
.append("svg")
.attr("width", svg_width + margin.left + margin.right)
.attr("height", svg_height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.attr("class", "chart");
//svg for another bar charts
var svg2 = d3.select("body")
.append("svg")
.attr("width", svg_width + margin.left + margin.right)
.attr("height", svg_height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.attr("class", "chart");
var year = 1900;
// to control the animation
var keepRunning = true;
var color = d3.scale.category10();
/********************
Scale and axis for main graph part
*********************/
// scale for gdp and xaxis
var xScale = d3.scale.linear().domain([60000, 0]).range([width, 0]),
xAxis = d3.svg.axis().scale(xScale).orient("bottom").tickFormat(d3.format("d"));
// scale for life and yaxis
var yScale = d3.scale.linear().domain([15, 90]).range([height, 0]), // value -> display
yAxis = d3.svg.axis().scale(yScale).orient("left");
//main x axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text("GDP");
//main y axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Life expectancy");
svg.append("text")
.attr("id", "newyear")
.attr("text-anchor", "end")
.attr("x", 650)
.attr("y", 325)
.style("opacity", 0.4)
.attr("font-size", "250px")
.text(+year);
/********************
Scale and axis for two bar charts part
***************/
//xscale for bar chart
// Create a scale to scale market share values nicely for bar heights
var barxScale = d3.scale.linear()
.domain([60,0])
.range([svg_width-100,0]);
var barxScale1 = d3.scale.linear().domain([125,0]).range([svg_width-100,0]);
// Create a scale object to nicely take care of positioning bars along the horizontal axis
// We don't set the domain yet as data isn't loaded
var baryScale = d3.scale.ordinal()
.range([0, 400], 0.1);
var baryScale1 = d3.scale.ordinal()
.range([0, 400], 0.1);
//Define axis for bar chart1
var barxAxis = d3.svg.axis().scale(baryScale).ticks(5).orient("left");
var baryAxis = d3.svg.axis().scale(barxScale).orient("top").ticks(10);
//Define axis for bar chart2
var baryAxis1 = d3.svg.axis().scale(barxScale1).orient("top").ticks(25);
var barxAxis1 = d3.svg.axis().scale(baryScale1).orient("left");
// Call the axises
svg1.append("g")
.attr("class", "axis")
.attr("id", "y-axis");
svg1.append("g")
.attr("class", "axis")
.attr("id", "x-axis")
.call(barxAxis);
// Call the axises
svg2.append("g")
.attr("class", "axis")
.attr("id", "y-axis")
//.call(baryAxis1);
svg2.append("g")
.attr("class", "axis")
.attr("id", "x-axis")
.call(barxAxis1);
//change the value of the filter variables
function dropdownChange(change){
regionFilter = change;
}
function dropdownChange1(change){
governmentFilter = change;
}
// function for travel from 1900 - 2016
function Animation(){
keepRunning = true;
setInterval(function(){
if(keepRunning == true){
ChangeYear(1);
}
else{
return false;
}
}, 1000);
}
//stop the animation
function stopRunning(){
keepRunning = false;
}
// function for choose a specific year to view the data
function Select(){
var temp = $('#text').val();
if(temp>1950) year = temp-1;
else year = temp - 10;
ChangeYear(1);
}
// funcion to change the main graph
function ChangeYear(x){
//console.log(year);
d3.csv(Data, function(error, data){
svg.select("#circles").remove();
svg.select("#title").remove();
//deal with 1900->1910->1920..
if(year<1950)
year = year + 10 * x;
else
year = year + x;
if ((year<1900) || (year>2016)){
if(x == 1) year = 1900;
if(x == -1) year = 2016;
}
var y = year.toString();
//prepare the data for each country every year.
var yearGDP = [];
var yearHealth = [];
var yearPopulation = [];
var region = [];
var govern = [];
var Countries = new Array();
// 0-7 for Asia Europe Africa North America Central America South America Oceania Australia
var Regions = new Array(0, 0, 0, 0, 0, 0, 0, 0);
var RegionNames = new Array("Asia", "Europe", "Africa", "North America", "Central America", "South America", "Oceania", "Australia");
// 0-8 for islamic republic republic monarchy external territory federal republic
// absolute monarchy people's republic socialist republic parliamentary federal republic
var Goverment = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0);
var GovermentNames = new Array("islamic republic", "republic", "monarchy", "external territory"
, "federal republic", "absolute monarchy", "people's republic", "socialist republic", "parliamentary federal republic");
//filter the data : if the condition is empty --> give all the data
data.forEach(function (d) {
if (d.Year == y) {
if(Countries.indexOf(d.Country) == -1 && regionFilter == "Empty" && governmentFilter=="Empty")
Countries.push(d.Country);
}
else if(Countries.indexOf(d.Country) == -1 && regionFilter == "Empty" && d.Government == governmentFilter){
Countries.push(d.Country);
}
else if (Countries.indexOf(d.Country) == -1 && governmentFilter == "Empty" && d.Region == regionFilter) {
Countries.push(d.Country);
}
else if(Countries.indexOf(d.Country) == -1 && d.Region == regionFilter && d.Government == governmentFilter){
Countries.push(d.Country);
}
});
// prepare GDP
data.forEach(function(d){
if (d.Year == y) {
if (Countries.indexOf(d.Country)!=-1){
if (isNaN(+d.GDP)) yearGDP.push(0);
else yearGDP.push(+d.GDP);
}
}
});
// prepare Life
data.forEach(function(d){
if (d.Year==y){
if (Countries.indexOf(d.Country)!=-1){
//console.log(d.LifeExp)
if (d.LifeExp==".."){
yearHealth.push(0);
}
else yearHealth.push(+d.LifeExp);
}
}
});
// prepare population
data.forEach(function(d){
if (d.Year == y) {
if (Countries.indexOf(d.Country)!=-1){
if (isNaN(+d.Population)) yearPopulation.push(0);
else yearPopulation.push(+d.Population);
}
}
});
// prepare Regions
for (var c in Countries) {
//console.log(Countries[c]);
var notFound = true;
data.forEach(function (d) {
if (notFound) {
if (d.Country == Countries[c]) {
region.push(d.Region);
notFound = false;
}
}
});
if (notFound) {
region.push("Unknown");
}
};
// prepare Governments
for (var c in Countries) {
//console.log(Countries[c]);
var notFound = true;
data.forEach(function (d) {
if (notFound) {
if (d.Country == Countries[c]) {
govern.push(d.Government);
notFound = false;
}
}
});
if (notFound) {
govern.push("Unknown");
}
};
// prepare the array for region and goverment for part two
for (var c in Countries) {
//console.log(Countries[c]);
data.forEach(function (d) {
if (d.Country == Countries[c] && d.Year == y) {
switch (d.Region) {
case "Asia":
Regions[0]++;
break;
case "Europe":
Regions[1]++;
break;
case "Africa":
Regions[2]++;
break;
case "North America":
Regions[3]++;
break;
case "Central America":
Regions[4]++;
break;
case "South America":
Regions[5]++;
break;
case "Oceania":
Regions[6]++;
break;
case "Australia":
Regions[7]++;
break;
}
switch (d.Government) {
case "islamic republic":
Goverment[0]++;
break;
case "republic":
Goverment[1]++;
break;
case "monarchy":
Goverment[2]++;
break;
case "external territory":
Goverment[3]++;
break;
case "federal republic":
Goverment[4]++;
break;
case "absolute monarchy":
Goverment[5]++;
break;
case "people's republic":
Goverment[6]++;
break;
case "socialist republic":
Goverment[7]++;
break;
case "parliamentary federal republic":
Goverment[8]++;
break;
}
}
});
};
//console.log(Regions[1]);
// It's like when year is 2016 allData for China is [China, China's GDP, Life, population, asia]
var allData = d3.zip(Countries, yearGDP, yearHealth, yearPopulation, region,govern);
var dataForBar1 = d3.zip(RegionNames,Regions);
var dataForBar2 = d3.zip(GovermentNames,Goverment);
//console.log(allData);
//Draw two bar charts
generateVis(dataForBar1);
generateVis1(dataForBar2);
//BarChart(dataForBar1,dataForBar2);
var circle = svg.append("g").attr("id", "circles")
.selectAll("circle")
.data(allData)
.enter().append("circle")
.style("fill", function (d) { return color(d[4]); })
.on("mouseover", function (d) {
informationTab.transition()
.duration(200)
.style("opacity", .9);
informationTab.html(d[0] + "<br/> " + "GDP: " + d[1] + "<br/>" + " Life Expectancy: " + d[2] + "<br/>" + " Population: " + d[3] + "<br/>" + "Continent: " + d[4])
})
.on("mouseout", function (d) {
informationTab.transition()
.duration(500)
.style("opacity", 0)
})
.transition()
.duration(500)
.attr("cx", function(d) {return xScale(d[1]);})
.attr("cy", function(d) {return yScale(d[2]);})
.attr("class", "y1")
.attr("r", function(d){return Math.pow(d[3], 1/2)/600;})
.style("stroke", "white")
.style("stroke-size","3px")
.style("opacity", 0.8);
//set the year
d3.select("#newyear").text(y)
});
};
/*
function Region_filter(value) {
console.log(regionFilter + " " + governmentFilter)
if (regionFilter == "Empty")
return value
else
s = value.Region
return (s == regionFilter)
}
function Government_filter(value) {
if (governmentFilter == "Empty")
return value
else
s = value.Government
return (s == government_select)
}
*/
//Bar chart No.1
function generateVis(data) {
//console.log(data);
svg1.selectAll("rect").remove();
svg1.selectAll("text").remove();
// Filter the data to only include the current year
/******** PERFORM DATA JOIN ************/
// Join new data with old elements, if any.
var bars = svg1.selectAll("rect")
.data(data);
var texts = svg1.selectAll("text").data(data);
/******** HANDLE ENTER SELECTION ************/
// Create new elements in the dataset
bars.enter()
.append("rect")
.attr("y", function (d,i) {
return 50*i;
})
.attr("width", function (d) {
return barxScale(d[1]);
})
.attr("height", 37)
.style("fill", "Blue");
texts.enter()
.append("text")
.attr("y", function (d, i) {
return 50 * i + 25;
})
.attr("fill", "black")
.html(function (d) {
return "Regions: " + d[0] + " Number: " + d[1];
});
}
//bar chart No.2
function generateVis1(data) {
//console.log(data);
svg2.selectAll("rect").remove();
svg2.selectAll("text").remove();
// Filter the data to only include the current year
/******** PERFORM DATA JOIN ************/
// Join new data with old elements, if any.
var bars = svg2.selectAll("rect")
.data(data);
/******** HANDLE ENTER SELECTION ************/
// Create new elements in the dataset
bars.enter()
.append("rect")
.attr("y", function (d, i) {
return 45 * i;
})
.attr("width", function (d) {
return barxScale1(d[1]);
})
.attr("height", 39)
.style("fill", "Blue");
svg2.selectAll("text")
.data(data)
.enter()
.append("text")
.attr("fill", "black")
.attr("y", function (d, i) {
return 45 * i+30;
})
.html(function (d) {
return "Government: " + d[0]+" Number: "+ d[1];
});
}
// give the default graph
ChangeYear(0);
</script>
</body>
</html>