Skip to content

Commit ff41d70

Browse files
authored
Merge pull request #5836 from plotly/fix5822-hover-position
Fix scatter and scattergl hover on axes with period and adjust the unified hover position in respect to mean
2 parents f1d4f48 + 17215eb commit ff41d70

File tree

7 files changed

+328
-150
lines changed

7 files changed

+328
-150
lines changed

draftlogs/5836_fix.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fix `scatter` and `scattergl` hover on axes with period and adjust the unified hover position in respect to mean [[#5836](https://github.com/plotly/plotly.js/pull/5836)]

src/components/fx/hover.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1072,8 +1072,11 @@ function createHoverText(hoverData, opts, gd) {
10721072

10731073
// Position the hover
10741074
var winningPoint = hoverData[0];
1075-
var ly = (winningPoint.y0 + winningPoint.y1) / 2;
1076-
var lx = (winningPoint.x0 + winningPoint.x1) / 2;
1075+
var ly = axLetter === 'y' ?
1076+
(winningPoint.y0 + winningPoint.y1) / 2 : Lib.mean(hoverData.map(function(c) {return (c.y0 + c.y1) / 2;}));
1077+
var lx = axLetter === 'x' ?
1078+
(winningPoint.x0 + winningPoint.x1) / 2 : Lib.mean(hoverData.map(function(c) {return (c.x0 + c.x1) / 2;}));
1079+
10771080
var legendContainer = container.select('g.legend');
10781081
var tbb = legendContainer.node().getBoundingClientRect();
10791082
lx += xa._offset;

src/traces/scatter/calc.js

-10
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,13 @@ function calc(gd, trace) {
5151
calcAxisExpansion(gd, trace, xa, ya, x, y, ppad);
5252
}
5353

54-
var hasPeriodX = !!trace.xperiodalignment;
55-
var hasPeriodY = !!trace.yperiodalignment;
56-
5754
for(i = 0; i < serieslen; i++) {
5855
var cdi = cd[i] = {};
5956
var xValid = isNumeric(x[i]);
6057
var yValid = isNumeric(y[i]);
6158
if(xValid && yValid) {
6259
cdi[xAttr] = x[i];
6360
cdi[yAttr] = y[i];
64-
65-
if(hasPeriodX) {
66-
cdi.orig_x = origX[i]; // used by hover
67-
}
68-
if(hasPeriodY) {
69-
cdi.orig_y = origY[i]; // used by hover
70-
}
7161
} else if(stackGroupOpts && (isV ? xValid : yValid)) {
7262
// if we're stacking we need to hold on to all valid positions
7363
// even with invalid sizes

src/traces/scatter/hover.js

+2-4
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) {
@@ -71,8 +69,8 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
7169
// the normalized individual sizes, so that's what I'm doing here
7270
// for now.
7371
var sizeVal = orientation && (di.sNorm || di.s);
74-
var xLabelVal = (orientation === 'h') ? sizeVal : di.orig_x !== undefined ? di.orig_x : di.x;
75-
var yLabelVal = (orientation === 'v') ? sizeVal : di.orig_y !== undefined ? di.orig_y : di.y;
72+
var xLabelVal = (orientation === 'h') ? sizeVal : di.x;
73+
var yLabelVal = (orientation === 'v') ? sizeVal : di.y;
7674

7775
Lib.extendFlat(pointData, {
7876
color: getTraceColor(trace, di),

src/traces/scattergl/hover.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,9 @@ function hoverPoints(pointData, xval, yval, hovermode) {
4848
for(i = 0; i < ids.length; i++) {
4949
ptx = x[ids[i]];
5050
dx = Math.abs(xa.c2p(ptx) - xpx);
51-
if(trace._origX && trace._origX[i] !== undefined) dx += xa.c2p(trace._origX[i]) - xa.c2p(ptx);
5251
if(dx < minDist) {
5352
minDist = dx;
5453
dy = ya.c2p(y[ids[i]]) - ypx;
55-
if(trace._origY && trace._origY[i] !== undefined) dy += ya.c2p(trace._origY[i]) - ya.c2p(pty);
5654
dxy = Math.sqrt(dx * dx + dy * dy);
5755
id = ids[i];
5856
}
@@ -155,19 +153,16 @@ function calcHover(pointData, x, y, trace) {
155153
var fakeCd = {};
156154
fakeCd[pointData.index] = di;
157155

158-
var origX = trace._origX;
159-
var origY = trace._origY;
160-
161156
var pointData2 = Lib.extendFlat({}, pointData, {
162157
color: getTraceColor(trace, di),
163158

164159
x0: xp - rad,
165160
x1: xp + rad,
166-
xLabelVal: origX ? origX[id] : di.x,
161+
xLabelVal: di.x,
167162

168163
y0: yp - rad,
169164
y1: yp + rad,
170-
yLabelVal: origY ? origY[id] : di.y,
165+
yLabelVal: di.y,
171166

172167
cd: fakeCd,
173168
distance: minDist,

test/jasmine/tests/gl2d_click_test.js

+50
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,12 @@ describe('hover with (x|y)period positioning', function() {
627627

628628
it('@gl shows hover info for scattergl', function(done) {
629629
Plotly.newPlot(gd, require('@mocks/gl2d_period_positioning.json'))
630+
.then(function() {
631+
return Plotly.restyle(gd, {
632+
xperiodalignment: 'start',
633+
yperiodalignment: 'start'
634+
});
635+
})
630636
.then(function() { _hover(100, 255); })
631637
.then(function() {
632638
assertHoverLabelContent({
@@ -641,6 +647,50 @@ describe('hover with (x|y)period positioning', function() {
641647
nums: '(Jan 2006, Jun 1, 1970)'
642648
});
643649
})
650+
651+
.then(function() {
652+
return Plotly.restyle(gd, {
653+
xperiodalignment: 'middle',
654+
yperiodalignment: 'middle'
655+
});
656+
})
657+
.then(function() { _hover(100, 255); })
658+
.then(function() { _hover(100, 255); })
659+
.then(function() {
660+
assertHoverLabelContent({
661+
name: '',
662+
nums: '(Jul 2001, Jan 16, 1970)'
663+
});
664+
})
665+
.then(function() { _hover(470, 45); })
666+
.then(function() {
667+
assertHoverLabelContent({
668+
name: '',
669+
nums: '(Jul 2006, Jun 16, 1970)'
670+
});
671+
})
672+
673+
.then(function() {
674+
return Plotly.restyle(gd, {
675+
xperiodalignment: 'end',
676+
yperiodalignment: 'end'
677+
});
678+
})
679+
.then(function() { _hover(100, 255); })
680+
.then(function() {
681+
assertHoverLabelContent({
682+
name: '',
683+
nums: '(Jan 2002, Feb 1, 1970)'
684+
});
685+
})
686+
.then(function() { _hover(470, 45); })
687+
.then(function() {
688+
assertHoverLabelContent({
689+
name: '',
690+
nums: '(Jan 2007, Jul 1, 1970)'
691+
});
692+
})
693+
644694
.then(done, done.fail);
645695
});
646696
});

0 commit comments

Comments
 (0)