Skip to content

Instantly share code, notes, and snippets.

@adamvaughan
Created October 16, 2012 14:20
Show Gist options
  • Save adamvaughan/3899545 to your computer and use it in GitHub Desktop.
Save adamvaughan/3899545 to your computer and use it in GitHub Desktop.
D3 Sparkline
<html>
<head>
<title>D3 Sparkline</title>
<script src="http://mbostock.github.com/d3/d3.v2.js"></script>
<style>
#graph {
width: 300px;
height: 60px;
}
path {
stroke: steelblue;
stroke-width: 1;
fill: lightsteelblue;
}
</style>
</head>
<body>
<div id="graph"></div>
<script>
var graph = d3.select("#graph").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, 5, 9];
var x = d3.scale.linear().domain([0, 10]).range([0, 50]);
var y = d3.scale.linear().domain([0, 10]).range([0, 30]);
var line = d3.svg.line()
.x(function(d, i) {
return x(i);
})
.y(function(d) {
return y(d);
});
graph.append("svg:path").attr("d", line(data));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment