Skip to content

Instantly share code, notes, and snippets.

@bsurnida
Last active February 5, 2018 16:15
Show Gist options
  • Select an option

  • Save bsurnida/c3a0f464a7c45e0dba6bc1452e0c06bc to your computer and use it in GitHub Desktop.

Select an option

Save bsurnida/c3a0f464a7c45e0dba6bc1452e0c06bc to your computer and use it in GitHub Desktop.
Histogram Chart
license: gpl-3.0
function histogramChart() {
var margin = {top: 0, right: 0, bottom: 20, left: 0},
width = 960,
height = 500;
var histogram = d3.layout.histogram(),
x = d3.scale.ordinal(),
y = d3.scale.linear(),
xAxis = d3.svg.axis().scale(x).orient("bottom").tickSize(6, 0);
function chart(selection) {
selection.each(function(data) {
// Compute the histogram.
data = histogram(data);
// Update the x-scale.
x .domain(data.map(function(d) { return d.x; }))
.rangeRoundBands([0, width - margin.left - margin.right], .1);
// Update the y-scale.
y .domain([0, d3.max(data, function(d) { return d.y; })])
.range([height - margin.top - margin.bottom, 0]);
// Select the svg element, if it exists.
var svg = d3.select(this).selectAll("svg").data([data]);
// Otherwise, create the skeletal chart.
var gEnter = svg.enter().append("svg").append("g");
gEnter.append("g").attr("class", "bars");
gEnter.append("g").attr("class", "x axis");
// Update the outer dimensions.
svg .attr("width", width)
.attr("height", height);
// Update the inner dimensions.
var g = svg.select("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Update the bars.
var bar = svg.select(".bars").selectAll(".bar").data(data);
bar.enter().append("rect");
bar.exit().remove();
bar .attr("width", x.rangeBand())
.attr("x", function(d) { return x(d.x); })
.attr("y", function(d) { return y(d.y); })
.attr("height", function(d) { return y.range()[0] - y(d.y); })
.order();
// Update the x-axis.
g.select(".x.axis")
.attr("transform", "translate(0," + y.range()[0] + ")")
.call(xAxis);
});
}
chart.margin = function(_) {
if (!arguments.length) return margin;
margin = _;
return chart;
};
chart.width = function(_) {
if (!arguments.length) return width;
width = _;
return chart;
};
chart.height = function(_) {
if (!arguments.length) return height;
height = _;
return chart;
};
// Expose the histogram's value, range and bins method.
d3.rebind(chart, histogram, "value", "range", "bins");
// Expose the x-axis' tickFormat method.
d3.rebind(chart, xAxis, "tickFormat");
return chart;
}
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.bars rect {
fill: steelblue;
}
.axis text {
font: 10px sans-serif;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="histogram-chart.js"></script>
<script>
var numarr = [
176,
176,
176,
176,
176,
176,
4436,
15796,
8376,
13416,
10180,
38040,
9616,
47216,
13868,
47592,
12720,
45612,
10560,
30572,
7740,
8216,
7168,
8232,
6960,
31044,
8560,
43748,
10660,
38476,
9288,
29976,
8312,
26944,
7496,
9024,
7500,
11520,
9272,
32268,
8280,
31868,
8428,
31636,
8716,
36900,
8016,
28092,
7204,
9296,
7672,
9296,
7860,
44324,
13364,
54020,
20168,
59748,
10088,
37528,
9648,
74640,
8604,
15012,
9216,
10096,
7684,
10068,
9044,
38620,
11168,
41428,
10592,
34672,
15124,
75464,
18052,
19764,
14040,
17940,
17252,
65080,
18416,
60936,
19628,
93544,
25560,
96300,
24548,
86072,
21824,
102256,
45172,
25480,
22180,
106896,
28976,
97784,
26024,
98264,
25852,
93776,
21496,
62540,
17820,
23212,
16332,
18356,
16580,
59608,
19108,
69096,
18936,
47012,
14832,
45600,
14956,
43132,
11512,
19944,
11104,
19856,
16060,
50964,
15484,
48924,
12840,
49060,
13620,
48904,
14020,
44244,
11668,
14080,
13148,
14724,
12812,
48088,
15472,
45612,
16824,
70880,
16096,
53868,
12332,
54492,
14020,
19300,
16092,
24608,
21328,
78164,
21244,
87024,
28696,
82704,
15736,
42700,
13624,
44944,
13216,
19992,
11476,
14444,
15436,
58932,
15796,
50712,
14468,
49772,
16368,
48816,
16000,
55184,
13224,
19980,
11368,
15200,
14552,
50080,
13748,
51812,
16104,
53088,
18380,
60412,
4,
4,
4,
4,
4,
4,
14556,
16508,
22204,
15444,
22468,
18724,
59560,
23384,
74800,
17612,
84632,
20684,
70376,
16156,
59560,
7968,
9744,
7884,
9596,
8744,
47824,
12584,
38768,
9584,
36480,
10856,
47236,
12544,
46596
]
console.log(irwinHallDistribution(10, 10))
//irwinHallDistribution(100, 10)
d3.select("body")
.datum(numarr)
.call(histogramChart()
.bins(d3.scale.linear().ticks(20))
.tickFormat(d3.format(".02f")));
function irwinHallDistribution(n, m) {
var distribution = [];
for (var i = 0; i < n; i++) {
for (var s = 0, j = 0; j < m; j++) {
s += Math.random();
}
distribution.push(s / m);
}
return distribution;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment