Created
November 26, 2015 16:54
-
-
Save ryestew/e1c90a1062597322a8f7 to your computer and use it in GitHub Desktop.
d3_2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
continent | 1998 | 2007 | 2008 | 2009 | 2010 | 2011 | 2012 | 2013 | |
---|---|---|---|---|---|---|---|---|---|
Africa | 75396 | 103407 | 108178 | 109361 | 105683 | 101098 | 102592 | 109665 | |
Asia | 29725 | 43324 | 44723 | 48065 | 44404 | 43134 | 44047 | 48770 | |
America | 13518 | 19239 | 19922 | 20285 | 23314 | 21096 | 20633 | 20734 | |
Europe | 4318 | 10528 | 10395 | 10643 | 10194 | 11254 | 11471 | 11957 | |
Oceania | 473 | 703 | 826 | 838 | 687 | 821 | 805 | 716 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Stacked area chart</title> | |
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script> | |
<style type="text/css"> | |
body { | |
margin: 0; | |
background-color: lightGray; | |
font-family: Helvetica, Arial, sans-serif; | |
} | |
#container { | |
width: 800px; | |
margin-left: auto; | |
margin-right: auto; | |
margin-top: 50px; | |
padding: 50px; | |
background-color: white; | |
box-shadow: 3px 3px 5px 6px #ccc; | |
} | |
h1 { | |
font-size: 24px; | |
margin: 0; | |
} | |
p { | |
font-size: 14px; | |
margin: 10px 0 0 0; | |
} | |
g.bar { | |
cursor: pointer; | |
} | |
g.bar text { | |
font-size: 11px; | |
font-weight: bold; | |
text-anchor: end; | |
opacity: 0; | |
} | |
g.bar:hover rect { | |
fill: #f7e12c; | |
} | |
g.bar:hover text { | |
opacity: 1; | |
} | |
svg { | |
background-color: white; | |
} | |
path:hover { | |
fill: red; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: black; | |
shape-rendering: crispEdges; | |
} | |
.axis text { | |
font-family: sans-serif; | |
font-size: 11px; | |
} | |
#tooltip { | |
visibility: hidden; | |
position: fixed; | |
background-color: #ffffff; | |
padding: 5px; | |
border-left: 3px #9fccd5 solid; | |
} | |
#tooltip p { | |
font-size: 10px; | |
margin: 0; | |
padding: 0; | |
} | |
#tooltip strong { | |
color: #888; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="container"> | |
<h1>Immigration flows to France by continent of origin</h1> | |
<p>First residence permits issued for one year or more, by continent of origin. Source: <a href="https://www.ined.fr/en/everything_about_population/data/france/immigration-flow/year-continent/">Institute National d'Étude Démographique</a>, 2014</p> | |
</div> | |
<script type="text/javascript"> | |
//Set up stack method | |
var stack = d3.layout.stack() | |
.values(function(d) { | |
return d.immigrants; | |
}) | |
.order("reverse"); | |
//Width, height, padding | |
var w = 700; | |
var h = 600; | |
var padding = [ 20, 10, 50, 100 ]; //Top, right, bottom, left | |
//Set up date format function (years) | |
var dateFormat = d3.time.format("%Y"); | |
//Set up scales | |
var xScale = d3.time.scale() | |
.range([ padding[3], w - padding[1] - padding[3] ]); | |
var yScale = d3.scale.linear() | |
.range([ padding[0], h - padding[2] ]); | |
//Configure axis generators | |
var xAxis = d3.svg.axis() | |
.scale(xScale) | |
.orient("bottom") | |
.ticks(8) | |
.tickFormat(function(d) { | |
return dateFormat(d); | |
}); | |
var yAxis = d3.svg.axis() | |
.scale(yScale) | |
.orient("left") | |
.ticks(5); | |
//Configure area generator | |
var area = d3.svg.area() | |
.x(function(d) { | |
return xScale(dateFormat.parse(d.x)); | |
}) | |
.y0(function(d) { | |
return yScale(d.y0); //Updated | |
}) | |
.y1(function(d) { | |
return yScale(d.y0 + d.y); //Updated | |
}); | |
var tooltip = document.getElementById("tooltip"); | |
//Create the empty SVG image | |
var svg = d3.select("#container") | |
.append("svg") | |
.attr("width", w) | |
.attr("height", h); | |
//Easy colors accessible via a 10-step ordinal scale | |
// var color = d3.scale.category10(); | |
//nahhh lets go with a custom set | |
var color = d3.scale.ordinal() | |
.range(["#c4cefc", "#8e93fa", "#71a1e2", "#469bc7", "#09839d"]); | |
//Load data | |
d3.csv("en_flux_immigr_continent-absolute.csv", function(data) { | |
//Uncomment to log the newly loaded data to the console | |
//console.log(data); | |
//Data is loaded in, but we need to restructure it. | |
//Remember, each line requires an array of x/y pairs; | |
//that is, an array of arrays, like so: | |
// | |
// [ [x: 1, y: 1], [x: 2, y: 2], [x: 3, y: 3] ] | |
// | |
//Our x value will be the year, and y will be the amount | |
//of CO2. We also need to know which country belongs to | |
//each line, so we will build an array of objects that is | |
//structured like this: | |
// | |
/* | |
[ | |
{ | |
country: "Australia", | |
emissions: [ | |
{ x: "1961", y: 90589.568 }, | |
{ x: "1962", y: 94912.961 }, | |
{ x: "1963", y: 101029.517 }, | |
… | |
] | |
}, | |
{ | |
country: "Bermuda", | |
emissions: [ | |
{ x: "1961", y: 176.016 }, | |
{ x: "1962", y: 157.681 }, | |
{ x: "1963", y: 150.347 }, | |
… | |
] | |
}, | |
… | |
] | |
*/ | |
// | |
//Note that this is an array of objects. Each object | |
//contains two values, 'country' and 'emissions'. | |
//The 'emissions' value is itself an array, containing | |
//more objects, each one holding x and y values. | |
// | |
//The x (year) values have to be strings in this case, | |
//because the date format function expects a string | |
//to parse into a Date object. | |
//New array with all the years, for referencing later | |
var years = ["1998", "2007", "2008", "2009", "2010", "2011", "2012", "2013"]; | |
//Create a new, empty array to hold our restructured dataset | |
var dataset = []; | |
// so the number of rows is the length of dataset ( not including the top row) | |
//console.log(data.length); | |
//Loop once for each row in data | |
for (var i = 0; i < data.length; i++) { | |
//Create new object with this country's name and empty array | |
dataset[i] = { | |
country: data[i].continent, | |
immigrants: [] | |
}; | |
//Loop through all the years | |
for (var j = 0; j < years.length; j++) { | |
//Default value, used in case no value is present | |
var amount = null; | |
// If value is not empty | |
if (data[i][years[j]]) { | |
// console.log('yo'); | |
//+ plus is to force it to number context ( int context) | |
amount = +data[i][years[j]]; | |
//console.log(amount); | |
} | |
//Add a new object to the emissions data array | |
//for this country | |
dataset[i].immigrants.push({ | |
x: years[j], | |
y: amount | |
}); | |
} | |
} | |
//Stack the data! | |
stack(dataset); | |
//Uncomment to log the original data to the console | |
//console.log(data); | |
//Uncomment to log the newly restructured dataset to the console | |
//console.log(dataset); | |
//Now that the data is ready, we can check its | |
//min and max values to set our scales' domains! | |
xScale.domain([ | |
d3.min(years, function(d) { | |
return dateFormat.parse(d); | |
}), | |
d3.max(years, function(d) { | |
return dateFormat.parse(d); | |
}) | |
]); | |
//Need to recalcluate the max value for yScale | |
//differently, now that everything is stacked. | |
//Loop once for each year, and get the total value | |
//of CO2 for that year. | |
var totals = []; | |
for (i = 0; i < years.length; i++) { | |
totals[i] = 0; | |
for (j = 0; j < dataset.length; j++) { | |
totals[i] += dataset[j].immigrants[i].y; | |
} | |
} | |
yScale.domain([ d3.max(totals), 0 ]); | |
//Areas | |
// | |
//Now that we are creating multiple paths, we can use the | |
//selectAll/data/enter/append pattern to generate as many | |
//as needed. | |
//Make a path for each country | |
var paths = svg.selectAll("path") | |
.data(dataset) | |
.enter() | |
.append("path") | |
.attr("class", "area") | |
.attr("d", function(d) { | |
//Calculate path based on only d.immigrants array, | |
//not all of d (which would include the country name) | |
return area(d.immigrants); | |
}) | |
.attr("stroke", "none") | |
.attr("fill", function(d, i) { | |
return color(i); | |
}); | |
//Append a title with the country name (so we get easy tooltips) | |
paths.append("title") | |
.text(function(d) { | |
return d.country; | |
}); | |
//Create axes | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + (h - padding[2]) + ")") | |
.call(xAxis); | |
svg.append("g") | |
.attr("class", "y axis") | |
.attr("transform", "translate(" + padding[3] + ",0)") | |
.call(yAxis); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment