public
Last active — forked from mbostock/income.csv

Irregular Histogram

  • Download Gist
income.csv
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
Income,People
0,245981
8.8,150444
30,126063
49.9,123519
70,115029
90.7,277149
109.1,355768
130,324246
150.3,353239
170.2,396008
190,396725
210,398640
230.1,401932
250,416079
270,412727
289.8,385192
309.7,343178
329.7,293707
349.6,239982
369.7,201557
389.3,165132
442.3,442075
543.4,196526
679.9,146784
883.9,48600
1555,44644
index.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
<!DOCTYPE html>
<meta charset="utf-8">
<style>
 
svg {
font: 10px sans-serif;
}
 
rect {
fill: steelblue;
}
 
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
 
</style>
<body>
<script src="http://d3js.org/d3.v2.min.js?2.9.6"></script>
<script>
 
var margin = {top: 10, right: 20, bottom: 20, left: 60},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
 
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
 
var x = d3.scale.linear()
.range([0, width]);
 
var y = d3.scale.linear()
.range([height, 0]);
 
d3.csv("income.csv", function(bins) {
 
// Coerce types.
bins.forEach(function(bin) {
bin.Income = +bin.Income;
bin.People = +bin.People;
});
 
// Normalize each bin to so that height = quantity/width;
// see http://en.wikipedia.org/wiki/Histogram#Examples
for (var i = 1, n = bins.length, bin; i < n; i++) {
bin = bins[i];
bin.offset = bins[i - 1].Income;
bin.width = bin.Income - bin.offset;
bin.height = bin.People / bin.width;
}
 
// Drop the first bin, since it's incorporated into the next.
bins.shift();
 
// Set the scale domain.
x.domain([0, d3.max(bins.map(function(d) { return d.offset + d.width; }))]);
y.domain([0, d3.max(bins.map(function(d) { return d.height; }))]);
 
// Add the bins.
svg.selectAll(".bin")
.data(bins)
.enter().append("rect")
.attr("class", "bin")
.attr("x", function(d) { return x(d.offset); })
.attr("width", function(d) { return x(d.width) - 1; })
.attr("y", function(d) { return y(d.height); })
.attr("height", function(d) { return height - y(d.height); });
 
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.svg.axis()
.scale(x)
.orient("bottom"));
 
svg.append("g")
.attr("class", "y axis")
.call(d3.svg.axis()
.scale(y)
.orient("left"));
 
});
 
</script>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.