Skip to content

Instantly share code, notes, and snippets.

@DimsumPanda
Last active November 15, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DimsumPanda/d33195c18a06a92d4352 to your computer and use it in GitHub Desktop.
Save DimsumPanda/d33195c18a06a92d4352 to your computer and use it in GitHub Desktop.
Week 3: Cleaned data file

Week 3: Cleaned data file

Assigment:

  • fix any column names you think should/could be improved in the csv (and give it a new name)
  • load your data in d3 using d3.csv, and in the function in d3.csv:
  • add a property to the data set in javascript that is the sum of the years' values for each
  • add a property to the data set in javascript that is the difference between 2010 and 2013
  • sort the data by the new value that is the sum of the refugees
  • draw your

    's for each row of the fixed data set, using the sort order for sum of refugees (largest at the top)

name year1990 year2000 year2010 year2013
Afghanistan 6339095 3587336 3054709 2556507
Albania 1822 6802 14772 10073
Algeria 19 8034 6689 3659
Angola 407760 433760 134858 10295
Argentina 334 609 557 388
Armenia 1 5786 17547 11998
Azerbaijan 0 284277 16753 10809
Bahrain 39 95 87 275
Bangladesh 477 5401 10049 9727
Belarus 0 2519 5743 4396
Benin 2 54 442 302
Bhutan 0 108945 75070 31566
Bolivia 731 175 590 601
Bosnia and Herzegovina 0 474981 63004 26802
Brazil 12 224 994 985
Bulgaria 1922 2404 2559 1880
Burkina Faso 2 130 1145 1584
Burundi 191622 568084 84064 72652
Cote d'Ivoire 2 773 41758 85651
Cambodia 54364 36855 16314 13748
Cameroon 100 2062 14963 11427
Central African Republic 100 139 164905 252865
Chad 184806 54962 53733 48640
Chile 14548 860 1170 596
China 1426 110975 184602 195134
Colombia 494 9279 395577 396717
Comoros 4 28 368 515
Congo 2 27579 20679 11749
Costa Rica 0 57 352 462
Croatia 0 335199 65861 49756
Cuba 13523 20084 7477 7460
Czech Republic 6123 810 817 991
Democratic People's Republic of Korea 0 13 917 1166
Democratic Republic of the Congo 67423 371713 476693 499528
Djibouti 5 1910 566 762
Dominican Republic 0 47 246 306
Ecuador 3 671 852 707
Egypt 48 3953 6913 12810
El Salvador 30019 7756 4976 9635
Equatorial Guinea 0 509 305 200
Eritrea 43 376851 222460 308006
Estonia 0 455 240 352
Ethiopia 1345928 66410 68848 77102
Fiji 1 401 1851 1112
Gabon 0 26 165 176
Gambia 4 750 2242 3397
Georgia 0 21821 10640 6779
Germany 0 1183 164 175
Ghana 4691 14775 20203 21079
Greece 16 125 51 92
Guatemala 47988 20711 5679 6615
Guinea 1497 11985 14555
Guinea-Bissau 5003 886 1127 1223
Guyana 2 36 749 800
Haiti 2092 7561 25892 38659
Honduras 18 903 1302 3301
Hungary 5767 518 1438 1220
India 198 11399 17769 11041
Indonesia 7169 9149 16892 14786
Iraq 1133805 526179 1683579 401384
Islamic Republic of Iran 75889 88278 68791 75038
Israel 16 416 1301 1044
Jamaica 0 37 1057 1503
Jordan 55 919 2254 1632
Kazakhstan 0 2537 3632 2122
Kenya 10 2373 8602 8586
Kuwait 44 692 988 977
Kyrgyzstan 0 748 2744 2299
Lao People's Democratic Republic 135633 16094 8414 7745
Latvia 0 491 713 233
Lebanon 6993 10215 15869 3818
Liberia 735689 266930 70129 17567
Libya 581 619 2309 3316
Lithuania 4 163 515 220
Madagascar 0 54 270 296
Malawi 53 33 171 326
Malaysia 0 82 552 485
Mali 1 364 3663 152828
Mauritania 65999 29752 37733 34251
Mexico 0 1291 6816 9395
Mongolia 1 23 1724 2064
Montenegro 0 0 3246 591
Morocco 15 392 2284 1307
Mozambique 1247992 30 131 56
Myanmar 28 137128 415670 479606
Namibia 532 2308 1017 1143
Nepal 0 235 5889 8111
Nicaragua 52618 5071 1431 1538
Niger 0 493 803 682
Nigeria 16 5742 15642 31210
Pakistan 486 10133 39982 48686
Panama 629 67 100 105
Paraguay 19 19 86 95
Peru 256 6944 5834 4765
Philippines 10 45482 970 724
Poland 25632 865 1764 1429
Republic of Korea 0 73 585 500
Republic of Moldova 0 2657 6200 2207
Romania 51282 7348 3934 2330
Russian Federation 117736 40310 111948 74321
Rwanda 361322 119056 114836 83937
Saint Lucia 0 11 334 710
Saint Vincent and the Grenadines 0 48 946 1535
Saudi Arabia 0 77 667 584
Senegal 60006 11088 16267 19822
Serbia (and Kosovo) 1745 146748 183289 48679
Sierra Leone 9 402807 11275 5311
Singapore 0 22 76 65
Slovakia 0 220 158 323
Slovenia 0 3284 38 28
Somalia 470174 475655 770154 1121772
South Africa 14783 168 380 423
South Sudan 0 0 0 114467
Sri Lanka 208271 124160 141074 123083
State of Palestine 8521 110640 93323 96044
Sudan 523998 494363 387288 649326
Suriname 6620 334 25 17
Syrian Arab Republic 2148 5871 18452 2468346
Tajikistan 0 59940 577 661
Thailand 20 217 356 222
Macedonia 0 2176 7889 1632
Tibetans 17800 20968 15082 15065
Timor-Leste 0 122202 8 10
Togo 6 4016 18330 10327
Tunisia 42 1207 2174 1363
Turkey 5508 47155 146794 66578
Turkmenistan 0 300 738 514
Uganda 58820 32414 6441 8176
Ukraine 0 19312 25111 5158
United Republic of Tanzania 8 313 1144 1040
United States 1 176 3026 4760
Uruguay 842 51 186 146
Uzbekistan 0 3628 8840 4951
Venezuela 0 471 6701 8395
Viet Nam 496029 370758 338698 314106
Western Sahara 165000 165810 116415 116504
Yemen 8 2113 2076 2428
Zambia 1 98 228 232
Zimbabwe 4 109 24089 19681
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Refugees by Origin</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
</head>
<body>
<h1>Refugees by Origin</h1>
<script type="text/javascript">
//Load in contents of CSV file, and do things to the data.
var total1990 = 0;
var total2000 = 0;
var total2010 = 0;
var total2013 = 0;
d3.csv("d3.csv", function(error, data) {
if (error) {
console.log("Had an error loading file.");
}
// let's do things to the data here. This is VERY COMMON in d3 code.
console.log(data);
// Your arguments for forEach are the item and it's number in the array:
data.forEach(function(data_item, index){
// NB: it's much more common to see (d, i) as shorthand in D3 code.
console.log(index, data_item);
// here we are making strings into numbers using type coercion:
data_item.year1990 = +data_item.year1990;
data_item.year2000 = +data_item.year2000;
data_item.year2010 = +data_item.year2010;
data_item.year2013 = +data_item.year2013;
// now we add another data object value, a calculated value.
data_item.difference = data_item.year2013 - data_item.year2010;
data_item.sum = data_item.year1990 + data_item.year2000 + data_item.year2010 + data_item.year2013;
total1990 = total1990 + data_item.year1990;
total2000 = total2000 + data_item.year2000;
total2010 = total2010 + data_item.year2010;
total2013 = total2013 + data_item.year2013;
console.log(index, data_item);
});
console.log(data);
console.log("total 1990", total1990);
console.log("total 2000", total2000);
console.log("total 2010", total2010);
console.log("total 2013", total2013);
// Sorting:
data.sort(function (a, b) {
return b.sum - a.sum;
});
// Suppose you wanted it sorted the other way? How would you do that?
console.log("sorted", data);
//Other code to execute after successful file Load
d3.select("body").append("h1").text("My Data");
d3.select("body").append("h3").text("Country 1990 2000 2010 2013 Total")
d3.select("body").selectAll("p")
.data(data)
.enter()
.append("p")
.text(function(d){
return d.name + " " + d.year1990 + " " + d.year2000 + " " + d.year2010 + " " + d.year2013 + " " + d.sum;
});
});
// Notice this below doesn't work. It's outside the scope of the function that loaded the data. You need to keep your operations on data inside the loading callback.
//console.log("my data now", data);
// what do you expect these values to be now? what's going on?
// console.log("totals", total1990, total2015);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment