Skip to content

Instantly share code, notes, and snippets.

@krish85
Created August 13, 2015 19:04
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 krish85/4ab59ea663520058a004 to your computer and use it in GitHub Desktop.
Save krish85/4ab59ea663520058a004 to your computer and use it in GitHub Desktop.
Rendering
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>
<body>
<script>
var scale = d3.scale.linear().domain([1,5]).range([0,200]);
var svg = d3.select("body").append("svg").attr("height",250).attr("width",250);
function render(data, color){
//Bind data
var rects = svg.selectAll("rect").data(data);
//Enter
rects.enter().append("rect")
.attr("y",50)
.attr("height",20)
.attr("width",20);
//Update
rects.attr("x",scale).attr("fill",color);
//Exit
rects.exit().remove();
}
setTimeout(function() { render([1,2,2.5], "green");}, 1000);
setTimeout(function() { render([1,2,3,4,5], "blue");}, 1500);
setTimeout(function() { render([1,2], "red");}, 2000);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment