Hexgonal Binning
HTML
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//d3js.org/d3.hexbin.v0.min.js?5c6e4f0"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/d3-tip/0.6.3/d3-tip.min.js"></script>
<script src="//rawgit.com/gka/d3-jetpack/master/d3-jetpack.js"></script>
CSS
.axis text {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.hexagon {
fill: none;
stroke: #000;
stroke-width: .5px;
}
.d3-tip {
line-height: 1;
font-weight: bold;
padding: 12px;
background: rgba(0, 0, 0, 0.8);
color: #fff;
border-radius: 2px;
pointer-events: none;
}
/* Creates a small triangle extender for the tooltip */
.d3-tip:after {
box-sizing: border-box;
display: inline;
font-size: 10px;
width: 100%;
line-height: 1;
color: rgba(0, 0, 0, 0.8);
position: absolute;
pointer-events: none;
}
/* Northward tooltips */
.d3-tip.n:after {
content: "\25BC";
margin: -1px 0 0 0;
top: 100%;
left: 0;
text-align: center;
}
/* Eastward tooltips */
.d3-tip.e:after {
content: "\25C0";
margin: -4px 0 0 0;
top: 50%;
left: -8px;
}
/* Southward tooltips */
.d3-tip.s:after {
content: "\25B2";
margin: 0 0 1px 0;
top: -8px;
left: 0;
text-align: center;
}
/* Westward tooltips */
.d3-tip.w:after {
content: "\25B6";
margin: -4px 0 0 -1px;
top: 50%;
left: 100%;
}
JavaScript
d3.selection.prototype.appendOnce = function(name) {
var that;
that = this.selectAll(name).data([{}]);
that.enter().append(name);
return that;
};
d3.selection.prototype.dataAppend = function(data, name) {
var that;
that = this.selectAll(name).data(data);
return that.enter().append(name);
};
d3.svg.container = function(sel, o, clip) {
var svg;
o.margin = o.margin || {
top: 20,
bottom: 40,
left: 40,
right: 20
};
o.W = o.width + o.margin.left + o.margin.right;
o.H = o.height + o.margin.top + o.margin.bottom;
svg = sel.appendOnce("svg").attr({
"width": o.W,
"height": o.H
}).appendOnce("g.main").attr({
transform: "translate(" + o.margin.left + ", " + o.margin.top + ")"
});
if (clip){
svg.append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("class", "mesh")
.attr("width", o.width)
.attr("height", o.height)
}
return svg;
};
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 600 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
var randomX = d3.random.normal(width / 2, 80),
randomY = d3.random.normal(height / 2, 80),
points = d3.range(2000).map(function() { return [randomX(), randomY()]; });
var color = d3.scale.linear()
.domain([0, 20])
.range(["white", "steelblue"])
.interpolate(d3.interpolateLab);
var x = d3.scale.identity()
.domain([0, width]);
var y = d3.scale.linear()
.domain([0, height])
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickSize(6, -height);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickSize(6, -width);
var body = d3.select("body")
var svg = d3.svg.container(body, {width: width, height: height, margin: margin}, clip = true)
/*
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
...