Skip to content

Instantly share code, notes, and snippets.

@MariellaCC
Created November 12, 2015 22:19
Show Gist options
  • Save MariellaCC/52d4fc224caf18130215 to your computer and use it in GitHub Desktop.
Save MariellaCC/52d4fc224caf18130215 to your computer and use it in GitHub Desktop.
module 2
Country 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
Belgium 16.21 16.21 16.21 16.21 16.21 16.21 16.21 16.21 16.21 18.19 18.19 18.19 18.19 18.19 18.19 18.19 18.19 18.19 18.19 19.87 19.87 19.87 19.87 19.87 19.87 19.87 19.87 19.87 19.87
Bulgaria 4.89 4.89 4.89 4.89 4.89 4.89 4.89 4.89 4.89 4.76 4.76 4.76 4.76 4.76 4.76 4.76 4.76 4.76 4.76 4.56 4.56 4.56 4.56 4.56 4.56 4.56 4.56 4.56 4.56
Czech Republic 8.07 8.07 8.07 8.07 8.07 8.07 8.07 8.07 8.07 7.86 7.86 7.86 7.86 7.86 7.86 7.86 7.86 7.86 7.86 7.38 7.38 7.38 7.38 7.38 7.38 7.38 7.38 7.38 7.38
Denmark 19.14 19.14 19.14 19.14 19.14 19.14 19.14 19.14 19.14 20.94 20.94 20.94 20.94 20.94 20.94 20.94 20.94 20.94 20.94 20.74 20.74 20.74 20.74 20.74 20.74 20.74 20.74 20.74 20.74
Germany 16.55 16.55 16.55 16.55 16.55 16.55 16.55 16.55 16.55 18.17 18.17 18.17 18.17 18.17 18.17 18.17 18.17 18.17 18.17 18.39 18.39 18.39 18.39 18.39 18.39 18.39 18.39 18.39 18.39
Estonia 8.22 8.22 8.22 8.22 8.22 8.22 8.22 8.22 8.22 7.41 7.41 7.41 7.41 7.41 7.41 7.41 7.41 7.41 7.41 6.43 6.43 6.43 6.43 6.43 6.43 6.43 6.43 6.43 6.43
Ireland 19.17 19.17 19.17 19.17 19.17 19.17 19.17 19.17 19.17 22.82 22.82 22.82 22.82 22.82 22.82 22.82 22.82 22.82 22.82 23.33 23.33 23.33 23.33 23.33 23.33 23.33 23.33 23.33 23.33
Greece 10.49 10.49 10.49 10.49 10.49 10.49 10.49 10.49 10.49 13.03 13.03 13.03 13.03 13.03 13.03 13.03 13.03 13.03 13.03 15.29 15.29 15.29 15.29 15.29 15.29 15.29 15.29 15.29 15.29
Spain 11.67 11.67 11.67 11.67 11.67 11.67 11.67 11.67 11.67 12.76 12.76 12.76 12.76 12.76 12.76 12.76 12.76 12.76 12.76 14.03 14.03 14.03 14.03 14.03 14.03 14.03 14.03 14.03 14.03
France 13.87 13.87 13.87 13.87 13.87 13.87 13.87 13.87 13.87 15.31 15.31 15.31 15.31 15.31 15.31 15.31 15.31 15.31 15.31 16.17 16.17 16.17 16.17 16.17 16.17 16.17 16.17 16.17 16.17
Croatia 8.44 8.44 8.44 8.44 8.44 8.44 8.44 8.44 8.44 8.34 8.34 8.34 8.34 8.34 8.34 8.34 8.34 8.34 8.34 9.05 9.05 9.05 9.05 9.05 9.05 9.05 9.05 9.05 9.05
Italy 12.32 12.32 12.32 12.32 12.32 12.32 12.32 12.32 12.32 14.5 14.5 14.5 14.5 14.5 14.5 14.5 14.5 14.5 14.5 17.03 17.03 17.03 17.03 17.03 17.03 17.03 17.03 17.03 17.03
Cyprus 13.44 13.44 13.44 13.44 13.44 13.44 13.44 13.44 13.44 15.47 15.47 15.47 15.47 15.47 15.47 15.47 15.47 15.47 15.47 17.26 17.26 17.26 17.26 17.26 17.26 17.26 17.26 17.26 17.26
Latvia 4.72 4.72 4.72 4.72 4.72 4.72 4.72 4.72 4.72 4.12 4.12 4.12 4.12 4.12 4.12 4.12 4.12 4.12 4.12 3.88 3.88 3.88 3.88 3.88 3.88 3.88 3.88 3.88 3.88
Lithuania 6.15 6.15 6.15 6.15 6.15 6.15 6.15 6.15 6.15 5.75 5.75 5.75 5.75 5.75 5.75 5.75 5.75 5.75 5.75 5.71 5.71 5.71 5.71 5.71 5.71 5.71 5.71 5.71 5.71
Luxembourg 17.82 17.82 17.82 17.82 17.82 17.82 17.82 17.82 17.82 20.27 20.27 20.27 20.27 20.27 20.27 20.27 20.27 20.27 20.27 21.2 21.2 21.2 21.2 21.2 21.2 21.2 21.2 21.2 21.2
Hungary 7.95 7.95 7.95 7.95 7.95 7.95 7.95 7.95 7.95 7.6 7.6 7.6 7.6 7.6 7.6 7.6 7.6 7.6 7.6 7.38 7.38 7.38 7.38 7.38 7.38 7.38 7.38 7.38 7.38
Malta 12.74 12.74 12.74 12.74 12.74 12.74 12.74 12.74 12.74 12.33 12.33 12.33 12.33 12.33 12.33 12.33 12.33 12.33 12.33 11.81 11.81 11.81 11.81 11.81 11.81 11.81 11.81 11.81 11.81
Netherlands 16.39 16.39 16.39 16.39 16.39 16.39 16.39 16.39 16.39 17.82 17.82 17.82 17.82 17.82 17.82 17.82 17.82 17.82 17.82 18.27 18.27 18.27 18.27 18.27 18.27 18.27 18.27 18.27 18.27
Austria 13.71 13.71 13.71 13.71 13.71 13.71 13.71 13.71 13.71 15.05 15.05 15.05 15.05 15.05 15.05 15.05 15.05 15.05 15.05 15.71 15.71 15.71 15.71 15.71 15.71 15.71 15.71 15.71 15.71
Poland 9.32 9.32 9.32 9.32 9.32 9.32 9.32 9.32 9.32 9.45 9.45 9.45 9.45 9.45 9.45 9.45 9.45 9.45 9.45 8.62 8.62 8.62 8.62 8.62 8.62 8.62 8.62 8.62 8.62
Portugal 8.73 8.73 8.73 8.73 8.73 8.73 8.73 8.73 8.73 10.34 10.34 10.34 10.34 10.34 10.34 10.34 10.34 10.34 10.34 11.82 11.82 11.82 11.82 11.82 11.82 11.82 11.82 11.82 11.82
Romania 5.31 5.31 5.31 5.31 5.31 5.31 5.31 5.31 5.31 5.32 5.32 5.32 5.32 5.32 5.32 5.32 5.32 5.32 5.32 5.69 5.69 5.69 5.69 5.69 5.69 5.69 5.69 5.69 5.69
Slovenia 10.69 10.69 10.69 10.69 10.69 10.69 10.69 10.69 10.69 11.18 11.18 11.18 11.18 11.18 11.18 11.18 11.18 11.18 11.18 11.74 11.74 11.74 11.74 11.74 11.74 11.74 11.74 11.74 11.74
Slovakia 7.67 7.67 7.67 7.67 7.67 7.67 7.67 7.67 7.67 7.17 7.17 7.17 7.17 7.17 7.17 7.17 7.17 7.17 7.17 6.89 6.89 6.89 6.89 6.89 6.89 6.89 6.89 6.89 6.89
Finland 15.35 15.35 15.35 15.35 15.35 15.35 15.35 15.35 15.35 16.2 16.2 16.2 16.2 16.2 16.2 16.2 16.2 16.2 16.2 15.81 15.81 15.81 15.81 15.81 15.81 15.81 15.81 15.81 15.81
Sweden 14.41 14.41 14.41 14.41 14.41 14.41 14.41 14.41 14.41 15.47 15.47 15.47 15.47 15.47 15.47 15.47 15.47 15.47 15.47 15.15 15.15 15.15 15.15 15.15 15.15 15.15 15.15 15.15 15.15
United Kingdom 17.73 17.73 17.73 17.73 17.73 17.73 17.73 17.73 17.73 18.31 18.31 18.31 18.31 18.31 18.31 18.31 18.31 18.31 18.31 17.67 17.67 17.67 17.67 17.67 17.67 17.67 17.67 17.67 17.67
Iceland 12.13 12.13 12.13 12.13 12.13 12.13 12.13 12.13 12.13 12.71 12.71 12.71 12.71 12.71 12.71 12.71 12.71 12.71 12.71 12.34 12.34 12.34 12.34 12.34 12.34 12.34 12.34 12.34 12.34
Norway 19.27 19.27 19.27 19.27 19.27 19.27 19.27 19.27 19.27 20.91 20.91 20.91 20.91 20.91 20.91 20.91 20.91 20.91 20.91 20.79 20.79 20.79 20.79 20.79 20.79 20.79 20.79 20.79 20.79
Switzerland 18.98 18.98 18.98 18.98 18.98 18.98 18.98 18.98 18.98 20.91 20.91 20.91 20.91 20.91 20.91 20.91 20.91 20.91 20.91 21.33 21.33 21.33 21.33 21.33 21.33 21.33 21.33 21.33 21.33
Former Yugoslav Republic of Macedonia 7.33 7.33 7.33 7.33 7.33 7.33 7.33 7.33 7.33 7.2 7.2 7.2 7.2 7.2 7.2 7.2 7.2 7.2 7.2 7.63 7.63 7.63 7.63 7.63 7.63 7.63 7.63 7.63 7.63
Turkey 7.17 7.17 7.17 7.17 7.17 7.17 7.17 7.17 7.17 8.4 8.4 8.4 8.4 8.4 8.4 8.4 8.4 8.4 8.4 12.13 12.13 12.13 12.13 12.13 12.13 12.13 12.13 12.13 12.13
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Stacked area chart</title>
<script type="text/javascript" src="//d3js.org/d3.v3.min.js"></script>
<style type="text/css">
body {
background-color: white;
font-family: Helvetica, Arial, sans-serif;
}
h1 {
font-size: 24px;
margin: 0;
}
p {
font-size: 14px;
margin: 10px 0 0 0;
}
svg {
background-color: white;
}
path:hover {
fill: yellow;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
.area {
opacity: 0.7;
}
</style>
</head>
<body>
<h1>Hourly earnings by country and by age category</h1>
<p>Mean earnings in Purchasing Power Standard (PPS), 2010. Source: <a href="http://ec.europa.eu/eurostat/web/equality/data/database">Eurostat</a>, 2015</p>
<script type="text/javascript">
var stack = d3.layout.stack()
.values(function(d) {
return d.earnings;
})
//.order("reverse");
var w = 700;
var h = 600;
var padding = [ 20, 10, 50, 100 ];
var xScale = d3.scale.linear()
.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(4);
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(5);
//Configure area generator
var area = d3.svg.area()
.x(function(d) {
return xScale(d.x);
})
.y0(function(d) {
return yScale(d.y0);
})
.y1(function(d) {
return yScale(d.y0 + d.y);
});
//Easy colors accessible via a 10-step ordinal scale
var color = d3.scale.category20();
//Create the empty SVG image
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
//Load data
d3.csv("earnings_by_age.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 = ["31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59"];
//Create a new, empty array to hold our restructured dataset
var dataset = [];
//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].Country,
earnings: []
};
//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]]) {
amount = +data[i][years[j]];
}
//Add a new object to the emissions data array
//for this country
dataset[i].earnings.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);
xScale.domain([ 31, 59 ]);
//Need to recalcluate the max value for yScale
//differently, now that everything is stacked.
//Loop once for each year, and get the total
//of earnings.
var totals = [];
for (i = 0; i < years.length; i++) {
totals[i] = 0;
for (j = 0; j < dataset.length; j++) {
totals[i] += dataset[j].earnings[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.emissions array,
//not all of d (which would include the country name)
return area(d.earnings);
})
.attr("stroke", "#000000")
.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