Skip to content

Instantly share code, notes, and snippets.

@mbostock
Last active Aug 8, 2019
Embed
What would you like to do?
Stacked Area via Nest
license: gpl-3.0
redirect: https://observablehq.com/@d3/stacked-area-chart-via-d3-group
key value date
Group1 37 04/23/12
Group2 12 04/23/12
Group3 46 04/23/12
Group1 32 04/24/12
Group2 19 04/24/12
Group3 42 04/24/12
Group1 45 04/25/12
Group2 16 04/25/12
Group3 44 04/25/12
Group1 24 04/26/12
Group2 52 04/26/12
Group3 64 04/26/12
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var format = d3.time.format("%m/%d/%y");
var margin = {top: 20, right: 30, 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 z = d3.scale.category20c();
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(d3.time.days);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var stack = d3.layout.stack()
.offset("zero")
.values(function(d) { return d.values; })
.x(function(d) { return d.date; })
.y(function(d) { return d.value; });
var nest = d3.nest()
.key(function(d) { return d.key; });
var area = d3.svg.area()
.interpolate("cardinal")
.x(function(d) { return x(d.date); })
.y0(function(d) { return y(d.y0); })
.y1(function(d) { return y(d.y0 + d.y); });
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 + ")");
d3.csv("data.csv", function(error, data) {
if (error) throw error;
data.forEach(function(d) {
d.date = format.parse(d.date);
d.value = +d.value;
});
var layers = stack(nest.entries(data));
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.y0 + d.y; })]);
svg.selectAll(".layer")
.data(layers)
.enter().append("path")
.attr("class", "layer")
.attr("d", function(d) { return area(d.values); })
.style("fill", function(d, i) { return z(i); });
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
});
</script>
@jeffersonrpn

This comment has been minimized.

Copy link

@jeffersonrpn jeffersonrpn commented Nov 28, 2017

Any example using v4?

@kiranb555

This comment has been minimized.

Copy link

@kiranb555 kiranb555 commented May 10, 2019

Thank you mike :)

@stuwilmur

This comment has been minimized.

Copy link

@stuwilmur stuwilmur commented Aug 7, 2019

Is there a v4 version of this that I might have missed? Thanks for the example!

@mbostock

This comment has been minimized.

Copy link
Owner Author

@mbostock mbostock commented Aug 7, 2019

Here is an updated example using d3-array 2.0’s d3.group: https://observablehq.com/@d3/stacked-area-chart-via-d3-group

@stuwilmur

This comment has been minimized.

Copy link

@stuwilmur stuwilmur commented Aug 8, 2019

Wonderful thank you Mike.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment