-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
685 lines (573 loc) · 22.7 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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=Varela' rel='stylesheet' type='text/css'>
<!-- Load the d3 library. -->
<script src="d3.min.js" charset="utf-8"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<style>
.country:hover{
stroke-width: 2px;
}
.text{
font-size:10px;
text-transform:capitalize;
}
#explanation {
font-family: Helvetica;
margin: 10px 10%;
}
#worldLegend{
margin: auto;
width: 65%;
height: 30%;
font-size: 12px;
}
#explanation2 {
font-family: Helvetica;
margin: 10px 10%;
}
#container {
margin:10px 10%;
border:2px solid #000;
border-radius: 5px;
height:100%;
overflow:hidden;
background: #F0F8FF;
}
.hidden {
display: none;
}
div.tooltip {
color: #222;
background: #fff;
padding: .5em;
text-shadow: #f5f5f5 0 1px 0;
border-radius: 2px;
box-shadow: 0px 0px 2px 0px #a6a6a6;
opacity: 0.9;
position: absolute;
}
.graticule {
stroke: #bbb;
stroke-width: .5px;
stroke-opacity: .5;
}
.equator {
stroke: #ccc;
stroke-width: 1px;
}
</style>
</head>
<body>
<div id = "explanation">
<h3 style=" font-size: 36px; text-align: center;">Passport Power</h3>
<p>How many countries can you travel to with your passport? <br>
Our world map visualization explores the power of passports. Upon clicking, you can see the country being selecte highlight in neon yellow
and the lines that shoot out from a country lead to the countries one from the selected country can visit <b>visa-free</b>! Those countries
are colored according to their income groups calculated by GDP Per Capita.
Upon hovering, you can also explore the names, Incoming Yearly Tourists, Outgoing yearly tourists and GDP per Capita of every country in the world.
The lines connecting connecting countries say that if you are a citizen of the source (Selection)
you can travel to the destination country without a visa. They are also helpfully colored by the range
of incoming tourists that the destination country gets, so you can see which places are hot and touristy
and which are quiet and secluded, when planning your travel!</p>
</div>
<div id="container"></div>
<div id="worldLegend"></div>
<div id="canvas"></div>
<div id ="explanation2">
<p>
The following visualization illustrates each country's GDP per Capita and the number of incoming tourists
in the most recent year upon interaction. <br>
The color of each bubble corresponds to the color of the country on the world map and
it represents the income group that country belongs to. The sizes
of circles upon hovering, represent the number of incoming tourists in the most recent year.
<br>
In order to help you observe the general trend, the circle sizes stay the same after being
hovered over. This design choice makes sure that you can see the full picture by the end. It
also engages with you. It hopefully simulates a gaming experience and aims to
capture the your attention fully to each and every country. </p>
</div>
<div id="canvas2"></div>
<script>
var lower_middle_income;
var upper_middle_income;
var high_income;
var avg;
d3.json("cleandata.json", function (error, data){
//Part of the code taken from: http://techslides.com/responsive-d3-map-with-zoom-and-pan-limits
var width = document.getElementById('container').offsetWidth;
var height = width / 2 + 60;
var topo,projection,path,svg,g;
var mapfillcolor = "#CCC";
var mapstrokecolor = "#FFF";
var graticule = d3.geo.graticule();
var tooltip = d3.select("#container").append("div").attr("class", "tooltip hidden");
var svg2, g2;
var width2 = document.getElementById('canvas').offsetWidth;
var height2 = width2;
var force = d3.layout.force()
.charge(-170)
.linkDistance(50)
.size([width2, height2]);
var margin = {top: 10, right: 10, bottom: 10, left: 10},
width2 = width2 - margin.left - margin.right,
height2 = height2 - margin.top - margin.bottom;
svg2 = d3.select("#canvas2").append("svg")
.attr("width", width2)
.attr("height", height2)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
g2 = svg2.append("g");
numbers = data;
maxGDP = d3.max(numbers, function(d) {return d.gdp_percapita});
minGDP = d3.min(numbers, function(d) {return d.gdp_percapita});
sumGDP = d3.sum(numbers, function(d) {return d.gdp_percapita} ) - maxGDP - minGDP;
avg = sumGDP / (numbers.length - 2);
lower_middle_income = avg * 3/4;
upper_middle_income = avg;
high_income = avg * 1.25;
var countryColor = function(income){
if (income == "high"){
return "#1F77B4"; //high income
}
else if (income == "upper") {
return "#2CA02C"; //upper middle income
}
else if (income == "lower") {
return "#FFBB78"; //lower middle income
}
else if (income == "low") {
return "#FF7F0E"; //low income
}
else{
return "brown"; //unknown GDP
}
}
var lineColor = function(oTour){
if (oTour == 1){
return "#00DEB1"; //0-500
}
else if (oTour == 2) {
return "#6600FF"; //500 - 1 Mil
}
else if (oTour == 3) {
return "#009933"; //1 - 10 Mil
}
else if (oTour == 4) {
return "#FF0000"; //10 - 50 Mil
}
else if (oTour == 5) {
return "#996633"; // > 50 Mil
}
else {
return "#FF66CC"; //unknown
}
}
var points = numbers.map(function (country) {
return {
x: Math.random(),
y: Math.random(),
name: country["country_name"],
incoming: country["incoming"],
id: country["iso3_digit_code"]
};
})
.filter(function (point) {
return ! isNaN(point.x) && ! isNaN(point.y);
});
var xScale = d3.scale.linear().domain(d3.extent(points, function (point) {return point.x;})).range([50, width - 50]);
var yScale = d3.scale.linear().domain(d3.extent(points, function (point) {return point.y;})).range([height - 50, 50]);
force
.nodes(points)
.start();
var node = svg2.selectAll("circle.node")
.data(points)
.enter()
.append("circle")
.attr("class", "node")
.attr("cx", function(d) { return xScale(d.x); })
.attr("cy", function(d) { return yScale(d.y); })
.attr("r", "20")
.style("fill", function (d) {return countryColor(getIncomeGroup(d.id)); })
.style("opacity", 0.5)
.call(force.drag);
var text = svg2.selectAll('text')
.data(points)
.enter()
.append("svg:text")
.attr("x", function(d) { return xScale(d.x); })
.attr("y", function(d) { return yScale(d.y); })
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(function(d) {
return d.name;
})
.attr("font-size", "12px");
node
.on("mouseover", function(d) {
d3.select(this)
.attr("r", function (d) {
if (isNaN(Number(d.incoming))) {
return d3.min(d.incoming);
}else{
return (d.incoming / 500000);
}
})
})
force.on("tick", function() {
text.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return d.y; });
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
setup(width,height);
function setup(width,height){
projection = d3.geo.mercator()
.translate([0, 0])
.scale(width / 2 / Math.PI);
path = d3.geo.path()
.projection(projection);
svg = d3.select("#container").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
g = svg.append("g");
}
function getIncomeGroup(code) {
for (var i = 0; i < numbers.length; i++) {
if (numbers[i].iso3_digit_code == code){
if (numbers[i].gdp_percapita >= high_income) {
return "high";
}
else if (numbers[i].gdp_percapita >= upper_middle_income && numbers[i].gdp_percapita < high_income) {
return "upper";
}
else if (numbers[i].gdp_percapita >= lower_middle_income && numbers[i].gdp_percapita < upper_middle_income) {
return "lower";
}
else if (numbers[i].gdp_percapita < lower_middle_income) {
return "low";
}
else{
return "No Available Data";
}
}
}
return "No Available Data";
}
function getoT(code) {
var nOut = code.incoming;
if (nOut/1000 >= 50000) {
return 5;
}
else if (nOut/1000 >= 10000 && nOut/1000 < 50000) {
return 4;
}
else if (nOut/1000 >= 1000 && nOut/1000 < 10000) {
return 3;
}
else if (nOut/1000 >= 500 && nOut/1000 < 1000) {
return 2;
}
else if (nOut/1000 < 500) {
return 1
}
else{
return 0;
}
return 0;
}
function getIncome(code) {
for (var i = 0; i < numbers.length; i++){
if (numbers[i].iso3_digit_code == code){
return numbers[i].gdp_percapita;
}
}
return "No Available Data";
}
function getCoordinates(code) {
for (var i = 0; i < numbers.length; i++){
if (numbers[i].iso3_digit_code == code){
return projection([numbers[i].long, numbers[i].lat]);
}
}
return projection([0, 0]);
}
function getCountryname(code) {
for (var i = 0; i < numbers.length; i++){
if (numbers[i].iso3_digit_code == code){
return numbers[i].country_name;
}
}
return "Unknown";
}
function getIncomingTourists(code) {
for (var i = 0; i < numbers.length; i++){
if (numbers[i].iso3_digit_code == code){
return numbers[i].incoming;
}
}
return "No Available Data";
}
function getOutgoingTourists(code) {
for (var i = 0; i < numbers.length; i++){
if (numbers[i].iso3_digit_code == code){
return numbers[i].outgoing;
}
}
return "No Available Data";
}
var worldData;
d3.json("world-topo.json", function(error, world) {
worldData = world;
// Use topojson to create an array containing one object per country.
var countries = topojson.feature(world, world.objects.countries).features;
topo = countries;
draw(topo);
});
function isInConnections(id, connectionCoords){
for (var i = 0; i < connectionCoords.length; i++){
if (connectionCoords[i].iso3_digit_code == id){
return true;//connectionCoords[i].country_name;
}
}
return false;
}
function calculateMinDistance(coords, countryCoords){
return (Math.sqrt(Math.pow((coords[0] - countryCoords[0]), 2)
+ Math.pow((coords[1] - countryCoords[1]), 2)))
}
var drawConnections = function (de, topo){
d3.json("cleandata.json", function (error1, data1){
svgScale.classed("hidden",false);
svg.selectAll("circle").remove();
svg.selectAll("text").remove();
svg.selectAll("line").remove();
var countryN = getCountryname(de.id);
var country;
data1.forEach(function(d){
if (d.country_name == countryN) {
country = d;
}
});
if (country == undefined ){
console.log("No data for this country unfortunately, please choose another");
} else{
var countryCoords = projection([country.long, country.lat]);
var initialCircle = svg.append("circle").attr("r",1).attr("cx",countryCoords[0])
.attr("cy",countryCoords[1]).attr("fill","red").attr("id","removable");
svg.append("text").attr("x",countryCoords[0])
.attr("y",countryCoords[1] + 20).attr("fill","black").text(country.country_name )
.attr("text-anchor","middle").attr("id","removable").attr("font-weight","bold");
var selectedCountry = g.selectAll(".country").data(topo);
var connectionCoords = (country.connections).map(function(d){
var targetCountry;
data1.forEach(function(d2){
if (d2.country_name == d){
targetCountry = d2;
}
});
return targetCountry;
});
var lines = svg.selectAll("line").data(connectionCoords);
lines.enter().append("line")
.attr("x1", projection([country.long, country.lat])[0])
.attr("y1", projection([country.long, country.lat])[1])
.attr("x2", projection([country.long, country.lat])[0])
.attr("y2", projection([country.long, country.lat])[1])
.style("stroke", function (d) {return lineColor(getoT(d)); })
.attr("stroke-width", 1);
lines.transition().duration(5000)
.attr("x2", function(d) { return (projection([d.long, d.lat]))[0]; })
.attr("y2", function(d) { return (projection([d.long, d.lat]))[1]; });
connectionCoords.forEach(function(d){
svg.append("circle").attr("r",5).attr("cx",projection([d.long, d.lat])[0])
.attr("cy",projection([d.long, d.lat])[1]).attr("fill","#BCED91").attr("id","removable");
selectedCountry.style("fill", function (d) {
if (isInConnections(d.id, connectionCoords)){
return countryColor(getIncomeGroup(d.id))
}
if (d.id == de.id){
return "#F3F315";
}
return mapfillcolor;
});
});
}
});
}
function draw(topo) {
svg.append("path")
.datum(graticule)
.attr("class", "graticule")
.attr("d", path);
g.append("path")
.datum({type: "LineString", coordinates: [[-180, 0], [-90, 0], [0, 0], [90, 0], [180, 0]]})
.attr("class", "equator")
.attr("d", path);
var country = g.selectAll(".country").data(topo);
country.enter().insert("path")
.attr("class", "country")
.attr("d", path)
.style("fill", function (d) {return mapfillcolor})
.style("stroke", mapstrokecolor);
//ofsets plus width/height of transform, plsu 20 px of padding, plus 20 extra for tooltip offset off mouse
var offsetL = document.getElementById('container').offsetLeft+(width/2)+40;
var offsetT =document.getElementById('container').offsetTop+(height/2)+20;
//tooltips
country
.on("mouseover", function(d,i) {
var mouse = d3.mouse(svg.node()).map( function(d) { return parseInt(d); } );
tooltip
.classed("hidden", false)
.attr("style", "left:"+(mouse[0]+offsetL)+"px;top:"+(mouse[1]+offsetT)+"px")
.html(getCountryname(d.id) +
" <br> GDP per Capita: " + getIncome(d.id)
+ "<br> Incoming Tourists/ Year: " + getIncomingTourists(d.id)
+ "<br> Outgoing Tourists/ Year: " + getOutgoingTourists(d.id)
+ "<br> If there is no Data, clicking on this country will be futile!")
})
.on("mouseout", function(d,i) {
tooltip.classed("hidden", true)
});
country
.on("click", function(d,i) {
var mouse = d3.mouse(svg.node()).map( function(d) { return parseInt(d); } );
drawConnections(d, topo);
});
}
function redraw() {
width = document.getElementById('container').offsetWidth-60;
height = width / 2;
d3.select('svg').remove();
setup(width,height);
draw(topo);
}
});
//LEGEND FOR WORLD MAP
var svgScale = d3.select("#worldLegend").append("svg")
.classed("hidden",true)
.attr("width", 800)
.attr("height", 160)
svgScale.append("line")
.attr("y1",15)
.attr("y2",15)
.attr("x1",100)
.attr("x2",115)
.style("stroke", "#00DEB1")
.attr("stroke-width", 4);
svgScale.append("text")
.text("Destination Country has 0 - 500,000 Incoming Tourists/Year")
.attr("x",120)
.attr("y",19)
svgScale.append("line")
.attr("y1",31)
.attr("y2",31)
.attr("x1",100)
.attr("x2",115)
.style("stroke", "#6600FF")
.attr("stroke-width", 4);
svgScale.append("text")
.text("Destination Country has 500,000 - 1 Million Incoming Tourists/Year")
.attr("x",120)
.attr("y",35)
svgScale.append("line")
.attr("y1",51)
.attr("y2",51)
.attr("x1",100)
.attr("x2",115)
.style("stroke", "#009933")
.attr("stroke-width", 4);
svgScale.append("text")
.text("Destination Country has 1 - 10 Million Incoming Tourists/Year")
.attr("x",120)
.attr("y",55)
svgScale.append("line")
.attr("y1",75)
.attr("y2",75)
.attr("x1",100)
.attr("x2",115)
.style("stroke", "#FF0000")
.attr("stroke-width", 4);
svgScale.append("text")
.text("Destination Country has 10 - 50 Million Incomingg Tourists/Year")
.attr("x",120)
.attr("y",79)
svgScale.append("line")
.attr("y1",102)
.attr("y2",102)
.attr("x1",100)
.attr("x2",115)
.style("stroke", "#996633")
.attr("stroke-width", 4);
svgScale.append("text")
.text("Destination Country More than 50 Million Incoming Tourists/Year")
.attr("x",120)
.attr("y",106)
svgScale.append("line")
.attr("y1",135)
.attr("y2",135)
.attr("x1",100)
.attr("x2",115)
.style("stroke", "#FF66CC")
.attr("stroke-width", 4);
svgScale.append("text")
.text("Incoming Tourist Data Unknown")
.attr("x",120)
.attr("y",139)
svgScale.append("circle")
.attr("cx",500)
.attr("cy",15)
.attr("r",11)
.attr("opacity",0.7)
.attr("fill","brown");
svgScale.append("text")
.text("GDP Data Unavailable")
.attr("x",520)
.attr("y",16);
svgScale.append("circle")
.attr("cx",500)
.attr("cy",40)
.attr("r",11)
.attr("opacity",0.7)
.attr("fill","#FF7F0E");
svgScale.append("text")
.text("The Low GDP per Capita Bracket, GDP per Capita < 13,049 $/Year ")
.attr("x",520)
.attr("y",42)
svgScale.append("circle")
.attr("cx",500)
.attr("cy",65)
.attr("r",11)
.attr("opacity",0.7)
.attr("fill","#FFBB78");
svgScale.append("text")
.text("The Lower Middle GDP per Capita Bracket, 13,049 $/Year < GDP per Capita < 17,399 $/Year")
.attr("x",520)
.attr("y",67)
svgScale.append("circle")
.attr("cx",500)
.attr("cy",90)
.attr("r",11)
.attr("opacity",0.7)
.attr("fill","#2CA02C");
svgScale.append("text")
.text("The Upper Middle GDP per Capita Bracket, 17,399 $/Year < GDP per Capita < 21,749 $/Year ")
.attr("x",520)
.attr("y",93)
svgScale.append("circle")
.attr("cx",500)
.attr("cy",115)
.attr("r",11)
.attr("opacity",0.7)
.attr("fill","#1F77B4");
svgScale.append("text")
.text("The High GDP per Capita Bracket, GDP per Capita > 21,749 $/Year")
.attr("x",520)
.attr("y",118)
</script>
</body>
</html>