Skip to content

Commit a4b771c

Browse files
committed
fix #3962 - don't build pointgroup array using d3-data indices
- N.B. in the case where multiple item in a given d3-data bound array have identical key-function results, *only the first* of those items is considered. This implies that the `i` in e.g `selection.each(function(d, i) { })` does not always increment with a constant step.
1 parent 0e8d53e commit a4b771c

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/components/fx/hover.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,8 @@ function createHoverText(hoverData, opts, gd) {
842842
// first create the objects
843843
var hoverLabels = container.selectAll('g.hovertext')
844844
.data(hoverData, function(d) {
845+
// N.B. when multiple items have the same result key-function value,
846+
// only the first of those items in hoverData gets rendered
845847
return [d.trace.index, d.index, d.x0, d.y0, d.name, d.attr, d.xa, d.ya || ''].join(',');
846848
});
847849
hoverLabels.enter().append('g')
@@ -1061,18 +1063,18 @@ function hoverAvoidOverlaps(hoverLabels, axKey, fullLayout) {
10611063

10621064
// make groups of touching points
10631065
var pointgroups = new Array(nLabels);
1066+
var k = 0;
10641067

1065-
hoverLabels.each(function(d, i) {
1068+
hoverLabels.each(function(d) {
10661069
var ax = d[axKey];
10671070
var axIsX = ax._id.charAt(0) === 'x';
10681071
var rng = ax.range;
10691072

1070-
if(!i && rng && ((rng[0] > rng[1]) !== axIsX)) {
1073+
if(k === 0 && rng && ((rng[0] > rng[1]) !== axIsX)) {
10711074
axSign = -1;
10721075
}
1073-
pointgroups[i] = [{
1076+
pointgroups[k++] = [{
10741077
datum: d,
1075-
i: i,
10761078
traceIndex: d.trace.index,
10771079
dp: 0,
10781080
pos: d.pos,

test/jasmine/tests/box_test.js

+19
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,25 @@ describe('Test box hover:', function() {
437437
},
438438
nums: '0.6',
439439
name: 'pt #0'
440+
}, {
441+
desc: 'when zoomed in, cropping out labels',
442+
mock: {
443+
data: [{
444+
type: 'box',
445+
y: [1, 2, 2, 3]
446+
}],
447+
layout: {
448+
// cropping out q1 and max w/o failing during hoverAvoidOverlap
449+
// https://github.com/plotly/plotly.js/issues/3962
450+
yaxis: {range: [1.6, 2.4]},
451+
width: 400,
452+
height: 400
453+
}
454+
},
455+
pos: [200, 200],
456+
nums: ['median: 2', 'min: 1', 'q3: 2.5'],
457+
name: ['', '', ''],
458+
axis: 'trace 0'
440459
}].forEach(function(specs) {
441460
it('should generate correct hover labels ' + specs.desc, function(done) {
442461
run(specs).catch(failTest).then(done);

0 commit comments

Comments
 (0)