Skip to content

Instantly share code, notes, and snippets.

@letsbreelhere
Created September 12, 2012 18:22
Show Gist options
  • Save letsbreelhere/3708825 to your computer and use it in GitHub Desktop.
Save letsbreelhere/3708825 to your computer and use it in GitHub Desktop.
d3 stacked line graph
var width = 400;
var height = 300;
var len = 20;
var rand_data = function() {
return d3.range(len).map(function() { return 1 + Math.random() });
}
var stack = d3.select("#chart").
append("svg:svg").
attr("width", width).
attr("height", height);
var x = d3.scale.linear().domain([0, len]).range([0, width]);
var y = d3.scale.linear().domain([0, 6]).range([height, 0]);
var max_height = []
var area = d3.svg.area().
x(function(d, i) { return x(i) }).
y0(function(d, i) {
if (typeof max_height[i] !== "undefined") {
return y(max_height[i])
} else {
return y(0)
}
}).
y1(function(d, i) {
if (typeof max_height[i] !== "undefined") {
max_height[i] += d;
return y(max_height[i]);
} else {
max_height[i] = d;
return y(d)
}
}).
interpolate("basis");
var line = d3.svg.line().
x(function(d, i) { return x(i); }).
y(function(d, i) { return y(d.y); });
var color = d3.interpolateRgb("#000", "#fff");
stack.selectAll("path").data([rand_data(), rand_data()]).enter().
append("svg:path").
attr("d", area).
style("fill", function() { return color(Math.random()) });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment