Skip to content

Instantly share code, notes, and snippets.

@fieldoffice
Last active August 29, 2015 14:03
Show Gist options
  • Save fieldoffice/e3682bf807d2c3f8bf7f to your computer and use it in GitHub Desktop.
Save fieldoffice/e3682bf807d2c3f8bf7f to your computer and use it in GitHub Desktop.
Generate random numbers for d3 barchart
var dataset = [];
for (var i = 0; i < 25; i++) { //Loop 25 times
var newNumber = Math.round(Math.random() * 30); //New random number (0-30)
dataset = dataset.concat(newNumber); //Add new number to array
}
d3.select("body").selectAll("div")
.data(dataset)
.enter()
.append("div")
.attr("class", "bar")
.style("height", function(d) {
var barHeight = d * 5;
return barHeight + "px";
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment