|
<!DOCTYPE html> |
|
<meta charset="utf-8"> |
|
<style> |
|
|
|
svg { |
|
font: 12px sans-serif; |
|
font-weight: bold; |
|
shape-rendering: crispEdges; |
|
} |
|
|
|
.y.axis path { |
|
display: none; |
|
} |
|
|
|
.y.axis line { |
|
stroke: #fff; |
|
stroke-opacity: .2; |
|
} |
|
|
|
.y.axis .zero line { |
|
stroke: #000; |
|
stroke-opacity: 1; |
|
} |
|
|
|
.title { |
|
font: 300 78px sans-serif; |
|
fill: #666; |
|
} |
|
|
|
.subtitle { |
|
fill: #9E9E9E; |
|
text-anchor: right; |
|
} |
|
|
|
.pay { |
|
text-anchor: middle; |
|
} |
|
|
|
rect { |
|
fill-opacity: .8; |
|
fill: #00b300; |
|
} |
|
|
|
</style> |
|
<body> |
|
<script src="http://d3js.org/d3.v3.min.js"></script> |
|
<script> |
|
|
|
var margin = {top: 20, right: 40, bottom: 30, left: 20}, |
|
width = 960 - margin.left - margin.right, |
|
height = 500 - margin.top - margin.bottom, |
|
barWidth = Math.floor(width/300); |
|
|
|
var x = d3.scale.linear() |
|
.range([barWidth, width - barWidth]); |
|
|
|
var y = d3.scale.linear() |
|
.range([height, 0]); |
|
|
|
var yAxis = d3.svg.axis() |
|
.scale(y) |
|
.orient("left") |
|
.tickSize(-width); |
|
|
|
var nmw = {"1999": 3.6, "2000": 3.7, "2001": 4.1, "2002": 4.2, "2003": 4.5, "2004": 4.85, "2005": 5.05, "2006": 5.35, "2007": 5.52, "2008": 5.73, "2009": 5.8, "2010": 5.93, "2011": 6.08, "2012": 6.19, "2013": 6.31, "2014": 6.5, "2015": 6.7, "2016": 7.2}; |
|
|
|
console.log(nmw); |
|
|
|
// An SVG element with a bottom-right origin. |
|
var svg = d3.select("body").append("svg") |
|
.attr("width", width + margin.left + margin.right) |
|
.attr("height", height + margin.top + margin.bottom) |
|
.append("g") |
|
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); |
|
|
|
// A sliding container to hold the bars by bracket. |
|
var brackets = svg.append("g") |
|
.attr("class", "brackets"); |
|
|
|
// A label for the starting year. |
|
var title = svg.append("text") |
|
.attr("class", "title") |
|
.attr("dy", ".71em") |
|
.attr("x", width - 200) |
|
.text(1999); |
|
|
|
d3.csv("earnings.csv", function(error, data) { |
|
console.log(data); |
|
|
|
// Convert strings to numbers. |
|
data.forEach(function(d) { |
|
d.density = +d.density; |
|
d.year = +d.year; |
|
d.pay = +d.pay; |
|
}); |
|
|
|
// Compute the extent of the data set in pay and years. |
|
var pay1 = d3.max(data, function(d) { return d.pay; }), |
|
year0 = d3.min(data, function(d) { return d.year; }), |
|
year1 = d3.max(data, function(d) { return d.year; }), |
|
year = year0; |
|
|
|
// Update the scale domains. |
|
x.domain([year0, year0 - pay1]); |
|
y.domain([0, d3.max(data, function(d) { return d.density; })]); |
|
|
|
// Produce a map from year and pay. |
|
data = d3.nest() |
|
.key(function(d) { return d.year; }) |
|
.key(function(d) { return d.year - d.pay; }) |
|
.rollup(function(v) { return v.map(function(d) { return d.density; }); }) |
|
.map(data); |
|
|
|
// Add an axis to show the population values. |
|
svg.append("g") |
|
.attr("class", "y axis") |
|
.call(yAxis) |
|
.selectAll("g") |
|
.filter(function(value) { return !value; }) |
|
.classed("zero", true); |
|
|
|
// Add labeled rects for each pay bracket (so that no enter or exit is required). |
|
var bracket = brackets.selectAll(".bracket") |
|
.data(d3.range(year0 - pay1, year1 + 1, 0.1)) |
|
.enter().append("g") |
|
.attr("class", "bracket") |
|
.attr("transform", function(bracket) { return "translate(" + x(bracket) + ",0)"; }); |
|
|
|
bracket.selectAll("rect") |
|
.data(function(bracket) { return data[year][bracket] || [0, 0]; }) |
|
.enter().append("rect") |
|
.attr("x", -barWidth / 2) |
|
.attr("width", barWidth) |
|
.attr("y", y) |
|
.attr("height", function(value) { return height - y(value); }); |
|
|
|
// Add labels to show pay (separate; not animated). |
|
svg.selectAll(".pay") |
|
.data(d3.range(3, pay1 + 0.1, 1)) |
|
.enter().append("text") |
|
.attr("class", "pay") |
|
.attr("x", function(pay) { return x(year - pay); }) |
|
.attr("y", height + 4) |
|
.attr("dy", ".71em") |
|
.text(function(pay) { return ('£' + pay);}); |
|
|
|
// A label for the minimum wage in the displayed year |
|
var subtitle = svg.append("text") |
|
.attr("class", "subtitle") |
|
.attr("dy", ".50em") |
|
.attr("x", width - 200) |
|
.attr("y", 78) |
|
.text("Min Wage: £" + nmw[year].toFixed(2)); |
|
|
|
// Allow the arrow keys to change the displayed year. |
|
window.focus(); |
|
d3.select(window).on("keydown", function() { |
|
switch (d3.event.keyCode) { |
|
case 37: year = Math.max(year0, year - 1); break; |
|
case 39: year = Math.min(year1, year + 1); break; |
|
} |
|
update(); |
|
}); |
|
|
|
function update() { |
|
if (!(year in data)) return; |
|
title.text(year); |
|
|
|
subtitle.text("Min Wage: £" + nmw[year].toFixed(2)); |
|
|
|
brackets.transition() |
|
.duration(750) |
|
.attr("transform", "translate(" + (x(year0) - x(year)) + ",0)"); |
|
|
|
bracket.selectAll("rect") |
|
.data(function(bracket) { return data[year][bracket] || [0, 0]; }) |
|
.transition() |
|
.duration(750) |
|
.attr("y", y) |
|
.attr("height", function(value) { return height - y(value); }); |
|
|
|
} |
|
}); |
|
|
|
</script> |