[ Launch: Tributary inlet ] 5388568 by jvns
-
-
Save jvns/5388568 to your computer and use it in GitHub Desktop.
histogram example
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
{"description":"histogram example","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01} |
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
// Generate an Irwin–Hall distribution of 10 random variables. | |
var values = d3.range(1000).map(d3.random.irwinHall(10)); | |
values = [1,2,38] | |
function update_histogram(values) { | |
// A formatter for counts. | |
var formatCount = d3.format(",.0f"); | |
var margin = {top: 63, right: 30, bottom: 30, left: 30}, | |
width = 568 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
var x = d3.scale.linear() | |
.domain([d3.min(values), d3.max(values)]) | |
.range([0, width]); | |
// Generate a histogram using twenty uniformly-spaced bins. | |
var data = d3.layout.histogram() | |
(values); | |
var y = d3.scale.linear() | |
.domain([0, d3.max(data, function(d) { return d.y; })]) | |
.range([height, 0]); | |
var xAxis = d3.svg.axis() | |
.scale(x) | |
.orient("bottom"); | |
var svg = d3.select("svg") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
d3.selectAll(".bar").remove() | |
d3.selectAll(".axis").remove() | |
var bar = svg.selectAll(".bar") | |
.data(data) | |
.enter().append("g") | |
.attr("class", "bar") | |
.attr("transform", function(d) { return "translate(" + x(d.x) + "," + y(d.y) + ")"; }); | |
bar.append("rect") | |
.attr("x", 1) | |
.attr("width", x(data[0].dx) - 1) | |
.attr("height", function(d) { return height - y(d.y); }); | |
bar.append("text") | |
.attr("dy", ".75em") | |
.attr("y", 10) | |
.attr("x", x(data[0].dx) / 2) | |
.attr("text-anchor", "middle") | |
.text(function(d) { return formatCount(d.y); }); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis); | |
} | |
update_histogram(values) | |
update_histogram([1,2,3]) | |
update_histogram([1,2,3,6]) |
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
body { | |
font: 10px sans-serif; | |
} | |
.bar rect { | |
fill: steelblue; | |
shape-rendering: crispEdges; | |
} | |
.bar text { | |
fill: #fff; | |
} | |
.axis path, .axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment