Skip to content

Instantly share code, notes, and snippets.

@wimdows
Forked from benjchristensen/index.html
Created December 6, 2011 14:10
Show Gist options
  • Save wimdows/1438322 to your computer and use it in GitHub Desktop.
Save wimdows/1438322 to your computer and use it in GitHub Desktop.
Animated Line Graphs / Sparklines using SVG Path and d3.js
<html>
<head>
<title>Animated Sparkline using SVG Path and d3.js</title>
<script src="https://raw.github.com/mbostock/d3/master/d3.js"></script>
<style>
/* tell the SVG path to be a thin blue line without any area fill */
path {
stroke: steelblue;
stroke-width: 1;
fill: none;
}
</style>
</head>
<body>
<p>
<b>Size:</b> 300x30 &nbsp;&nbsp;<b>Interpolation:</b> basis &nbsp;&nbsp;<b>Animation:</b> true &nbsp;&nbsp;<b>Transition:</b> 1000ms &nbsp;&nbsp;<b>Update Frequency:</b> 1000ms
<div id="graph1" class="aGraph" style="width:600px; height:200px;"></div>
</p>
<script>
function displayGraphExample(id, width, height, interpolation, animate, updateDelay, transitionDelay) {
var graph = d3.select(id).append("svg:svg").attr("width", "100%").attr("height", "100%");
var data = [3, 6, 2, 7, 5, 2, 1, 3, 8, 9, 2, 5, 9, 3, 6, 3, 6, 2, 7, 5, 2, 1, 3, 8, 9, 2, 5, 9, 2, 7, 5, 2, 1, 3, 8, 9, 2, 5, 9, 3, 6, 2, 7, 5, 2, 1, 3, 8, 9, 2, 9];
var x = d3.scale.linear().domain([0, 48]).range([-5, width]);
var y = d3.scale.linear().domain([0, 10]).range([0, height]);
var line = d3.svg.line()
.x(function(d,i) {
return x(i);
})
.y(function(d) {
return y(d);
})
.interpolate(interpolation)
graph.append("svg:path").attr("d", line(data));
xruler = graph.selectAll("g.rulers")
.data(data)
.enter().append("svg:g")
.attr("transform", function(d, i) { return "translate(" + x(i) + "," + height + ")rotate(-90)"; })
xruler.append("svg:text")
.attr("class", "datums")
.attr("dy", "1.5em")
// .attr("x", function(d, i) { return x(i); })
.text(function(d) { return d; })
.style("text-anchor", "end")
.style("stroke", "black")
// .style("display", function(d, i) { return i % 12 ? "none" : null }) //|| i == 0
// .attr("transform", function() { return "rotate(-90)"; });
function redrawWithAnimation() {
graph.selectAll("path")
.data([data])
.attr("transform", "translate(" + x(1) + ")")
.attr("d", line)
.transition()
.ease("linear")
.duration(transitionDelay)
.attr("transform", "translate(" + x(0) + ")");
xruler.select("text")
.data(data)
.attr("transform", "translate(0," + x(1) + ")")
// .attr("transform", function() { return "rotate(-90)"; })
.text(function(d) { return d; })
.transition()
.ease("linear")
.duration(transitionDelay)
// .attr("transform", function() { return "rotate(-90)"; })
.attr("transform", "translate(0," + x(0) + ")")
}
function redrawWithoutAnimation() {
graph.selectAll("path")
.data([data])
.attr("d", line);
}
setInterval(function() {
var v = data.shift();
data.push(v);
if(animate) {
redrawWithAnimation();
} else {
redrawWithoutAnimation();
}
}, updateDelay);
}
displayGraphExample("#graph1", 600, 30, "basis", true, 1000, 1000);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment