Skip to content

Instantly share code, notes, and snippets.

@d3indepth
Last active August 2, 2017 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d3indepth/e4efd402b4d9fdb2088ccdf3135745c3 to your computer and use it in GitHub Desktop.
Save d3indepth/e4efd402b4d9fdb2088ccdf3135745c3 to your computer and use it in GitHub Desktop.
Stack generator
license: gpl-3.0
height: 210
border: no
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>Stack generator</title>
</head>
<style>
</style>
<body>
<svg width="700" height="200">
<g></g>
</svg>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
<script>
var yScale = d3.scaleLinear().domain([0, 600]).range([200, 0]);
var areaGenerator = d3.area()
.x(function(d, i) {
return i * 100;
})
.y0(function(d) {
return yScale(d[0]);
})
.y1(function(d) {
return yScale(d[1]);
});
var colors = ['#FBB65B', '#513551', '#de3163'];
var data = [
{day: 'Mon', apricots: 120, blueberries: 180, cherries: 100},
{day: 'Tue', apricots: 60, blueberries: 185, cherries: 105},
{day: 'Wed', apricots: 100, blueberries: 215, cherries: 110},
{day: 'Thu', apricots: 80, blueberries: 230, cherries: 105},
{day: 'Fri', apricots: 120, blueberries: 240, cherries: 105}
];
var stack = d3.stack()
.keys(['apricots', 'blueberries', 'cherries']);
var stackedSeries = stack(data);
d3.select('g')
.selectAll('path')
.data(stackedSeries)
.enter()
.append('path')
.style('fill', function(d, i) {
return colors[i];
})
.attr('d', areaGenerator)
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment