Skip to content

Instantly share code, notes, and snippets.

@bniedzie
Last active June 10, 2019 03:20
Show Gist options
  • Save bniedzie/6e13f7725edb521d43a7fff324a3c479 to your computer and use it in GitHub Desktop.
Save bniedzie/6e13f7725edb521d43a7fff324a3c479 to your computer and use it in GitHub Desktop.
Women by Birth Date
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.bar rect {
shape-rendering: crispEdges;
}
.bar text {
fill: #999999;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
// Set the base bar color
var color = "pink";
// A comma-separated list of the years, using negative numbers for BCE
var years = [-525,-500,-500,-475,-350,-300,-275,-250,-250,-160,-140,-140,-150,-100,-100,-100,-100,-100,-100,-100,-39,-209,-225,-300,-150,-150,-150,-125,-100,-150,-73,-100,-150,-125,-150,-175,-75,-75,-125,-150,-125,-50,-125,-100,-75,-100,-100,-75,-125,-100,-175,-125,-100,-50,-50,-50,-175,-150,-87,-150,-150,-100,-79,-175,-150,-125,-150,-200,-125,-325,-325,-200,-225,-225,-175,-150,-175,-150,-75,-100,-150,-100,-100,-75,-125,-100,-175,-100,-175,-150,-100,-132,-125,-100,-150,-125,-75,-175,-125,-150,-125,-170,-150,-106,-125,-125,-125,-163,-125,-75,-125,-125,-83,-200,-200,-150,-225,-175,-125,-150,-150,-150,-250,-125,-150,-125,-200,-150,-100,-175,-150,-75,-150,-100,-100,-150,-75,-100,-100,-125,-150,-150,-125,-225,-125,-75,-200,-100,-125,-125,-200,-125,-100,-125,-125,-125,-75,-100,-125,-100,-150,-75,-50,-75,-125,-39,-50,-50,-75,-150,-75,-125,-58,-125,-250,-175,-325,-150,-175,-250,-250,-275,-225,-225,-75,-100,-110,-175,-100,-275,-150,-125,-175,-275,-250,-100,-75,-100,-125,-50,-75,-75,-150,-100,-125,-100,-100,-150,-125,-200,-175,-100,-125,-175,-125,-78,-138,-150,-138,-225,-100,-125,-79,-75,-175,-100,-150,-125,-100,-75,-150,-100,-100,-50,-50,-100,-125,-50,-75,-75,-75,-50,-375,-100,-75,-150,-125,-75,-100,-75,-150,-125,-75,-219,-150,-275,-125,-125,-200,-325,-100,-100,-25,-150,-100,-69,-100,-50,-50,-75,-100,-50,-50,-100,-125,-125,-125,-150,-125,-175,-75,-75,-75,-125,-50,-75,-100,-25,-75,-25,-150,-225,-400,-400,-175,-50,-89,-275,-100,-219,-75,-75,-150,-175,-75,-100,-100,-150,-75,-50,-50,-50,-150,-50,-75,-100,-100,-550,-525,-475,-350,-575,-175,-125,-125,-100,-125,-125,-125,-125,-125,-100,-125,-125,-125,-125,-125,-125,-200,-200,-125];
// A formatter for counts.
var formatCount = d3.format(",.0f");
// Sets the margins
var margin = {top: 40, right: 30, bottom: 60, left: 30},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// Round the minimum year down to the nearest 25 and the maximum year up to the nearest 25
var max = Math.ceil(d3.max(years) / 25) * 25;
var min = Math.floor(d3.min(years) / 25) * 25;
var x = d3.scale.linear()
.domain([min, max])
.range([0, width]);
// Generate a histogram using bins of length 25 years.
var data = d3.layout.histogram()
.bins(d3.range(min, max, 25))
(years);
// Set the bar coloring
var yMax = d3.max(data, function(d){return d.length});
var yMin = d3.min(data, function(d){return d.length});
var colorScale = d3.scale.linear()
.domain([yMin, yMax])
.range([d3.rgb(color).brighter(), d3.rgb(color).darker()]);
var y = d3.scale.linear()
.domain([0, yMax])
.range([height, 0]);
// Ensure the axis uses 25 year increments
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickValues(d3.range(min, max, 25));
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 + ")");
var bar = svg.selectAll(".bar")
.data(data)
.enter().append("g")
.attr("class", "bar")
.attr("transform", function(d) { return "translate(" + x(d.x) + "," + y(d.y) + ")"; });
bar.append("rect")
.attr("x", 1)
.attr("width", (x(data[0].dx) - x(0)) - 1)
.attr("height", function(d) { return height - y(d.y); })
.attr("fill", function(d) { return colorScale(d.y) });
bar.append("text")
.attr("dy", ".75em")
.attr("y", -12)
.attr("x", (x(data[0].dx) - x(0)) / 2)
.attr("text-anchor", "middle")
.text(function(d) { return formatCount(d.y); });
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Graph title
svg.append("text")
.attr("x", (width / 2))
.attr("y", 0 - (margin.top / 2))
.attr("text-anchor", "middle")
.style("font-size", "16px")
.text("Women in Database per 25 Years (by Birth Year)");
// Axis label
svg.append("text")
.attr("class", "x label")
.attr("text-anchor", "middle")
.attr("x", width / 2)
.attr("y", height + 50)
.text("Year");
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment