Built with blockbuilder.org
Last active
March 16, 2017 01:26
-
-
Save scottlittle/1b52c60db716a2e38a3e9ad58a0aa08c to your computer and use it in GitHub Desktop.
basic d3 bar chart
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
license: mit |
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> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://d3js.org/d3.v3.min.js"></script> | |
<style> | |
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
</style> | |
</head> | |
<body> | |
<script> | |
var dataset = [ 5, 10, 13, 19, 21, 25, 22, 18, 15, 13, | |
11, 12, 15, 20, 18, 17, 16, 18, 23, 25 ]; | |
//Width and height | |
var w = 500; | |
var h = 100; | |
var barPadding = 1; | |
var vscale = d3.scale.linear() | |
.domain([0, d3.max(dataset)]) | |
.range([0, h]); | |
//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("width", w / dataset.length - barPadding) | |
.attr("height", function(d) { | |
return vscale(d); //Just the data value | |
}) | |
.attr("y", function(d) { | |
return h - vscale(d); //Height minus data value | |
}) | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment