Skip to content

Instantly share code, notes, and snippets.

@bstaats
Forked from mbostock/.block
Created February 20, 2012 20:24
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 bstaats/1871195 to your computer and use it in GitHub Desktop.
Save bstaats/1871195 to your computer and use it in GitHub Desktop.
Stacked Aera Spline Transition
<!DOCTYPE html>
<meta charset="utf-8">
<style>
svg {
font: 10px sans-serif;
}
.Beer {
fill: #ccc;
stroke: none;
stroke-width: 1.5px;
}
.Wine {
fill: #ddd;
stroke: none;
stroke-width: 1.5px;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="http://mbostock.github.com/d3/d3.js?2.7.2"></script>
<script>
var margin = {top: 10, right: 10, bottom: 20, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom,
duration = 2000,
domain = [new Date(2012, 01, 01), new Date(2012, 01, 29)],
random = d3.random.normal(10, 2.5),
context = ['Beer','Wine'],
data = d3.layout.stack()(context.map(function(k){
return d3.time.days(domain[0],domain[1]).map(function(d){
return {x:d, y:random(), key:k};
})
})),
x = d3.time.scale().domain(domain).range([0, width]),
y = d3.scale.linear().domain([0,d3.max(d3.merge(data),function(d){return d.y0 + d.y;})]).range([height,0]);
var area = d3.svg.area()
.interpolate("basis")
.x(function(d) { return x(d.x); })
.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 + ")");
svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
var axis = svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(x.axis = d3.svg.axis().scale(x).orient("bottom"));
svg.append("g")
.attr("class", "y axis")
.call(d3.svg.axis().scale(y).orient("left"));
var g = svg.append("g")
.attr("clip-path", "url(#clip)");
var path = g.selectAll('path')
.data(data)
.enter().append('path')
.attr("class", function(d){ return 'area '+d[0].key;})
.attr("d", function(d){ return area(d)});
setTimeout(tick, duration);
function tick() {
var past = domain[0];
//slide window
domain[0].setDate(domain[0].getDate()+1);
domain[1].setDate(domain[1].getDate()+1);
// reset x
x.domain(domain);
// push a new data point onto the back
var my = 0;
data.forEach(function(d,i){
var r = random();
d.push({x:domain[1],y:r,y0:my,key:context[i]});
my += r;
});
// rest y
y.domain([0,Math.max(my,y.domain()[1])]);
// redraw the area
d3.selectAll('path.area')
.attr("d", area)
.attr("transform", null);
// slide the x-axis left
axis.transition()
.duration(duration)
.ease("linear")
.call(x.axis);
// slide it to the left
d3.selectAll('path.area')
.transition()
.duration(duration)
.ease("linear")
.attr("transform", "translate(" + x(domain[0]) + ")");
// pop the old data point off the front
data.forEach(function(d){
d.shift();
});
setTimeout(tick, duration);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment