Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make hexbin locations deterministic #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/js/hexbin/HexbinLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,18 @@ L.HexbinLayer = L.SVG.extend({
that._scale.color.domain(colorDomain);
that._scale.radius.domain(radiusDomain);

// Get location of Pixel Origin (which is the reference point of hexbin draw layer).
// This is used to transform the absolute bin positions
// to the relative coordinate system of Leaflet custom layers
var pixelOrigin = that._map.getPixelOrigin();

/*
* Join
* Join the Hexagons to the data
* Use a deterministic id for tracking bins based on position
*/
bins = bins.filter(function(d) {
return bounds.contains(that._map.layerPointToLatLng(L.point(d.x, d.y)));
return bounds.contains(that._map.layerPointToLatLng(L.point(d.x - pixelOrigin.x, d.y - pixelOrigin.y)));
});
var join = g.selectAll('g.hexbin-container')
.data(bins, function(d) {
Expand Down Expand Up @@ -252,7 +256,7 @@ L.HexbinLayer = L.SVG.extend({

enter.append('path').attr('class', 'hexbin-hexagon')
.attr('transform', function(d) {
return 'translate(' + d.x + ',' + d.y + ')';
return 'translate(' + (d.x - pixelOrigin.x) + ',' + (d.y - pixelOrigin.y) + ')';
})
.attr('d', function(d) {
return that._hexLayout.hexagon(that._scale.radius.range()[0]);
Expand All @@ -270,7 +274,7 @@ L.HexbinLayer = L.SVG.extend({
// Grid
var gridEnter = enter.append('path').attr('class', 'hexbin-grid')
.attr('transform', function(d) {
return 'translate(' + d.x + ',' + d.y + ')';
return 'translate(' + (d.x - pixelOrigin.x) + ',' + (d.y - pixelOrigin.y) + ')';
})
.attr('d', function(d) {
return that._hexLayout.hexagon(that.options.radius);
Expand Down Expand Up @@ -328,7 +332,8 @@ L.HexbinLayer = L.SVG.extend({
},

_project : function(coord) {
var point = this._map.latLngToLayerPoint([ coord[1], coord[0] ]);
// Get (absolute) pixel location relative to the map's CRS
var point = this._map.project([ coord[1], coord[0] ]);
return [ point.x, point.y ];
},

Expand Down