Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@pandafulmanda
Created November 9, 2015 23:00
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 pandafulmanda/c6706391ac03f3deb4e8 to your computer and use it in GitHub Desktop.
Save pandafulmanda/c6706391ac03f3deb4e8 to your computer and use it in GitHub Desktop.
<html>
<head>
<script src="http://d3js.org/d3.v3.min.js"></script>
</head>
<body>
<svg width="960" height="500"></svg>
<script>
var sampleData = [1, 2, 3, 4, 5];
// Tell d3 that each "rect" should be bound to each item in sampleData
var eachDataPoint = d3.select("svg")
.selectAll("rect")
.data(sampleData);
// Says that if there is a data point that is not rendered yet,
// d3 should make a new "rect" and add it to the svg
eachDataPoint.enter().append("rect");
// Says that for each data point as bound to a "rect",
// it should set the rect's attributes based on the data
eachDataPoint.attr("x", function(dataItem, index){
return 20 * index;
})
.attr("y", function(){
return 0;
})
.attr("height", function(dataItem, index){
return dataItem * 100;
})
.attr("width", function(dataItem, index){
return 20;
});
// Says that for each "rect" point that is exiting, remove that element
eachDataPoint.exit().remove();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment