Skip to content

Instantly share code, notes, and snippets.

@samirgambhir
Created April 25, 2015 05:18
Show Gist options
  • Save samirgambhir/366ba2407d853d018aca to your computer and use it in GitHub Desktop.
Save samirgambhir/366ba2407d853d018aca to your computer and use it in GitHub Desktop.
Module 5 exercise - Global refugee data
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Global Refugee Data</title>
<script type="text/javascript" src="http://d3js.org/d3.v3.js"></script>
<style type="text/css">
body {
background-color: white;
font-family: Book Antiqua;
font-weight: bold;
margin-left: 50px;
}
h1 {
font-family: Book Antiqua;
font-size: 24px;
margin: 0;
color: #323232;
}
p {
font-family: Book Antiqua;
font-size: 14px;
color: #323232;
margin: 10px 0 0 0;
}
svg {
background-color: #323232;
}
circle:hover {
fill: #ff9900;
}
.axis path, .axis line {
fill: none;
stroke: white;
shape-rendering: crispEdges;
}
.axis text {
font-family: Book Antiqua;
fill: white;
font-size: 10px;
}
.y.axis path, .y.axis line {
fill: none;
stroke: white;
shape-rendering: crispEdges;
}
.countryName {
font-weight: bold;
font-size: 14px;
color: white;
}
.dataVal {
font-weight: bold;
color: white;
}
.toolTip {
padding: 6px;
background-color: #323232;
border-radius: 4px;
position: absolute;
font-size: 12px;
color: #ff9900;
line-height: 18px;
visibility: hidden;
}
.subhead {
fill: white;
font-size: 14px;
}
</style>
</head>
<body>
<div class="toolTip"></div>
<h1>Global Refugee Data</h1>
<p>
Average annual refugees (2009-2013) and GDP at PPP 2013 for selected countries. Source: <a href="http://popstats.unhcr.org/#_ga=1.152203577.1033056182.1429126874">UNHCR</a> and <a href="http://data.worldbank.org/indicator/NY.GDP.PCAP.PP.CD">World Bank</a>
</p>
<br/>
<script type="text/javascript">
var w = 900;
var h = 750;
var tt;
var padding = [50, 10, 75, 100];
//Top, right, bottom, left
var xScale = d3.scale.linear().range([padding[3], w - padding[1] - padding[3]]);
var yScale = d3.scale.linear().range([padding[0], h - padding[2]]);
var xAxis = d3.svg.axis().scale(xScale).orient("bottom").ticks(10);
var yAxis = d3.svg.axis().scale(yScale).orient("left").ticks(15);
var svg = d3.select("body").append("svg").attr("width", w).attr("height", h);
svg.append('text').text('GDP per capita at PPP 2013 in USD').attr('transform', function(d) {
return "rotate(-90 0, 360)"
}).attr('x', 0).attr('y', h / 2 + 15).attr('text-anchor', 'middle').attr('class', 'y subhead').attr('opacity', 1)
svg.append('text').text('Average annual refugees 2009-2013').attr('x', w / 2).attr('y', h - 15).attr('text-anchor', 'middle').attr('class', 'x subhead').attr('opacity', 1)
d3.csv("RefugeeData.csv", function(data) {
xScale.domain([d3.min(data, function(d) {
if (d.ref_p_gdp > 10) {
return +d.avg_refugees;
}
}), d3.max(data, function(d) {
if (d.ref_p_gdp > 10) {
return +d.avg_refugees;
}
})]);
yScale.domain([d3.max(data, function(d) {
if (d.ref_p_gdp > 10) {
return +d.gdp_ppp_13;
}
}), d3.min(data, function(d) {
if (d.ref_p_gdp > 10) {
return +d.gdp_ppp_13;
}
})]);
var circlesData = svg.selectAll("circle").data(data).enter().append("circle");
circlesData.attr("cx", function(d) {
if (d.ref_p_gdp > 10) {
return xScale(d.avg_refugees);
}
}).attr("cy", function(d) {
if (d.ref_p_gdp > 10) {
return yScale(d.gdp_ppp_13);
}
}).attr("r", function(d) {
if (d.ref_p_gdp > 10) {
return 0.2;
}
}).attr("fill", "#ffc04c");
circlesData.sort(function(a, b) {
return d3.descending(+a.ref_p_gdp, +b.ref_p_gdp);
}).transition().delay(function(d, i) {
if (d.ref_p_gdp > 10) {
return i * 25;
}
}).duration(1000).attr("cx", function(d) {
if (d.ref_p_gdp > 10) {
return xScale(d.avg_refugees);
}
}).attr("cy", function(d) {
if (d.ref_p_gdp > 10) {
return yScale(d.gdp_ppp_13);
}
}).attr("r", function(d) {
if (d.ref_p_gdp > 10) {
return 5;
}
}).attr("fill", "#ffc04c");
svg.append("g").attr("class", "x axis").attr("transform", "translate(0," + (h - padding[2] + 10) + ")").call(xAxis);
svg.append("g").attr("class", "y axis").attr("transform", "translate(" + (padding[3] - 10) + ",0)").call(yAxis);
circlesData.style('cursor', 'pointer')
circlesData.on('mouseover', function(d) {
d3.select(this).classed('hover', true).transition().attr('r', 10)
tt = d3.select('.toolTip');
tt.html('<span class="countryName">' + d.country + '</span><br/>' + 'Average annual refugees : <span class="dataVal">' + Math.floor(Math.round(d.avg_refugees)) + '</span><br/>GDP per capita at PPP : <span class="dataVal">USD ' + Math.floor(Math.round(d.gdp_ppp_13)) + '</span>').style('left', d3.event.pageX + 10 + 'px').style('top', d3.event.pageY + 10 + 'px').style('visibility', 'visible')
})
circlesData.on('mouseout', function() {
d3.select(this).classed('hover', false).transition().attr('r', 5)
tt.style('visibility', 'hidden')
})
});
</script>
</body>
</html>
country c_code avg_refugees avg_asylees avg_idps gdp_ppp_13 ref_p_gdp asy_p_gdp pop_2013
United States 002 266086.4 37015.4 0 53041.98141 5.016524514 0.697851004 316128839
Canada 020 164789.8 41760 0 43247.04434 3.810429186 0.965615122 35158304
Bahamas 031 21.4 9 0 23264.27221 0.000919865 0.000386859 377374
Cuba 040 399.4 4.2 0 18796 0.021249202 0.000223452 11265629
Haiti 041 0.6 8.4 0 1703.029349 0.000352313 0.004932387 10317461
Dominican Republic 042 533.6 1025.6 0 12186.38856 0.043786557 0.08415947 10403761
Jamaica 051 21 0 0 8891.891542 0.002361702 0 2715000
Trinidad and Tobago 052 21.2 72.6 0 30446.00754 0.000696315 0.002384549 1341151
Barbados 053 0 0 0 15574 0 0 284644
Grenada 055 0.6 0.6 0 11645.3406 0.0000515 0.0000515 105897
Saint Lucia 056 0.8 2.8 0 10487.96828 0.0000763 0.000266973 182273
Saint Vincent and the Grenadines 057 0 0.4 0 10490.60417 0 0.0000381 109373
Antigua and Barbuda 058 0 0 0 21027.82664 0 0 89985
Saint Kitts and Nevis 060 0 0.2 0 21395.69322 0 0.0000935 54191
Mexico 070 1521.2 511.6 0 16463.39267 0.092398938 0.031075004 122332399
Belize 080 98.2 48.8 0 8486.89854 0.011570776 0.005750039 331900
Guatemala 090 145.8 6.6 0 7296.592573 0.01998193 0.000904532 15468203
Honduras 091 14.4 4.6 0 4592.593399 0.003135483 0.001001613 8097688
El Salvador 092 37.6 16.2 0 7764.142745 0.004842775 0.002086515 6340454
Nicaragua 093 116.6 8.8 0 4642.696251 0.025114716 0.00189545 6080478
Costa Rica 094 19930.2 527.4 0 13875.85635 1.436322162 0.038008465 4872166
Panama 095 17260.4 606.4 0 19416.23202 0.888967539 0.0312316 3864170
Colombia 100 205.6 107.6 4035198 12423.92304 0.016548718 0.00866071 48321405
Venezuela, RB 101 202563.4 9907.6 18198.37002 11.13085402 0.544422385 30405207
Guyana 110 5.6 0.2 0 6545.93223 0.000855493 0.0000306 799613
Suriname 115 0.4 2.6 0 16071.38463 0.0000249 0.000161778 539276
Ecuador 130 121623.4 29812 0 10889.98913 11.16836744 2.737560125 15737878
Peru 135 1125.6 419.4 0 11774.18834 0.095598946 0.03562029 30375603
Brazil 140 4577 2348 0 15037.45747 0.304373263 0.156143418 200361925
Bolivia 145 710.4 20.2 0 6131.061689 0.115869002 0.003294699 10671200
Paraguay 150 117 6.2 0 8092.672757 0.014457523 0.000766125 6802295
Chile 155 1650.4 378 0 21911.30097 0.075321863 0.017251372 17619708
Argentina 160 3330 1140.8 0 22363 0.148906676 0.051012834 41446246
Uruguay 165 178.4 38.8 0 19594.36956 0.009104656 0.001980161 3407062
United Kingdom 200 195362.6 16871.2 0 38451.65716 5.080732911 0.438763925 64097085
Ireland 205 7841 4850.2 0 46139.6166 0.169940727 0.105120076 4595281
Netherlands 210 74423.2 12111.6 0 46298.43586 1.607466831 0.26159847 16804224
Belgium 211 20687 16400.4 0 41663.11374 0.496530339 0.393643166 11195138
Luxembourg 212 3015.2 1011 0 90410.14365 0.03335024 0.011182374 543202
France 220 211522 47980.2 0 37871.85562 5.585202958 1.266909139 66028467
Monaco 221 21.6 0.2 0 85500 0.000252632 0.0000234 37831
Liechtenstein 223 94 33 0 89400 0.001051454 0.000369128 36925
Switzerland 225 49717.4 18150.8 0 56564.93595 0.878943804 0.320884302 8081482
Spain 230 4220.8 3145.2 0 32925.474 0.128192536 0.095524821 46647421
Portugal 235 439 132 0 26759.02612 0.016405679 0.004932915 10459806
Germany 255 507399 74934.6 0 44469.43809 11.41006097 1.685080883 80621788
Poland 290 15804.2 2377.2 0 23649.11018 0.668278843 0.100519638 38530725
Austria 305 47179.8 25472.2 0 45492.82364 1.037082252 0.559916883 8473786
Hungary 310 4602 721.2 0 23482.10205 0.195979048 0.030712753 9897247
Czech Republic 316 2630.2 816.4 0 28769.85777 0.091422072 0.028376922 10521468
Slovakia 317 548.2 220 0 26642.70848 0.020575986 0.008257419 5414095
Italy 325 62441.4 9983.2 0 35597.31606 1.754104155 0.28044811 59831093
Malta 338 7433.2 1244.4 0 29133.11181 0.255146105 0.042714284 423282
Albania 339 78 64.6 0 10374.01564 0.007518786 0.006227097 2773620
Montenegro 341 14585.2 66.6 0 14131.605 1.032097911 0.00471284 621383
The former Yugoslav Republic of Macedonia 343 1225.4 439.4 0 11611.97491 0.105528991 0.037840247 2107158
Croatia 344 876.2 174.4 882 21365.58246 0.041009881 0.008162661 4252700
Bosnia and Herzegovina 346 6978.6 127.2 105591 9535.539791 0.731851594 0.01333957 3829307
Slovenia 349 222.2 80.8 0 28995.64349 0.00766322 0.002786626 2060484
Greece 350 2051 46767.4 0 25705.03739 0.079789808 1.819386577 11032328
Cyprus 352 3450 3742 0 28224.45744 0.122234413 0.132580051 1141166
Bulgaria 355 4635.8 1953.2 0 15731.67262 0.29467941 0.124157173 7265115
Moldova 359 166.4 62.4 0 4666 0.035662237 0.013373339 3559000
Romania 360 1215.8 339 0 18991.30195 0.064018781 0.017850277 19963581
Russian Federation 365 4065.8 1234.8 36754 24114.0881 0.168606832 0.051206581 143499861
Estonia 366 43.6 11 0 25461.85075 0.001712366 0.000432019 1324612
Latvia 367 94 137.2 0 22560.22991 0.004166624 0.006081498 2013385
Lithuania 368 834.2 70.6 0 25467.04594 0.032756057 0.00277221 2956121
Ukraine 369 3851 3835 0 8789.982001 0.438112387 0.436292133 45489600
Belarus 370 587.8 67.4 0 17619.76966 0.033360255 0.003825249 9466000
Armenia 371 5280.4 106 0 7776.286043 0.679038807 0.013631186 2976566
Georgia 372 654.4 180.4 304748 7176.443626 0.091187228 0.025137799 4476900
Azerbaijan 373 1621.6 103 597486 17143.47923 0.094589901 0.006008115 9416598
Finland 375 9291.4 2303.8 0 39812.37922 0.233379672 0.057866424 5439407
Sweden 380 91518.8 20280.8 0 45148.12526 2.027078632 0.449205806 9592552
Norway 385 41526.4 11184.6 0 65461.16539 0.634366953 0.170858553 5084190
Denmark 390 15322.6 1825.2 0 43444.62971 0.352692614 0.042012097 5613706
Iceland 395 60 105.2 0 41938.92995 0.001430652 0.002508409 323002
Sao Tome and Principe 403 0 0 0 2971.06708 0 0 192993
Guinea-Bissau 404 7938.2 182.4 0 1407.026772 5.641825839 0.12963506 1704255
Equatorial Guinea 411 0 0 0 33768.28985 0 0 757014
Gambia 420 9486.8 33.8 0 1661.423554 5.710043039 0.020344 1849285
Mali 432 14192 1285.6 96550 1641.834521 8.643989279 0.783026537 15301650
Senegal 433 18389 2403.6 0 2241.996525 8.202064452 1.072080163 14133280
Benin 434 5342.4 164 0 1790.753637 2.983324948 0.091581554 10323474
Mauritania 435 50659 452.6 0 3042.838494 16.64859969 0.148742696 3889880
Niger 436 21820.6 70.8 0 916.1996912 23.81642366 0.07727573 17831270
Cote d'Ivoire 437 16395.6 450.2 245865 3210.454644 5.106940236 0.14022936 20316086
Guinea 438 12992.4 626.6 0 1253.0849 10.36833179 0.500045927 11745189
Burkina Faso 439 14030.8 634.2 0 1514.186221 9.266231459 0.41883884 16934839
Liberia 450 55825.2 244.6 0 877.9954801 63.58255967 0.278589133 4294077
Sierra Leone 451 6505.2 117.6 0 1544.18324 4.212712477 0.076156765 6092075
Ghana 452 15151.2 3680.4 0 3992.090039 3.795305179 0.921923094 25904598
Togo 461 17199 300.8 0 1390.519711 12.36875671 0.216321996 6816982
Cameroon 471 103660.6 3687.4 0 2830.129539 36.62751071 1.302908559 22253959
Nigeria 475 6303.4 1265.6 0 5602.408796 1.12512318 0.22590283 173615345
Gabon 481 4573.6 3118 0 19264.33594 0.237412803 0.16185349 1671711
Central African Republic 482 18735.6 2025 288167 603.7429598 31.03241155 3.354076378 4616417
Chad 483 372216.8 163.6 107064 2088.591836 178.2142368 0.078330288 12825314
Congo 484 107046.6 3883.6 0 5868.480551 18.24093972 0.661772663 4447632
Democratic Republic of the Congo 490 136672 1197.8 2223241 809.2175334 168.8940172 1.480195313 67513677
Uganda 500 164201.8 21617.2 120335 1413.15463 116.1952107 15.29712286 37578876
Kenya 501 485634.6 35281.8 282200 2794.981319 173.7523599 12.62326863 44353691
United Republic of Tanzania 510 112476 742 0 1775.05858 63.36466934 0.418014373 49253126
Burundi 516 35458.2 8124.2 98772 771.7154295 45.94724771 10.5274557 10162532
Rwanda 517 59258.6 511 0 1473.638012 40.21245349 0.346760871 11776522
Somalia 520 2115.2 14625.4 1327318 600 3.525333333 24.37566667 10495583
Djibouti 522 17339.8 2087 0 2998.740586 5.782360796 0.695958834 872932
Ethiopia 530 275068.2 1319.4 0 1380.000264 199.3247444 0.956086773 94100756
Eritrea 531 4209 58.4 0 1195.676757 3.520182168 0.048842632 6333135
Angola 540 18659.2 10402.2 0 7736.174682 2.411941401 1.344618035 21471618
Mozambique 541 4106.8 7711.6 0 1105.073498 3.7163139 6.978359373 25833752
Zambia 551 39901.2 962.8 0 3925.462387 10.16471337 0.245270469 14538640
Zimbabwe 552 4744.8 546.6 34469 1832.083244 2.589838652 0.298348889 14149648
Malawi 553 5965.8 9151.2 0 780.0031076 7.648431067 11.73226095 16362567
South Africa 560 58971.2 232691 0 12506.74948 4.715150017 18.60523395 52981991
Namibia 565 4917.4 1182.8 0 9583.184757 0.513127955 0.123424522 2303315
Lesotho 570 18.4 1.2 0 2576.255494 0.007142149 0.000465792 2074465
Botswana 571 2974.2 216.2 0 15751.90221 0.188815291 0.013725326 2021144
Swaziland 572 654.6 311.6 0 6685.338117 0.097915766 0.04660946 1249514
Madagascar 580 4.8 0.4 0 1413.990757 0.003394647 0.000282887 22924851
Comoros 581 0 0 0 1446.171495 0 0 734917
Mauritius 590 0 0 0 17199.52942 0 0 1296303
Morocco 600 897.6 1371.8 0 7198.163719 0.12469847 0.190576382 33008150
Algeria 615 94136.8 961.6 0 13320.23219 7.067204134 0.072190934 39208194
Tunisia 616 1285 259.6 0 11124.49601 0.115510851 0.023335889 10886500
Libya 620 8550.2 3205.2 41314 21046.34734 0.406255768 0.15229246 6201521
Sudan 625 163209.4 7474.8 1765472 3372.972007 48.3874161 2.216087173 37964306
South Sudan 626 107438.2 28.8 247386 2029.745312 52.93186262 0.014188972 11296173
Islamic Republic of Iran 630 951183.2 828.2 0 15590.15196 61.01179784 0.053123279 77447168
Turkey 640 182364.2 18027.2 0 19020.06506 9.587990335 0.947799071 74932641
Iraq 645 90035.6 4391.8 1262778 14951.07768 6.02201406 0.293744712 33417476
Egypt 651 124907.4 17348.2 0 11089.21118 11.26386701 1.564421465 82056378
Syrian Arab Republic 652 688231 2410 1707460 5100 134.9472549 0.47254902 22845550
Lebanon 660 211587 1585 0 17173.84224 12.32030649 0.09229152 4467390
Jordan 663 459456.2 3008.4 0 11784.53169 38.98807454 0.255283797 6459000
Israel 666 36249.6 5951 0 32760.41121 1.106506257 0.181652176 8059400
Saudi Arabia 670 577 92.6 0 53644.12693 0.01075607 0.001726191 28828870
Yemen 679 210828.8 4893 302045 3959.291883 53.24911783 1.235827048 24407381
Kuwait 690 408.4 1847.2 0 88259 0.00462729 0.02092931 3368572
Bahrain 692 217.2 68.6 0 43714.95324 0.004968552 0.001569257 1332171
Qatar 694 74 28 0 136727.2536 0.000541223 0.000204787 2168673
United Arab Emirates 696 545.4 74.4 0 59845 0.009113543 0.001243212 9346129
Oman 698 92.6 31.6 0 43304 0.002138371 0.000729725 3632444
Afghanistan 700 8505.6 41.8 442833 1946.193806 4.370376667 0.02147782 30551674
Turkmenistan 701 53.4 0 0 14004.16006 0.003813153 0 5240072
Tajikistan 702 2684.2 1921 0 2512.247566 1.068445656 0.76465394 8207834
Kyrgyzstan 703 2876 435.4 82500 3212.932441 0.895132423 0.135514832 5719500
Uzbekistan 704 278.8 0 0 5168.286035 0.053944383 0 30241100
Kazakhstan 705 2101.2 133.8 0 23211.31227 0.090524826 0.005764431 17037508
China 710 301015 161.8 0 11906.50739 25.2815532 0.013589208 1357380000
Mongolia 712 7.2 3.6 0 9434.962769 0.000763119 0.00038156 2839073
Republic of Korea 732 405.4 1288.2 0 33791 0.011997277 0.038122577 50219669
Japan 740 2544.8 4215.2 0 36449.06183 0.069817984 0.115646324 127338621
India 750 185857.8 3985.2 0 5411.615546 34.34423573 0.736415949 1252139596
Pakistan 770 1719796.6 2963.4 961004 4601.688943 373.7316062 0.643980946 182142594
Bangladesh 771 229868.4 2.6 0 2948.01489 77.97396166 0.000881949 156594962
Myanmar 775 0 0 254181 4345 0 0 53259018
Sri Lanka 780 183 509.6 196549 9738.119174 0.01879213 0.052330434 20483000
Nepal 790 74696 619.8 0 2244.799628 33.27513024 0.276104821 27797457
Thailand 800 102435.6 10625 0 14393.5307 7.116780595 0.738178854 67010502
Cambodia 811 93.8 31.8 0 3041.07709 0.030844335 0.010456821 15135169
Lao People's Democratic Republic 812 0 0 0 4822.022074 0 0 6769727
Viet Nam 816 1055 0 0 5294.439869 0.19926565 0 89708900
Malaysia 820 84398.4 17439.6 0 23338.01402 3.616348842 0.747261527 29716965
Singapore 830 4 0 0 78763.38494 0.0000508 0 5399200
Brunei Darussalam 835 0 0 0 71776.64801 0 0 417784
Philippines 840 153.4 56.2 83500 6535.875864 0.023470458 0.008598695 98393574
Indonesia 850 1525.8 4054.4 0 9561.126352 0.159583708 0.424050457 249865631
Timor-Leste 860 0.4 3.4 0 2076 0.000192678 0.001637765 1178252
Australia 900 26455 7175.2 0 43543.81204 0.607549012 0.164781163 23130900
Papua New Guinea 910 9506.8 112.8 0 2539.081704 3.744188296 0.04442551 7321262
New Zealand 920 2076 248 0 34825.70839 0.059611135 0.007121176 4470800
Vanuatu 935 2 0.4 0 2990.91683 0.000668691 0.000133738 252763
Solomon Islands 940 0 0.6 0 2068.961062 0 0.000290001 561231
Fiji 950 3.2 5 0 7750.430659 0.00041288 0.000645125 881065
Tonga 955 1 0.8 0 5304.206689 0.00018853 0.000150824 105323
Palau 986 2.6 0.8 0 15095.98814 0.000172231 0.000053 20918
Federated States of Micronesia 987 0.2 0 0 3395 0.0000589 0 103549
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment