Created
February 8, 2013 22:29
-
-
Save koaning/4742471 to your computer and use it in GitHub Desktop.
simplebar
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<style type="text/css"> | |
/* No style rules here yet */ | |
</style> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
//Width and height | |
var w = 400; | |
var h = 200; | |
var barPadding = 1; | |
var dataset = [ 1,2,3,4,5,6,7,8,9,10,11,12,15,20,25,30 ]; | |
var i = 0; | |
for (var i = dataset.length - 1; i >= 0; i--) { | |
console.log(dataset[i]) | |
dataset[i] = dataset[i] + Math.round(Math.random()*20) | |
}; | |
//Create SVG element | |
var svg = d3.select("body") | |
.append("svg") | |
.attr("width", w) | |
.attr("height", h); | |
svg.selectAll("rect") | |
.data(dataset) | |
.enter() | |
.append("rect") | |
.attr("x", function(d, i) { | |
return i * (w / dataset.length); | |
}) | |
.attr("y", function(d) { | |
return h - (d * 4); | |
}) | |
.attr("width", w / dataset.length - barPadding) | |
.attr("height", function(d) { | |
return d * 4; | |
}) | |
.attr("fill", function(d) { | |
return "gray"; | |
}); | |
svg.selectAll("text") | |
.data(dataset) | |
.enter() | |
.append("text") | |
.text(function(d) { | |
return d; | |
}) | |
.attr("text-anchor", "middle") | |
.attr("x", function(d, i) { | |
return i * (w / dataset.length) + (w / dataset.length - barPadding) / 2; | |
}) | |
.attr("y", function(d) { | |
return h - (d * 4) + 14; | |
}) | |
.attr("font-family", "sans-serif") | |
.attr("font-size", "11px") | |
.attr("fill", "white"); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment