Skip to content

Instantly share code, notes, and snippets.

@peachyo
Created August 10, 2014 18:05
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 peachyo/c333cc637ea7993fccad to your computer and use it in GitHub Desktop.
Save peachyo/c333cc637ea7993fccad to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script>
<style>
body {
background-color: #777;
}
</style>
<body>
<div>
<script>
var width=960,
height = 500,
margin = 50,
i=0;
var svg=d3.select("div").append("svg").attr("width",width)
.attr("height",height);
var line = d3.svg.line()
.x(function(d) { return d[0]+100; })
.y(function(d) { return d[1] + 100; })
.interpolate("step");
function init(){
var randData = [];
for (var i = 0; i < 8; i++){
randData.push([i*50, Math.random() * 200]);
}
svg.data([randData])
.append("path")
.attr("d", line)
.attr("fill", "green")
.attr("stroke", "#aaa")
.attr("stroke-width", 7);
svg.selectAll("circle")
.data(randData)
.enter().append("circle")
.attr("cx", function(d,i){ return d[0]+100; })
.attr("cy", function(d,i){ return d[1] + 100 })
.attr("fill", "#dfdfdf")
.attr("r", 10);
setInterval(refresh, 1200);
}
function refresh(){
var randData = [];
for (var i = 0; i < 8; i++){
randData.push([i*50, Math.random() * 200]);
}
svg.data([randData])
.select("path")
.transition()
.duration(500)
.attr("d", line);
svg.selectAll("circle")
.data(randData)
.transition()
.duration(500)
.attr("cy", function(d,i){ return d[1] + 100 });
}
init();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment