Created
March 15, 2013 18:48
-
-
Save Meekohi/5172099 to your computer and use it in GitHub Desktop.
Snippet for animating data on "Streams" d3.js demo
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
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
path { | |
stroke: #000; | |
fill-opacity: .8; | |
} | |
</style> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script> | |
var width = 960, | |
height = 500; | |
var m = 5, // number of series | |
n = 90; // number of values | |
function w() | |
{ | |
// Generate random data into five arrays. | |
return d3.range(m).map(function() { | |
return d3.range(n).map(function() { | |
return Math.random() * 100 | 0; | |
}); | |
}); | |
} | |
var x = d3.scale.linear() | |
.domain([0, n - 1]) | |
.range([0, width]); | |
var y = d3.scale.ordinal() | |
.domain(d3.range(m)) | |
.rangePoints([0, height], 1); | |
var color = d3.scale.ordinal() | |
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56"]); | |
var area = d3.svg.area() | |
.interpolate("basis") | |
.x(function(d, i) { return x(i); }) | |
.y0(function(d) { return -d / 2; }) | |
.y1(function(d) { return d / 2; }); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
svg.selectAll("path").data(w).enter().append("path") | |
setInterval(function(){ | |
svg.selectAll("path").data(w) | |
.transition() | |
.attr("transform", function(d, i) { return "translate(0," + y(i) + ")"; }) | |
.style("fill", function(d, i) { return color(i); }) | |
.attr("d", area); | |
},1000); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment