I'll use this example to build my answer. This zoomable example, as opposed to this example one, is zoomable.
Projection Scale
First, I'll just explain the scale using the above example. It uses a projection that has a starting scale of 1/tau
: the 2 π radians of the world are stretched over one pixel. The translate is [0,0], so that 0°N, 0°E is at [0,0] of the SVG. The scale and translate of the map is managed by d3.zoom:
projection.scale(transform.k / Math.PI / 2)
.translate([transform.x, transform.y]);
As k
represents the zoom factor, and our starting map width was 1, k
represents map width and height. Dividing by tau we get how many pixels of the map correspond to each radian of the earth. The translate centers the map.
The reason why you can't see any hexbins in your example is because you use a scale of that stretches the earth over and area 4194304 pixels wide (1<<22), but your hexbins only stretch over an area the size of your SVG. You see no hexagons because the extent of your SVG represents only a small geographic extent - some area of ocean north of the Bering Sea.
Also for reference: The relationship between map scale and map width is not consistent across all map projections
Adding hexbins (fixed)
If we want hexagonal binning with bins that remain the same geographical size regardless of zoom, we can set the radius and extent to reflect our initial projection (before applying the zoom):
var hexbin = d3.hexbin()
.radius(0.01)
.extent([[-0.5, -0.5], [0.5, 0.5]]);
We can then pass it projected points, using the initial projection, and transform the hexagons based on the zoom while scaling the stroke width of the hexagons based on zoom scale:
var width = Math.max(960, window.innerWidth),
height = Math.max(500, window.innerHeight);
var svg = d3.select("svg")
.attr("width", width)
.attr("height", height);
// Projection details:
var projection = d3.geoMercator()
.scale(1 / Math.PI / 2)
.translate([0, 0]);
var center = projection([0,0]);
var tile = d3.tile()
.size([width, height]);
// Zoom details:
var zoom = d3.zoom()
.scaleExtent([1 << 11, 1 << 14])
.on("zoom", zoomed);
// Layers for map
var raster = svg.append("g"); // holds tiles
var vector = svg.append("g"); // holds hexagons
var hexes; // to hold hexagons
// Hexbin:
var hexbin = d3.hexbin()
.radius(0.01)
.extent([[-0.5, -0.5], [0.5, 0.5]]); // extent of the one pixel projection.
var color = d3.scaleLinear()
.range(["rgba(255,255,255,0.1)","orange"])
.domain([0, 5]);
d3.json("https://unpkg.com/world-atlas@1/world/110m.json", function(error, world) {
// Create some hexbin data:
var land = topojson.feature(world, world.objects.land);
var data = d3.range(500).map(function(d) {
while(true) {
var lat = Math.random() * 170 - 70;
var lon = Math.random() * 360 - 180;
if(d3.geoContains(land,[lon,lat])) return projection([lon,lat]);
}
})
// Create hex bins:
hexes = vector.selectAll()
.data(hexbin(data))
.enter()
.append("path")
.attr("d", hexbin.hexagon(0.0085))
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.attr("fill", function(d) { return color(d.length); })
.attr("stroke","black")
svg
.call(zoom)
.call(zoom.transform, d3.zoomIdentity
.translate(width / 2, height / 2)
.scale(1 << 11)
.translate(-center[0], -center[1]));
});
function zoomed() {
var transform = d3.event.transform;
var tiles = tile
.scale(transform.k)
.translate([transform.x, transform.y])
();
// Update projection
projection
.scale(transform.k / Math.PI / 2)
.translate([transform.x, transform.y]);
// Update vector holding hexes:
vector.attr("transform","translate("+[transform.x,transform.y]+")scale("+transform.k+")" )
.attr("stroke-width", 1/transform.k);
// Update tiles:
var image = raster
.attr("transform", stringify(tiles.scale, tiles.translate))
.selectAll("image")
.data(tiles, function(d) { return d; });
image.exit().remove();
image.enter().append("image")
.attr("xlink:href", function(d) { return "http://" + "abc"[d[1] % 3] + ".tile.openstreetmap.org/" + d[2] + "/" + d[0] + "/" + d[1] + ".png"; })
.attr("x", function(d) { return d[0] * 256; })
.attr("y", function(d) { return d[1] * 256; })
.attr("width", 256)
.attr("height", 256);
}
function stringify(scale, translate) {
var k = scale / 256, r = scale % 1 ? Number : Math.round;
return "translate(" + r(translate[0] * scale) + "," + r(translate[1] * scale) + ") scale(" + k + ")";
}
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-tile.v0.0.min.js"></script>
<script src="https://d3js.org/d3-hexbin.v0.2.min.js"></script>
<script src="https://unpkg.com/topojson-client@3"></script>
<svg></svg>
Adding hexbins (updated with zoom)
However, if we want to change the bins scale as we zoom in the code is potentially a bit easier, but computationally more complex. To do so we recalculate the hexbins each zoom based on the projected coordinates of the points after applying the current projection:
var hexbin = d3.hexbin()
.radius(30)
.extent([[0,0], [width,height]]) // extent of projected data (displayed)
.x(function(d) { return projection(d)[0]; })
.y(function(d) { return projection(d)[1]; })
The extent and radius reflect the entire SVG extent - the visible extent to which we are projecting the data after the zoom is applied - the extent we want to add hexagons to. Below I recalculate the hexes each zoom/pan:
var width = Math.max(960, window.innerWidth),
height = Math.max(500, window.innerHeight);
var svg = d3.select("svg")
.attr("width", width)
.attr("height", height);
// Projection details:
var projection = d3.geoMercator()
.scale(1 / Math.PI / 2)
.translate([0, 0]);
var center = projection([0,0]);
var tile = d3.tile()
.size([width, height]);
// Zoom details:
var zoom = d3.zoom()
.scaleExtent([1 << 11, 1 << 14])
.on("zoom", zoomed);
// Layers for map
var raster = svg.append("g"); // holds tiles
var vector = svg.append("g"); // holds hexagons
var hexes; // to hold hexagons
// Hexbin:
var hexbin = d3.hexbin()
.radius(30)
.extent([[0,0], [width,height]]) // extent of projected data (displayed)
.x(function(d) { return projection(d)[0]; })
.y(function(d) { return projection(d)[1]; })
var color = d3.scaleLinear()
.range(["rgba(255,255,255,0.1)","orange"])
.domain([0, 5]);
var data;
d3.json("https://unpkg.com/world-atlas@1/world/110m.json", function(error, world) {
// Create some hexbin data:
var land = topojson.feature(world, world.objects.land);
data = d3.range(500).map(function(d) {
while(true) {
var lat = Math.random() * 170 - 70;
var lon = Math.random() * 360 - 180;
if(d3.geoContains(land,[lon,lat])) return [lon,lat];
}
})
svg
.call(zoom)
.call(zoom.transform, d3.zoomIdentity
.translate(width / 2, height / 2)
.scale(1 << 11)
.translate(-center[0], -center[1]));
});
function zoomed() {
var transform = d3.event.transform;
var tiles = tile
.scale(transform.k)
.translate([transform.x, transform.y])
();
// Update projection
projection
.scale(transform.k / Math.PI / 2)
.translate([transform.x, transform.y]);
hexes = vector.selectAll("path")
.data(hexbin(data)) ;
hexes.exit().remove();
hexes.enter()
.append("path")
.merge(hexes)
.attr("d", hexbin.hexagon(29))
.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
.attr("fill", function(d) { return color(d.length); })
.attr("stroke","black")
// Update tiles:
var image = raster
.attr("transform", stringify(tiles.scale, tiles.translate))
.selectAll("image")
.data(tiles, function(d) { return d; });
image.exit().remove();
image.enter().append("image")
.attr("xlink:href", function(d) { return "http://" + "abc"[d[1] % 3] + ".tile.openstreetmap.org/" + d[2] + "/" + d[0] + "/" + d[1] + ".png"; })
.attr("x", function(d) { return d[0] * 256; })
.attr("y", function(d) { return d[1] * 256; })
.attr("width", 256)
.attr("height", 256);
}
function stringify(scale, translate) {
var k = scale / 256, r = scale % 1 ? Number : Math.round;
return "translate(" + r(translate[0] * scale) + "," + r(translate[1] * scale) + ") scale(" + k + ")";
}
<svg></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-tile.v0.0.min.js"></script>
<script src="https://d3js.org/d3-hexbin.v0.2.min.js"></script>
<script src="https://unpkg.com/topojson-client@3"></script>
Both examples randomly create some data over land masses, this is the primary reason for the slow load
Last thoughts
Both examples leave a fair amount to be desired, the method of organizing coordinate spaces with d3-tile and the hexagons is a bit less intuitive than possible - and can take a bit to get used to. But, for the moment there aren't a lot of alternatives.