Skip to content

Instantly share code, notes, and snippets.

@jhsu
Created February 22, 2012 15:16
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 jhsu/1885497 to your computer and use it in GitHub Desktop.
Save jhsu/1885497 to your computer and use it in GitHub Desktop.
<html>
<head><title></title>
<style>
svg {
border: 1px solid #ccc;
}
path {
stroke: steelblue;
stroke-width: 1;
fill: none;
}
line {
stroke-width: 1;
stroke: #ececec;
}
</style>
</head>
<body>
<div id="content">
</div>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.v2.js"></script>
<script>
var width = 60;
var height = 15;
vis = d3.select("#content")
.append("svg")
.attr("width", width)
.attr("height", height);
g = vis.append("svg:g")
.attr("class", "sparkline");
g.append("svg:line")
.attr("x1", 0)
.attr("y1", height / 2)
.attr("x2", width)
.attr("y2", height / 2)
g.append("svg:path")
var max = 100;
y = d3.scale.linear()
.domain([0, max])
.range([height, 0]);
x = d3.scale.linear()
.domain([0, width])
.range([0, width]);
plot = d3.svg.line()
.x(function(d,i) { return x(i)})
.y(function(d) { return y(d)})
.interpolate("linear")
line = {
'name': "",
'data': [],
'addPoint': function (datapoint) {
this.data.push(datapoint)
maxx = x(this.data.length-1)
if (maxx >= width) {
this.graph.select("g.sparkline")
.select("path")
.attr("d", plot(this.data))
.attr("transform", "translate(" + x(1) + ")")
.transition().duration(100).ease("linear")
.attr("transform", "translate(" + x(0) + ")");
this.data.shift()
} else {
// this.graph.attr("width", maxx)
this.graph.select("g.sparkline")
.select("path")
.attr("d", plot(this.data))
}
}
}
line.graph = vis;
setInterval(function () {
line.addPoint(Math.floor(Math.random() * 100))
}, 100);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment