Last active
November 9, 2015 22:59
-
-
Save pandafulmanda/d604ca62cde0beaac648 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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