Skip to content

Commit 53fea8e

Browse files
committed
fix hover on scatter and scattergl with periods
1 parent b4bdec3 commit 53fea8e

File tree

4 files changed

+68
-14
lines changed

4 files changed

+68
-14
lines changed

src/components/fx/hover.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1934,7 +1934,10 @@ function getCoord(axLetter, winningPoint, fullLayout) {
19341934
var val = winningPoint[axLetter + 'Val'];
19351935

19361936
if(ax.type === 'category') val = ax._categoriesMap[val];
1937-
else if(ax.type === 'date') val = ax.d2c(val);
1937+
else if(ax.type === 'date') {
1938+
var period = winningPoint[axLetter + 'Period'];
1939+
val = ax.d2c(period !== undefined ? period : val);
1940+
}
19381941

19391942
var cd0 = winningPoint.cd[winningPoint.index];
19401943
if(cd0 && cd0.t && cd0.t.posLetter === ax._id) {

src/traces/scatter/hover.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,12 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
2828
var rad = Math.max(3, di.mrc || 0);
2929
var kink = 1 - 1 / rad;
3030
var dxRaw = Math.abs(xa.c2p(di.x) - xpx);
31-
if(di.orig_x !== undefined) dxRaw += xa.c2p(di.orig_x) - xa.c2p(di.x);
3231
return (dxRaw < rad) ? (kink * dxRaw / rad) : (dxRaw - rad + kink);
3332
};
3433
var dy = function(di) {
3534
var rad = Math.max(3, di.mrc || 0);
3635
var kink = 1 - 1 / rad;
3736
var dyRaw = Math.abs(ya.c2p(di.y) - ypx);
38-
if(di.orig_y !== undefined) dyRaw += ya.c2p(di.orig_y) - ya.c2p(di.y);
3937
return (dyRaw < rad) ? (kink * dyRaw / rad) : (dyRaw - rad + kink);
4038
};
4139
var dxy = function(di) {
@@ -89,6 +87,9 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
8987
hovertemplate: trace.hovertemplate
9088
});
9189

90+
if(di.orig_x !== undefined) pointData.xPeriod = di.x;
91+
if(di.orig_y !== undefined) pointData.yPeriod = di.y;
92+
9293
fillText(di, trace, pointData);
9394
Registry.getComponentMethod('errorbars', 'hoverInfo')(di, trace, pointData);
9495

src/traces/scattergl/hover.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,42 +41,43 @@ function hoverPoints(pointData, xval, yval, hovermode) {
4141

4242
// pick the id closest to the point
4343
// note that point possibly may not be found
44-
var id, ptx, pty, i, dx, dy, dist, dxy;
44+
var k, closestId, ptx, pty, i, dx, dy, dist, dxy;
4545

4646
var minDist = maxDistance;
4747
if(hovermode === 'x') {
4848
for(i = 0; i < ids.length; i++) {
49-
ptx = x[ids[i]];
49+
k = ids[i];
50+
ptx = x[k];
5051
dx = Math.abs(xa.c2p(ptx) - xpx);
51-
if(trace._origX && trace._origX[i] !== undefined) dx += xa.c2p(trace._origX[i]) - xa.c2p(ptx);
5252
if(dx < minDist) {
5353
minDist = dx;
54-
dy = ya.c2p(y[ids[i]]) - ypx;
55-
if(trace._origY && trace._origY[i] !== undefined) dy += ya.c2p(trace._origY[i]) - ya.c2p(pty);
54+
pty = y[k];
55+
dy = ya.c2p(pty) - ypx;
5656
dxy = Math.sqrt(dx * dx + dy * dy);
57-
id = ids[i];
57+
closestId = ids[i];
5858
}
5959
}
6060
} else {
6161
for(i = ids.length - 1; i > -1; i--) {
62-
ptx = x[ids[i]];
63-
pty = y[ids[i]];
62+
k = ids[i];
63+
ptx = x[k];
64+
pty = y[k];
6465
dx = xa.c2p(ptx) - xpx;
6566
dy = ya.c2p(pty) - ypx;
6667

6768
dist = Math.sqrt(dx * dx + dy * dy);
6869
if(dist < minDist) {
6970
minDist = dxy = dist;
70-
id = ids[i];
71+
closestId = k;
7172
}
7273
}
7374
}
7475

75-
pointData.index = id;
76+
pointData.index = closestId;
7677
pointData.distance = minDist;
7778
pointData.dxy = dxy;
7879

79-
if(id === undefined) return [pointData];
80+
if(closestId === undefined) return [pointData];
8081

8182
return [calcHover(pointData, x, y, trace)];
8283
}
@@ -176,6 +177,9 @@ function calcHover(pointData, x, y, trace) {
176177
hovertemplate: di.ht
177178
});
178179

180+
if(origX) pointData2.xPeriod = di.x;
181+
if(origY) pointData2.yPeriod = di.y;
182+
179183
if(di.htx) pointData2.text = di.htx;
180184
else if(di.tx) pointData2.text = di.tx;
181185
else if(trace.text) pointData2.text = trace.text;

test/jasmine/tests/hover_label_test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5407,6 +5407,52 @@ describe('hovermode: (x|y)unified', function() {
54075407
});
54085408
});
54095409

5410+
it('two end positioned scatter period', function(done) {
5411+
var fig = {
5412+
data: [{
5413+
x: [
5414+
'1970-01-01',
5415+
'1970-07-01',
5416+
'1971-01-01'
5417+
],
5418+
xperiod: 'M6',
5419+
xperiodalignment: 'end',
5420+
y: [1, 2, 3]
5421+
}, {
5422+
x: [
5423+
'1970-01-01',
5424+
'1970-07-01',
5425+
'1971-01-01',
5426+
],
5427+
xperiod: 'M6',
5428+
xperiodalignment: 'end',
5429+
y: [11, 12, 13]
5430+
}],
5431+
layout: {
5432+
showlegend: false,
5433+
width: 600,
5434+
height: 400,
5435+
hovermode: 'x unified'
5436+
}
5437+
};
5438+
5439+
Plotly.newPlot(gd, fig)
5440+
.then(function(gd) {
5441+
_hover(gd, { xpx: 200, ypx: 200 });
5442+
assertLabel({title: 'Jul 1, 1970', items: [
5443+
'trace 0 : 2',
5444+
'trace 1 : 12'
5445+
]});
5446+
5447+
_hover(gd, { xpx: 400, ypx: 200 });
5448+
assertLabel({title: 'Jan 1, 1971', items: [
5449+
'trace 0 : 3',
5450+
'trace 1 : 13'
5451+
]});
5452+
})
5453+
.then(done, done.fail);
5454+
});
5455+
54105456
it('period with hover distance -1 include closest not farthest', function(done) {
54115457
Plotly.newPlot(gd, {
54125458
data: [

0 commit comments

Comments
 (0)