Skip to content

Instantly share code, notes, and snippets.

@monkeycycle
Forked from mbostock/.block
Last active March 27, 2017 20:18
Show Gist options
  • Save monkeycycle/d60ee1118fc7f307bd5b1cc64193e67e to your computer and use it in GitHub Desktop.
Save monkeycycle/d60ee1118fc7f307bd5b1cc64193e67e to your computer and use it in GitHub Desktop.
Stacked Bar Chart
license: gpl-3.0

This stacked bar chart is constructed from a CSV file storing the populations of different states by age group. The chart employs conventional margins and a number of D3 features:

  • d3-dsv - load and parse data
  • d3-scale - x- and y-position, and color encoding
  • d3-format - SI prefix formatting (e.g., “10M” for 10,000,000)
  • d3-array - compute simple statistics (e.g., max)
  • d3-axis - display axes
  • d3-shape - computed stacked positions
year limit startingBalance contribution interest total
2009 5000 0 0 0 0
2010 5000 0 0 0 0
2011 5000 0 0 0 0
2012 5000 0 0 0 0
2013 5500 0 0 0 0
2014 5500 0 0 0 0
2015 10000 0 0 0 0
2016 5500 0 0 0 0
2017 5500 20000 5500 765 26265
2018 6500 26000 6500 975 33475
2019 7000 32500 7000 1185 40685
2020 7500 39500 7500 1410 48410
2021 8000 47000 8000 1650 56650
2022 8500 55000 8500 1905 65405
2023 9000 63500 9000 2175 74675
2024 9500 72500 9500 2460 84460
2025 10000 82000 10000 2760 94760
2026 10500 92000 10500 3075 105575
2027 11000 102500 11000 3405 116905
2028 11500 113500 11500 3750 128750
2029 12000 125000 12000 4110 141110
2030 12500 137000 12500 4485 153985
2031 13000 149500 13000 4875 167375
2032 13500 162500 13500 5280 181280
2033 14000 176000 14000 5700 195700
2034 14500 190000 14500 6135 210635
2035 15000 204500 15000 6585 226085
2036 15500 219500 15500 7050 242050
2037 16000 235000 16000 7530 258530
2038 16500 0 0 0 0
2039 17000 0 0 0 0
2040 17500 0 0 0 0
2041 18000 0 0 0 0
2042 18500 0 0 0 0
2043 19000 0 0 0 0
2044 19500 0 0 0 0
2045 20000 0 0 0 0
2046 20500 0 0 0 0
2047 21000 0 0 0 0
2048 21500 0 0 0 0
2049 22000 0 0 0 0
2050 22500 0 0 0 0
2051 23000 0 0 0 0
2052 23500 0 0 0 0
2053 24000 0 0 0 0
2054 24500 0 0 0 0
2055 25000 0 0 0 0
2056 25500 0 0 0 0
2057 26000 0 0 0 0
2058 26500 0 0 0 0
2059 27000 0 0 0 0
2060 27500 0 0 0 0
2061 28000 0 0 0 0
2062 28500 0 0 0 0
2063 29000 0 0 0 0
2064 29500 0 0 0 0
2065 30000 0 0 0 0
2066 30500 0 0 0 0
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.bar {
fill: steelblue;
}
.axis path {
display: none;
}
</style>
<svg width="960" height="500"></svg>
<script src="//d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 30, left: 40},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom,
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var x = d3.scaleBand()
.rangeRound([0, width])
.padding(0.1)
.align(0.1);
var y = d3.scaleLinear()
.rangeRound([height, 0]);
var z = d3.scaleOrdinal()
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);
var stack = d3.stack();
d3.csv("data.csv", type, function(error, data) {
if (error) throw error;
data.sort(function(a, b) { return b.total - a.total; });
x.domain(data.map(function(d) { return d.yeah; }));
y.domain([0, d3.max(data, function(d) { return d.total; })]).nice();
z.domain(data.columns.slice(1));
g.selectAll(".serie")
.data(stack.keys(data.columns.slice(1))(data))
.enter().append("g")
.attr("class", "serie")
.attr("fill", function(d) { return z(d.key); })
.selectAll("rect")
.data(function(d) { return d; })
.enter().append("rect")
.attr("x", function(d) { return x(d.data.year); })
.attr("y", function(d) { return y(d[1]); })
.attr("height", function(d) { return y(d[0]) - y(d[1]); })
.attr("width", x.bandwidth());
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
g.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y).ticks(10, "s"))
.append("text")
.attr("x", 2)
.attr("y", y(y.ticks(10).pop()))
.attr("dy", "0.35em")
.attr("text-anchor", "start")
.attr("fill", "#000")
.text("Population");
var legend = g.selectAll(".legend")
.data(data.columns.slice(1).reverse())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; })
.style("font", "10px sans-serif");
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.attr("fill", z);
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.attr("text-anchor", "end")
.text(function(d) { return d; });
});
function type(d, i, columns) {
for (i = 1, t = 0; i < columns.length; ++i) t += d[columns[i]] = +d[columns[i]];
d.total = t;
return d;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment