Skip to content

Commit 63d181a

Browse files
committed
Display hover label at cursor position as fallback for scatter traces.
For curved edges of fills there is a chance that the hover detection polygons miss a point and the correct hover label position cannot be determined. Previous code fell back to positioning the label at the largest edge of any polygon of the trace of their combined vertical midpoint, with undetermined behaviour if no polygon overlapped the midpoint. This change instead falls back to simply displaying the label at the current cursor position, which simplifies code and results in less confusing label placement.
1 parent 3eb4738 commit 63d181a

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/traces/scatter/hover.js

+14-11
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,12 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
151151
var xmax = -Infinity;
152152
var ymin = Infinity;
153153
var ymax = -Infinity;
154-
var yminAll = Infinity;
155-
var ymaxAll = -Infinity;
156154
var yPos;
157155

158156
for(i = 0; i < polygons.length; i++) {
159157
var polygon = polygons[i];
160158
// This is not going to work right for curved or jagged edges, it will
161159
// act as though they're straight.
162-
yminAll = Math.min(yminAll, polygon.ymin);
163-
ymaxAll = Math.max(ymaxAll, polygon.ymax);
164160
if(polygon.contains(pt)) {
165161
polygonsIn.push(polygon);
166162
ymin = Math.min(ymin, polygon.ymin);
@@ -170,14 +166,9 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
170166

171167
// The above found no polygon that contains the cursor, but we know that
172168
// the cursor must be inside the fill as determined by the SVGElement
173-
// (so we are probably close to a curved/jagged edge...). In this case
174-
// as a crude approximation, simply consider all polygons for determination
175-
// of the hover label position.
176-
// TODO: This might cause some jumpiness of the label close to edges...
169+
// (so we are probably close to a curved/jagged edge...).
177170
if(polygonsIn.length === 0) {
178-
polygonsIn = polygons;
179-
ymin = yminAll;
180-
ymax = ymaxAll;
171+
return null;
181172
}
182173

183174
// constrain ymin/max to the visible plot, so the label goes
@@ -229,6 +220,18 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
229220
if(inside) {
230221
var hoverLabelCoords = getHoverLabelPosition(trace._polygons);
231222

223+
// getHoverLabelPosition may return null if the cursor / hover point is not contained
224+
// in any of the trace's polygons, which can happen close to curved edges. in that
225+
// case we fall back to displaying the hover label at the cursor position.
226+
if(hoverLabelCoords === null) {
227+
hoverLabelCoords = {
228+
x0: pt[0],
229+
x1: pt[0],
230+
y0: pt[1],
231+
y1: pt[1]
232+
};
233+
}
234+
232235
// get only fill or line color for the hover color
233236
var color = Color.defaultLine;
234237
if(Color.opacity(trace.fillcolor)) color = trace.fillcolor;

0 commit comments

Comments
 (0)