Skip to content

Instantly share code, notes, and snippets.

@chuckpr
Last active August 29, 2015 14:10
Show Gist options
  • Save chuckpr/cc4369aa8e34245b923f to your computer and use it in GitHub Desktop.
Save chuckpr/cc4369aa8e34245b923f to your computer and use it in GitHub Desktop.

First D3.js plot!!!

var dataset = [];
for (var i = 0; i < 12; i++) {
var n = Math.random() * 30;
dataset.push(n);
}
var w = 500;
var h = 100;
var p = 4;
var scale = d3.scale.linear();
scale.domain([100, 500])
scale.range([10, 500])
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", function(x, i) {
return i * (w / dataset.length);
})
.attr("y", function(x) {
return h - x * 4;
})
.attr("width", w / dataset.length - p)
.attr("height", function (x) {
return x * 4;
})
.attr("fill", "teal");
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3: Drawing divs with data</title>
<script src="http://d3js.org/d3.v3.min.js"></script>
<style type="text/css">
div.bar {
display: inline-block;
width: 30px;
height: 75px;
background-color: teal;
margin-right: 10px;
}
</style>
</head>
<body>
<script type="text/javascript" src="bars.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment