Skip to content

Instantly share code, notes, and snippets.

@mravey
Created October 30, 2012 21:03
Show Gist options
  • Save mravey/3983044 to your computer and use it in GitHub Desktop.
Save mravey/3983044 to your computer and use it in GitHub Desktop.
Simple line animation
window.addEventListener('DOMContentLoaded', function() {
d3.selectAll('body > *').remove();
d3.select('body').style('overflow', 'hidden');
var svg = d3.select('body').append('svg')
.attr('height', '100%')
.attr('width', '100%');
var x = d3.scale.linear().domain([0, 100]).range([0, parseInt(svg.style('width'), 10)]);
var y = d3.scale.linear().domain([0, 100]).range([parseInt(svg.style('height'), 10), 0]);
var line = d3.svg.line()
.x(function(d) { return x(d.x); })
.y(function(d) { return y(d.y); })
.interpolate('cardinal');
svg.append('path')
.datum(getData())
.attr('d', line)
.attr('fill', 'none')
.attr('stroke', 'blue');
function draw() {
svg.selectAll('path')
.datum(getData())
.transition()
.duration(1000)
.attr('d', line);
}
setInterval(draw, 1000);
}, false);
function getData() {
var data = [];
var LIMIT = 100;
for (var i = 0; i < LIMIT; i++) {
data.push({
x: i,
y: parseInt(Math.random() * 100, 10)
});
}
return data;
}
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.7.3/build/cssreset/cssreset-min.css">
<script type="text/javascript" src="https://raw.github.com/mbostock/d3/master/d3.v2.min.js"></script>
<script type="text/javascript" src="app.js"></script>
</head>
<body>
</body>
</html>
@biovisualize
Copy link

Could you please fix the broken link to d3.js, using http://d3js.org/d3.v2.min.js ?

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