Last active
August 29, 2015 14:23
-
-
Save jamesleesaunders/9aeba4cd5c5661d63092 to your computer and use it in GitHub Desktop.
D3 : Basic DIV Height and Width
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> | |
<head> | |
<title>D3 : DIV Height using Scale / Range</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js" charset="utf-8"></script> | |
<script src="https://raw.githubusercontent.com/mbostock/d3/master/lib/colorbrewer/colorbrewer.js"></script> | |
<style type="text/css"> | |
.bar { | |
display: inline-block; | |
width: 25px; | |
height: 75px; | |
margin-right: 2px; | |
background-color: teal; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="chartholder"></div> | |
<script type="text/javascript"> | |
var height = 400; | |
var width = 600; | |
var data = d3.range(10).map(function () { | |
return Math.random(); | |
}); | |
var colors = colorbrewer.Set2[5]; | |
var colorIndex = d3.scale.linear() | |
.domain([0, 1, 2, 3, 4, 5]) | |
.range(colors); | |
var yScale = d3.scale.linear() | |
.domain([0, d3.max(data)]) | |
.range([0, height]); | |
var svg = d3.select("#chartholder") | |
.style('width', width + 'px') | |
.style('height', height + 'px'); | |
svg.selectAll('div') | |
.data(data) | |
.enter() | |
.append('div') | |
.attr('class', 'bar') | |
.style('height', function (d) { | |
return yScale(d) + 'px'; | |
}) | |
.style('width', function () { | |
return (width / 10) * 0.95 + 'px'; | |
}) | |
.style('background-color', function (d) { | |
return colorIndex(Math.round(d * 5)); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment