Created
September 12, 2012 18:22
-
-
Save letsbreelhere/3708825 to your computer and use it in GitHub Desktop.
d3 stacked line graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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