Skip to content

Instantly share code, notes, and snippets.

@alex-rind
Last active December 2, 2017 10:03
Show Gist options
  • Save alex-rind/7eae8e2c19c22922fdef18680bcf998d to your computer and use it in GitHub Desktop.
Save alex-rind/7eae8e2c19c22922fdef18680bcf998d to your computer and use it in GitHub Desktop.
Force Layout from CSV
license: gpl-3.0
source target
A1 A2
A2 A3
A2 A4
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.link {
stroke: #000;
}
.node {
stroke: #fff;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 500;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var force = d3.layout.force()
.size([width, height]);
d3.csv("unhcr_popstats_asylum_2016-top2.csv", function(error, links) {
if (error) throw error;
var nodesByName = {};
// Create nodes for each unique source and target.
links.forEach(function(link) {
link.source = nodeByName(link.Origin);
link.target = nodeByName(link["Country / territory of asylum/residence"]);
});
// Extract the array of nodes from the map by name.
var nodes = d3.values(nodesByName);
// Create the link lines.
var link = svg.selectAll(".link")
.data(links)
.enter().append("line")
.attr("class", "link");
// Create the node circles.
var node = svg.selectAll(".node")
.data(nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 4.5)
.call(force.drag);
//.append("title").text(function(d) { return d.name; })
// Start the force layout.
force
.nodes(nodes)
.links(links)
.on("tick", tick)
.start();
function tick() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
}
function nodeByName(name) {
return nodesByName[name] || (nodesByName[name] = {name: name});
}
});
</script>
Year Country / territory of asylum/residence Origin Population type Value
2016 South Africa Various/Unknown Asylum-seekers 142380
2016 Germany Afghanistan Asylum-seekers 126522
2016 Turkey Afghanistan Asylum-seekers 114693
2016 Germany Syrian Arab Rep. Asylum-seekers 100527
2016 Turkey Iraq Asylum-seekers 99678
2016 Germany Iraq Asylum-seekers 65646
2016 United States of America Mexico Asylum-seekers 63823
2016 United States of America China Asylum-seekers 57450
2016 United States of America El Salvador Asylum-seekers 55594
2016 Malaysia Myanmar Asylum-seekers 48352
2016 United States of America Guatemala Asylum-seekers 45508
2016 South Africa Zimbabwe Asylum-seekers 41260
2016 Sweden Afghanistan Asylum-seekers 34217
2016 United States of America Honduras Asylum-seekers 32614
2016 Austria Afghanistan Asylum-seekers 29783
2016 United States of America Venezuela (Bolivarian Republic of) Asylum-seekers 29105
2016 Jordan Iraq Asylum-seekers 27786
2016 Germany Iran (Islamic Rep. of) Asylum-seekers 27254
2016 Turkey Iran (Islamic Rep. of) Asylum-seekers 24626
2016 Ecuador Various/Unknown Asylum-seekers 24542
2016 Italy Nigeria Asylum-seekers 22980
2016 Germany Pakistan Asylum-seekers 22748
2016 Germany Nigeria Asylum-seekers 20460
2016 Germany Russian Federation Asylum-seekers 19467
2016 Libya Syrian Arab Rep. Asylum-seekers 19366
2016 Egypt Sudan Asylum-seekers 19280
2016 Germany Various/Unknown Asylum-seekers 18117
2016 Uganda Dem. Rep. of the Congo Asylum-seekers 16692
2016 Germany Serbia and Kosovo (S/RES/1244 (1999)) Asylum-seekers 16541
2016 United States of America India Asylum-seekers 16108
2016 Kenya Dem. Rep. of the Congo Asylum-seekers 15989
2016 Germany Somalia Asylum-seekers 15973
2016 Germany Eritrea Asylum-seekers 15400
2016 Sweden Iraq Asylum-seekers 14051
2016 Greece Syrian Arab Rep. Asylum-seekers 13997
2016 United States of America Ecuador Asylum-seekers 13540
2016 Somalia Ethiopia Asylum-seekers 12400
2016 Lebanon Iraq Asylum-seekers 11966
2016 Israel Various/Unknown Asylum-seekers 11677
2016 Italy Pakistan Asylum-seekers 11339
2016 Germany Albania Asylum-seekers 11258
2016 Syrian Arab Rep. Iraq Asylum-seekers 10982
2016 United Kingdom Various/Unknown Asylum-seekers 10957
2016 Egypt Ethiopia Asylum-seekers 10637
2016 Austria Iraq Asylum-seekers 10211
2016 Austria Syrian Arab Rep. Asylum-seekers 9812
2016 Germany Gambia Asylum-seekers 9773
2016 Malawi Dem. Rep. of the Congo Asylum-seekers 9732
2016 United States of America Haiti Asylum-seekers 9245
2016 Angola Guinea Asylum-seekers 9145
2016 Uganda Somalia Asylum-seekers 8852
2016 Sudan Ethiopia Asylum-seekers 8813
2016 Germany Armenia Asylum-seekers 8555
2016 United Rep. of Tanzania Dem. Rep. of the Congo Asylum-seekers 8229
2016 Germany Turkey Asylum-seekers 8198
2016 Kenya Ethiopia Asylum-seekers 8173
2016 Switzerland Afghanistan Asylum-seekers 7761
2016 Italy Eritrea Asylum-seekers 7683
2016 Bulgaria Afghanistan Asylum-seekers 7636
2016 Yemen Ethiopia Asylum-seekers 7539
2016 Uganda Eritrea Asylum-seekers 7353
2016 Switzerland Eritrea Asylum-seekers 7344
2016 Germany Ethiopia Asylum-seekers 7247
2016 Djibouti Ethiopia Asylum-seekers 7243
2016 Sweden Syrian Arab Rep. Asylum-seekers 7085
2016 Brazil Haiti Asylum-seekers 6909
2016 Kenya Burundi Asylum-seekers 6744
2016 Belgium Afghanistan Asylum-seekers 6457
2016 Kenya Sudan Asylum-seekers 6443
2016 Germany Ukraine Asylum-seekers 6346
2016 Italy Gambia Asylum-seekers 6302
2016 Germany Azerbaijan Asylum-seekers 6242
2016 Germany The former Yugoslav Republic of Macedonia Asylum-seekers 6057
2016 United States of America Ukraine Asylum-seekers 5985
2016 South Africa Dem. Rep. of the Congo Asylum-seekers 5967
2016 Angola Côte d'Ivoire Asylum-seekers 5938
2016 United States of America Nepal Asylum-seekers 5820
2016 Australia Malaysia Asylum-seekers 5793
2016 Iraq Iran (Islamic Rep. of) Asylum-seekers 5776
2016 Greece Various/Unknown Asylum-seekers 5767
2016 Italy Bangladesh Asylum-seekers 5695
2016 Italy Senegal Asylum-seekers 5683
2016 Egypt Eritrea Asylum-seekers 5601
2016 Spain Ukraine Asylum-seekers 5555
2016 Jordan Yemen Asylum-seekers 5524
2016 United States of America Russian Federation Asylum-seekers 5511
2016 Italy Côte d'Ivoire Asylum-seekers 5497
2016 Angola Mauritania Asylum-seekers 5403
2016 Algeria Syrian Arab Rep. Asylum-seekers 5329
2016 Germany Georgia Asylum-seekers 5298
2016 Brazil Senegal Asylum-seekers 5281
2016 Mozambique Dem. Rep. of the Congo Asylum-seekers 5095
2016 France Albania Asylum-seekers 5032
2016 Germany Guinea Asylum-seekers 5031
2016 Italy Mali Asylum-seekers 5020
2016 France Sudan Asylum-seekers 5002
2016 Malawi Burundi Asylum-seekers 4738
2016 United States of America Bangladesh Asylum-seekers 4650
2016 Mozambique Burundi Asylum-seekers 4543
2016 Iraq Turkey Asylum-seekers 4528
2016 Austria Iran (Islamic Rep. of) Asylum-seekers 4490
2016 France Haiti Asylum-seekers 4484
2016 Germany Lebanon Asylum-seekers 4452
2016 Pakistan Afghanistan Asylum-seekers 4447
2016 Spain Venezuela (Bolivarian Republic of) Asylum-seekers 4435
2016 United States of America Ethiopia Asylum-seekers 4411
2016 Italy Guinea Asylum-seekers 4380
2016 Congo Central African Rep. Asylum-seekers 4322
2016 Brazil Venezuela (Bolivarian Republic of) Asylum-seekers 4302
2016 Belgium Various/Unknown Asylum-seekers 4295
2016 India Afghanistan Asylum-seekers 4255
2016 Greece Afghanistan Asylum-seekers 4217
2016 Libya Eritrea Asylum-seekers 4200
2016 France Afghanistan Asylum-seekers 4180
2016 Japan Nepal Asylum-seekers 4144
2016 Bulgaria Iraq Asylum-seekers 4092
2016 Greece Pakistan Asylum-seekers 4067
2016 United States of America Pakistan Asylum-seekers 4033
2016 United States of America Colombia Asylum-seekers 4002
2016 Sudan Eritrea Asylum-seekers 4000
2016 Sweden Iran (Islamic Rep. of) Asylum-seekers 3958
2016 Sweden Somalia Asylum-seekers 3890
2016 Switzerland Syrian Arab Rep. Asylum-seekers 3858
2016 Italy Ghana Asylum-seekers 3817
2016 South Africa Ethiopia Asylum-seekers 3784
2016 Germany Stateless Asylum-seekers 3765
2016 United States of America Egypt Asylum-seekers 3710
2016 India Myanmar Asylum-seekers 3677
2016 Egypt Somalia Asylum-seekers 3643
2016 United States of America Iraq Asylum-seekers 3631
2016 Peru Venezuela (Bolivarian Republic of) Asylum-seekers 3553
2016 Malawi Mozambique Asylum-seekers 3496
2016 United States of America Brazil Asylum-seekers 3478
2016 Germany India Asylum-seekers 3449
2016 South Africa Nigeria Asylum-seekers 3446
2016 Uganda Burundi Asylum-seekers 3423
2016 South Africa Malawi Asylum-seekers 3396
2016 United States of America Syrian Arab Rep. Asylum-seekers 3364
2016 Germany Sudan Asylum-seekers 3308
2016 Germany Bangladesh Asylum-seekers 3275
2016 Germany Ghana Asylum-seekers 3267
2016 Germany Egypt Asylum-seekers 3231
2016 Austria Somalia Asylum-seekers 3152
2016 Greece Iraq Asylum-seekers 3135
2016 Egypt Iraq Asylum-seekers 3130
2016 Egypt South Sudan Asylum-seekers 3109
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment