Skip to content

Instantly share code, notes, and snippets.

@mjfwest
Created September 25, 2012 23:24
Show Gist options
  • Select an option

  • Save mjfwest/3785048 to your computer and use it in GitHub Desktop.

Select an option

Save mjfwest/3785048 to your computer and use it in GitHub Desktop.
D3js example, Single Line works. Multi Line works.. But then dies quickly.
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>multilinegraph</title>
<meta name="generator" content="TextMate http://macromates.com/">
<meta name="author" content="Martin West">
<!-- Date: 2012-09-26 -->
</head>
<style>
svg {
font: 10px sans-serif;
}
.line {
fill: none;
stroke: #000;
stroke-width: 1.5px;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="http://mbostock.github.com/d3/d3.js?2.7.2"></script>
<script>
var n = 40,
random = d3.random.normal(0, .2),
data = [d3.range(n).map(function(){return {x:random(),y:random()}})];
setTimeout(function(){ data.push(d3.range(n).map(function(){return {x:random(),y:random()}})) ;update_paths();},20*1000);
var margin = {top: 10, right: 10, bottom: 20, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear()
.domain([0, n-1])
.range([0, width]);
var y = d3.scale.linear()
.domain([-1, 1])
.range([height, 0]);
var z = d3.scale.category10();
var line = d3.svg.line()
.x(function(d, i) { return x(i); })
.y(function(d, i) { return y(d.y); });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.svg.axis().scale(x).orient("bottom"));
svg.append("g")
.attr("class", "y axis")
.call(d3.svg.axis().scale(y).orient("left"));
var line_group = svg.selectAll(".line_groups").data(data)
line_group.enter().append("g").attr("class", "line_groups")
line_group.exit().remove();
var group=svg.append("g")
.attr("clip-path", "url(#clip)")
update_paths();
tick();
function update_paths(){
var path=group.selectAll(".line")
.data(data);
path.enter().append("path")
.attr("class", "line")
.attr("d", line)
.attr("stroke",function(d,i){return z(i)});
path.exit().remove();
}
function tick() {
// push a new data point onto the back
data[0].push({x:random(),y:random()});
// redraw the line, and slide it to the left
var path=group.selectAll(".line")
.attr("d", line)
.attr("transform", null)
.transition()
.duration(500)
.ease("linear")
.attr("transform", "translate(" + x(-1) + ")")
.each("end", tick);
// pop the old data point off the front
data[0].shift();
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment