Data from the Sunlight Labs Congress API - get an API key and play around with it!
xxxxxxxxxx
<meta charset="utf-8">
<style>
rect.bar {
fill: steelblue;
}
.axis text {
font: 10px sans-serif;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script>
var format = d3.time.format("%Y-%m-%d");
var yformat = d3.time.format("%Y");
d3.json('legislators.json')
.get()
.on('load', function(data) {
var births = data.response.legislators.map(function(d) {
return format.parse(d.legislator.birthdate);
});
var data = d3.nest()
.key(function(d) {
return d.getFullYear();
})
.entries(births);
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickFormat(yformat);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
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 + ")");
x.domain(d3.extent(data, function(d) { return format.parse(d.key + '-01-01'); }));
y.domain(d3.extent(data, function(d) { return d.values.length }));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Frequency");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(format.parse(d.key + '-01-01')); })
.attr("width", 10)
.attr("y", function(d) { return y(d.values.length); })
.attr("height", function(d) { return height - y(d.values.length); });
});
</script>
Modified http://d3js.org/d3.v3.min.js to a secure url
https://d3js.org/d3.v3.min.js