A D3 bar chart created with DIV elements.
Last active
June 12, 2016 14:47
-
-
Save jeffreymorganio/abc8ad469e4159193c1d5f7af169e392 to your computer and use it in GitHub Desktop.
A D3 Bar Chart of DIV Elements.
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
div.bar { | |
width: 50px; | |
margin-right: 4px; | |
display: inline-block; | |
background-color: #C70039; | |
} |
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
function drawBarChart() { | |
var dataSet = randomDataSet(10, 5, 25); | |
d3.select("body").selectAll("div") | |
.data(dataSet) | |
.enter() | |
.append("div") | |
.attr("class", "bar") | |
.style("height", function(d) { | |
var barHeight = d * 10; | |
return barHeight + "px"; | |
}); | |
} | |
function randomDataSet(dataSetSize, minValue, maxValue) { | |
return new Array(dataSetSize).fill(0).map(function(n) { | |
return Math.random() * (maxValue - minValue) + minValue; | |
}); | |
} |
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"> | |
<title>A D3 Bar Chart of DIV Elements</title> | |
<link rel="stylesheet" href="bar-chart.css"> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js" charset="utf-8"></script> | |
<script src="bar-chart.js"></script> | |
</head> | |
<body onload="drawBarChart()"> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment