Skip to content

Instantly share code, notes, and snippets.

@enjalot
Last active November 11, 2015 03:53
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 enjalot/d6a92a077e78fd33316a to your computer and use it in GitHub Desktop.
Save enjalot/d6a92a077e78fd33316a to your computer and use it in GitHub Desktop.
The Migrant Files: Money
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
pre { width: 100%; height: 100%; font-size: 10px; overflow-y: scroll; }
</style>
</head>
<body>
<pre id="json">
</pre>
<script>
function toNumber(s) {
// convert strings with spaces instead of commas into numbers
var n = s.replace(/[w]/g, "") //remove any whitespace
.replace(/ /g, "") //utf8 space?
return +n; //convert to number
}
function process(d) {
var processed = {
start: d.Start,
end: d.End,
year: +d.Year,
// we just deal with the price in Euros adjusted for inflation
price: toNumber(d["Price EUR 2014"]),
service: d.Service,
route: d.route,
author: d.author,
comment: d.Comment,
source: d.Source,
}
return processed;
}
d3.csv("http://enjalot.github.io/migrants/money.csv", function(err, rawdata) {
console.log("rawdata", rawdata);
var data = rawdata.map(process);
console.log("data", data);
d3.select("#json").text(JSON.stringify(data, null, 2));
// I want to see how many distinct routes there are
var routes = d3.nest()
.key(function(d) { return d.route })
.rollup(function(leaves) { return leaves.length })
.entries(data);
console.log("routes");
console.table(routes.sort(function(a,b) { return b.values - a.values }));
var ends = d3.nest()
.key(function(d) { return d.end })
.rollup(function(leaves) { return leaves.length })
.entries(data);
console.log("destinations")
console.table(ends.sort(function(a,b) { return b.values - a.values }));
var starts = d3.nest()
.key(function(d) { return d.start })
.rollup(function(leaves) { return leaves.length })
.entries(data);
console.log("origins")
console.table(starts.sort(function(a,b) { return b.values - a.values }));
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment