Skip to content

Instantly share code, notes, and snippets.

@jashcny
Created January 18, 2016 20:55
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 jashcny/d5632fe6efeb61c39af4 to your computer and use it in GitHub Desktop.
Save jashcny/d5632fe6efeb61c39af4 to your computer and use it in GitHub Desktop.
Week 1: CSV data.
<!DOCTYPE html>
<!-- This was one of Scott Murray's Knight D3 course files, modified a bit. -->
<html lang="en">
<head>
<meta charset="utf-8">
<title>Loading CSV Data with D3</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
</head>
<body>
<h1>Loading CSV and JSON with D3.</h1>
<p>Look in the console for logging info after the data loads.</p>
<p>If you open the console after loading, you'll need to reload the page.</p>
<p>We're printing the contents of the files to the screen here, just to show they load.
<p class="csvdata"></p>
<p class="jsondata"></p>
<script>
//Load in contents of CSV file
d3.csv("kidnapping.csv", function(data1) {
//Now CSV contents have been transformed into
//an array of JSON objects.
console.log("The first file is csv...")
//Log 'data' to the console, for verification.
console.log(data1);
$("p.csvdata").text(JSON.stringify(data1));
});
d3.json("kidnapping.csv", function(data2) {
//Now json contents have been loaded into
//an array of JSON objects.
console.log("This file is json!")
//Log 'data' to the console, for verification.
console.log(data2);
$("p.jsondata").text(JSON.stringify(data2));
});
// Notice that the objects look the same. d3.csv() parses rows as objects.
// Also this is a good point to talk about scope and breakpoints.
</script>
</body>
</html>
Region Sub-region Country/territory 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
Africa Eastern Africa Burundi 24 31 24 49 49 58
Kenya 235 152 220 173 73 95 79 55 75 59
Mauritius 34 35 50
Rwanda 8 8 12 12 10 21
Uganda* 54 65 202
Zimbabwe 139 198 43 90 256 222
Middle Africa Sao Tome and Principe 0 0 0 0 0
Northern Africa Algeria 220 149 101 302 241 205 207 177 168 106 223
Egypt 16 21 18 29 36 47 70 93 273
Morocco 392 342 295 264 233 217 307 556 974
Sudan 616
Southern Africa Botswana 3 3 5
Lesotho 64 40 62
Swaziland 122 96
Western Africa Cabo Verde 6 7 22 28
Guinea 14 4 16 0 18 25
Nigeria 277 309 703 738 600 574
Americas Caribbean Bahamas 24 27 31 35 27 18 26 28
Barbados 12 23 14 17 12 18 12 13 9 8
Dominican Republic 20 14 19
Grenada 0 0 0 0 0 0 0 0
Jamaica 31 38 38 43 11 21
St. Kitts and Nevis 2 2 3
St. Vincent and the Grenadines 3 2 3 8 2 1 2 5 4
Trinidad and Tobago 28 58 17 14 17 8 7 3 3 4
Central America Belize 9 8 2 3
Costa Rica 5 8 11 6 8 8 5 11 12 5 4
El Salvador 4 8 21 10 22 29 15 14 14
Guatemala 51 52 57 98 213 162 133 124 83
Honduras 37 79 51
Mexico 169 323 325 595 438 907 1,162 1,284 1,344 1,512 1,888
Nicaragua 11 6 7
Panama* 11 17 13 29 21 28 38 25 17 216
Northern America Bermuda* 3 2
Canada** 3,198 3,457 3,896 4,488 4,660 4,732 4,791 4,301 3,780 3,637 3,239
South America Bolivia (Plurinational State of) 99 143 160 125 111 91 104 104
Brazil 525 516 290 552 401 319 414 396
Chile* 161 114 151 247 254 266 246 259 327 254
Colombia 1,442 800 687 521 437 213 282 305 305 299
Ecuador 257 203 37 47
Guyana 1 2 2 1 0 0
Paraguay 1 5 4 9 3 2 0 3
Peru* 275 469 497 611 463 483 726
Uruguay 3 19 20
Asia Central Asia Kazakhstan 92 84 84 89 120 124 196 80 107
Kyrgyzstan 17 24 24 13 34 25 6 84 54 60 41
Tajikistan 199 181 188 155 146 172
Turkmenistan 157 127
Eastern Asia Hong Kong Special Administrative Region of China 0 1 0 0 3 0 0 1 0 0 1
Japan 284 320 277 199 207 156 156 186 153 190 185
Macao Special Administrative Region of China* 0 0 2 0 0 1
Mongolia 0 0 1 0 1 0 1 0 1 0 2
Republic of Korea 127 155 259 298 222 256 195
South-Eastern Asia Brunei Darussalam 1 1 0 0
Indonesia 621 485 398 454 395 361
Philippines 90 42 44 28 35 46 35 21 11 29
Singapore 3 0 0 1 0 0 0 0 0
Thailand 12 11 21 16 5
Southern Asia Bangladesh 1,303 1,119
Bhutan 2 1 0 2 1
India 23,327 22,832 23,991 27,561 30,261 33,860 38,440 44,664 47,592 65,461
Maldives 1 2 5 14 26 19 17 8
Nepal 123 246
Sri Lanka 829 868 526 358 202 180 222 195
Western Asia Armenia 23 22 26 26 27 34 39 34 38
Azerbaijan 28 23 29 21 19 20 19 9 1 9
Bahrain 5 18 12 21 0
Cyprus 20 15 16 15 10 12 26 27 25 30 19
Georgia 94 88 34 11 5 9 4
Israel 397 418 383 337 340 267 265
Jordan 28 34
Kuwait 285 280 278 329 327 358
Lebanon 899 938 937 1,059 1,308 1,484
Oman 13 9
Qatar 7 6
State of Palestine* 126 167 202 83 23 21 24 12
Syrian Arab Republic 85 135 42 54 15 24
Turkey 7,753 7,624 8,737 10,519 9,455 10,764 12,700 13,586 13,584 13,431
United Arab Emirates 434 506 862 971 73
Yemen 102 27 26 48 41
Europe Eastern Europe Belarus 30 34 36 23 31 19 16 19 12 6
Bulgaria 209 192 190 125 138 127 142 118 77 75 87
Czech Republic 14 15 13 16 15 8 7 10 8 8 11
Hungary 36 19 7 13 7 11 14 18 12 9 7
Poland 110 117 22 23 25 37 59 26 11 27 445
Republic of Moldova 16 18 20 19 16 38 51 92 116
Romania 321 272 280 289 287 265
Russian Federation 1,117 1,135 994 837 698 633 519 520 512 442
Slovakia 16 12 4 3 6 4 4 7 4
Ukraine 205 172 178 233 256 279 338 292
Northern Europe Estonia 2 1 2 1 2 2 0 0 3 0 1
Finland 1 2 2 1 1 0 3 1 3 1 1
Ireland 53 74 74 81 106 77 146 134 109 101 120
Latvia 26 41 6 8 9 3 9 8 7 1 18
Lithuania 37 62 70 60 66 61 59 69 48 65 46
United Kingdom (England and Wales) 3,125 2,814 2,799 2,367 1,991 2,035 1,860 1,717 1,516 1,387 1,727
United Kingdom (Northern Ireland) 71 80 46 53 67 83 82 56 66 45 59
United Kingdom (Scotland) 353 347 343 284 319 269 260 219 227 253
Southern Europe Albania 13 13 10 5 9 2 2 5
Andorra 0 0 0 0 0 0 0
Bosnia and Herzegovina 7 9 5 8 6 1 1 2 5
Croatia 14 20 7 14 16 12 13 6 6 1
Greece 24 25 28 30 30 16 173 158 139 95
Italy 132 154 368 278 364 325 349 351 356 341 325
Kosovo under UNSCR 1244 46 50 43 31 33 34
Malta* 0 0 0 0 0 0
Montenegro 4 2 3 3 2 3 0 1
Portugal 446 438 556 443 492 539 501 507 419 432
Serbia 26 23 19 16 24 20 9 11 9
Slovenia 8 1 3 6 9 7 2 4 2 4 4
Spain 208 223 216 237 171 127 111 118 121
The former Yugoslav Rep. of Macedonia 24 24 30 14 16 17 14 19 18
Western Europe Austria 3 5 4 5 11 5 9 5 11 4
Belgium 1,003 1,066 919 915 958 1,013 1,080 1,121 1,187 1,170 1,135
France 2,031 2,143 2,012 2,288 2,109 2,074 2,085 2,092 2,234 2,117 2,266
Germany 1,830 1,727 1,596 1,825 1,857 1,810 1,699 1,688 1,643 1,702
Liechtenstein 0 0 0 0 0 0 0 0 0
Luxembourg 17 36 33 40 27 47
Monaco 0 2
Netherlands* 910 850 780 760 625 645 560 580 520
Switzerland* 263 267 245 274 269 309 359 309 308 295 324
Oceania Australia and New Zealand Australia* 4,090 3,422 608 675 638 601
New Zealand* 289 351 338 342 302 229 278 235
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment