Skip to content

Instantly share code, notes, and snippets.

@pandafulmanda
Last active November 9, 2015 22:59
Show Gist options
  • Save pandafulmanda/d604ca62cde0beaac648 to your computer and use it in GitHub Desktop.
Save pandafulmanda/d604ca62cde0beaac648 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];
var moreData = [1, 2, 3, 4, 5, 10, 6, 2, 9, 20, 54, 30];
drawBars(sampleData);
// try drawBars(moreData);
function drawBars(data){
var eachDataPoint = d3.select("svg")
.selectAll("rect")
.data(data);
eachDataPoint.enter()
.append("rect");
eachDataPoint
.attr("x", function(dataItem, index){
return 20 * index;
})
.attr("y", function(){
return 0;
})
.attr("width", function(dataItem, index){
return 20;
})
.attr("height", function(dataItem, index){
return dataItem * 100;
});
eachDataPoint.exit()
.remove();
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment