A demonstration of the d3-hexbin plugin for D3 4.0.
xxxxxxxxxx
<meta charset="utf-8">
<style>
html, body {
background: #111;
}
.hexagons {
fill: none;
stroke: #000;
}
</style>
<body>
<script src="//d3js.org/d3.v4.js"></script>
<script src="d3-hexbin.js"></script>
<script>
var margin = {top: 12, right: 12, bottom: 12, left: 12},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var randomX = d3.randomBates(2),
randomY = d3.randomBates(2),
points = d3.range(2000).map(function() { return [width * randomX(), height * randomY()]; });
var hexbin = d3.hexbin()
.extent([[-margin.left, -margin.top], [width + margin.right, height + margin.bottom]])
.radius(10);
var bins = hexbin(points);
var color = d3.scaleSequential(d3.interpolatePlasma)
.domain([0, 10]);
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 + ")");
svg.append("g")
.attr("class", "hexagons")
.selectAll("path")
.data(bins)
.enter().append("path")
.attr("d", hexbin.hexagon())
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.style("fill", function(d) { return color(d.length); });
/* Messing with bl.ocks.org styles */
if(window.parent && window.parent.document) {
window.parent.document.body.style.backgroundColor = "#111"
window.parent.document.body.style.color = "#fff"
d3.selectAll(window.parent.document.getElementsByTagName("a")).style("color", "rgb(244, 136, 73)");
}
</script>
https://d3js.org/d3.v4.js