Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1c26ec4

Browse files
committed
add geo.project and use that in scattergeo hover
... to not confuse positions on either side of the globe
1 parent 9bb7d91 commit 1c26ec4

File tree

3 files changed

+56
-20
lines changed

3 files changed

+56
-20
lines changed

src/plots/geo/geo.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -447,18 +447,22 @@ proto.makeFramework = function() {
447447
.attr('class', 'geo ' + _this.id)
448448
.call(Drawing.setClipUrl, clipId);
449449

450+
// sane lonlat to px
451+
_this.project = function(v) {
452+
var px = _this.projection(v);
453+
return px ?
454+
[px[0] - _this.xaxis._offset, px[1] - _this.yaxis._offset] :
455+
[null, null];
456+
};
457+
450458
_this.xaxis = {
451459
_id: 'x',
452-
c2p: function(v) {
453-
return (_this.projection(v) || [])[0] - _this.xaxis._offset;
454-
}
460+
c2p: function(v) { return _this.project(v)[0]; }
455461
};
456462

457463
_this.yaxis = {
458464
_id: 'y',
459-
c2p: function(v) {
460-
return (_this.projection(v) || [])[1] - _this.yaxis._offset;
461-
}
465+
c2p: function(v) { return _this.project(v)[1]; }
462466
};
463467

464468
// mock axis for hover formatting

src/traces/scattergeo/hover.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,25 @@ var attributes = require('./attributes');
1818

1919

2020
module.exports = function hoverPoints(pointData, xval, yval) {
21-
var cd = pointData.cd,
22-
trace = cd[0].trace,
23-
xa = pointData.xa,
24-
ya = pointData.ya,
25-
geo = pointData.subplot,
26-
projection = geo.projection;
21+
var cd = pointData.cd;
22+
var trace = cd[0].trace;
23+
var xa = pointData.xa;
24+
var ya = pointData.ya;
25+
var geo = pointData.subplot;
26+
27+
var isLonLatOverEdges = geo.projection.isLonLatOverEdges;
28+
var project = geo.project;
2729

2830
function distFn(d) {
2931
var lonlat = d.lonlat;
3032

3133
if(lonlat[0] === BADNUM) return Infinity;
32-
if(projection.isLonLatOverEdges(lonlat)) return Infinity;
33-
34-
var dx = Math.abs(xa.c2p(lonlat) - xa.c2p([xval, lonlat[1]]));
35-
var dy = Math.abs(ya.c2p(lonlat) - ya.c2p([lonlat[0], yval]));
34+
if(isLonLatOverEdges(lonlat)) return Infinity;
3635

36+
var pt = project(lonlat);
37+
var px = project([xval, yval]);
38+
var dx = Math.abs(pt[0] - px[0]);
39+
var dy = Math.abs(pt[1] - px[1]);
3740
var rad = Math.max(3, d.mrc || 0);
3841

3942
// N.B. d.mrc is the calculated marker radius
@@ -47,10 +50,10 @@ module.exports = function hoverPoints(pointData, xval, yval) {
4750
// skip the rest (for this trace) if we didn't find a close point
4851
if(pointData.index === false) return;
4952

50-
var di = cd[pointData.index],
51-
lonlat = di.lonlat,
52-
pos = [xa.c2p(lonlat), ya.c2p(lonlat)],
53-
rad = di.mrc || 1;
53+
var di = cd[pointData.index];
54+
var lonlat = di.lonlat;
55+
var pos = [xa.c2p(lonlat), ya.c2p(lonlat)];
56+
var rad = di.mrc || 1;
5457

5558
pointData.x0 = pos[0] - rad;
5659
pointData.x1 = pos[0] + rad;

test/jasmine/tests/geo_test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,35 @@ describe('Test geo interactions', function() {
12021202
.then(done);
12031203
});
12041204

1205+
it('should not confuse positions on either side of the globe', function(done) {
1206+
var gd = createGraphDiv();
1207+
var fig = Lib.extendDeep({}, require('@mocks/geo_orthographic.json'));
1208+
1209+
fig.data[0].visible = false;
1210+
fig.layout.geo.projection.rotation = {lon: -75, lat: 90};
1211+
1212+
function check(p, hoverLabelCnt) {
1213+
mouseEvent('mousemove', p[0], p[1]);
1214+
1215+
var invert = gd._fullLayout.geo._subplot.projection.invert;
1216+
var lonlat = invert(p);
1217+
1218+
expect(d3.selectAll('g.hovertext').size())
1219+
.toBe(hoverLabelCnt, 'for ' + lonlat);
1220+
1221+
delete gd._lastHoverTime;
1222+
}
1223+
1224+
Plotly.plot(gd, fig).then(function() {
1225+
var px = 255;
1226+
1227+
check([px, 163], 0);
1228+
check([px, 360], 1);
1229+
})
1230+
.catch(fail)
1231+
.then(done);
1232+
});
1233+
12051234
});
12061235

12071236
describe('Test event property of interactions on a geo plot:', function() {

0 commit comments

Comments
 (0)