Skip to content

Commit f9652c2

Browse files
committed
allow hover labels to extend to the edges of the plot
not get cut off at the subplot edges box and violin tests had some labels clipped before - now they all get drawn 🎉
1 parent 0dc2eb7 commit f9652c2

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

src/components/fx/hover.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ function _hover(gd, evt, subplot, noHoverEvent) {
452452

453453
var hoverLabels = createHoverText(hoverData, labelOpts, gd);
454454

455-
hoverAvoidOverlaps(hoverData, rotateLabels ? 'xa' : 'ya');
455+
hoverAvoidOverlaps(hoverData, rotateLabels ? 'xa' : 'ya', fullLayout);
456456

457457
alignHoverText(hoverLabels, rotateLabels);
458458

@@ -791,7 +791,7 @@ function createHoverText(hoverData, opts, gd) {
791791
// know what happens if the group spans all the way from one edge to
792792
// the other, though it hardly matters - there's just too much
793793
// information then.
794-
function hoverAvoidOverlaps(hoverData, ax) {
794+
function hoverAvoidOverlaps(hoverData, ax, fullLayout) {
795795
var nummoves = 0,
796796

797797
// make groups of touching points
@@ -804,8 +804,8 @@ function hoverAvoidOverlaps(hoverData, ax) {
804804
pos: d.pos,
805805
posref: d.posref,
806806
size: d.by * (axis._id.charAt(0) === 'x' ? YFACTOR : 1) / 2,
807-
pmin: axis._offset,
808-
pmax: axis._offset + axis._length
807+
pmin: 0,
808+
pmax: (axis._id.charAt(0) === 'x' ? fullLayout.width : fullLayout.height)
809809
}];
810810
})
811811
.sort(function(a, b) { return a[0].posref - b[0].posref; }),

test/jasmine/tests/box_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,11 @@ describe('Test box hover:', function() {
223223
return fig;
224224
},
225225
nums: [
226-
'q1: 0.3', 'median: 0.45', 'q3: 0.6', 'max: 1', 'median: 0.55', 'min: 0.2',
226+
'q1: 0.3', 'median: 0.45', 'q3: 0.6', 'max: 1', 'median: 0.55', 'min: 0', 'min: 0.2',
227227
'q3: 0.6', 'max: 0.7', 'median: 0.45', 'min: 0.1', 'q3: 0.6', 'max: 0.9'
228228
],
229229
name: [
230-
'', 'kale', '', '', 'radishes', '',
230+
'', 'kale', '', '', 'radishes', '', '',
231231
'', '', 'carrots', '', '', ''
232232
],
233233
axis: 'day 1'

test/jasmine/tests/hover_label_test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,44 @@ describe('hover info', function() {
966966
.then(done);
967967
});
968968
});
969+
970+
describe('overflowing hover labels', function() {
971+
var trace = {y: [1, 2, 3], text: ['', 'a<br>b<br>c', '']};
972+
var data = [trace, trace, trace, trace, trace, trace, trace];
973+
var layout = {
974+
width: 600, height: 600, showlegend: false,
975+
margin: {l: 100, r: 100, t: 100, b: 100},
976+
hovermode: 'x'
977+
};
978+
979+
var gd;
980+
981+
beforeEach(function(done) {
982+
gd = createGraphDiv();
983+
Plotly.plot(gd, data, layout).then(done);
984+
});
985+
986+
function labelCount() {
987+
return d3.select(gd).selectAll('g.hovertext').size();
988+
}
989+
990+
it('shows as many labels as will fit on the div, not on the subplot', function(done) {
991+
_hoverNatural(gd, 200, 200);
992+
993+
expect(labelCount()).toBe(7);
994+
995+
Plotly.relayout(gd, {'yaxis.domain': [0.48, 0.52]})
996+
.then(function() {
997+
_hoverNatural(gd, 150, 200);
998+
_hoverNatural(gd, 200, 200);
999+
1000+
expect(labelCount()).toBe(7);
1001+
})
1002+
.catch(fail)
1003+
.then(done);
1004+
});
1005+
1006+
});
9691007
});
9701008

9711009
describe('hover info on stacked subplots', function() {

test/jasmine/tests/violin_test.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,12 @@ describe('Test violin hover:', function() {
296296
},
297297
nums: [
298298
'q3: 0.6', 'median: 0.45', 'q3: 0.6', 'max: 1', 'y: 0.9266848, kde: 0.383',
299-
'median: 0.55', 'max: 0.7', 'y: 0.9266848, kde: 0.182',
300-
'median: 0.45', 'q3: 0.6', 'max: 0.9', 'y: 0.9266848, kde: 0.435',
301-
'q3: 0.6', 'max: 0.9'
299+
'median: 0.55', 'min: 0', 'q1: 0.3', 'min: 0.2', 'max: 0.7', 'y: 0.9266848, kde: 0.182',
300+
'median: 0.45', 'min: 0.1', 'q3: 0.6', 'max: 0.9', 'y: 0.9266848, kde: 0.435'
302301
],
303302
name: [
304-
'', 'kale', '', '', '', 'radishes', '',
305-
'', 'carrots', '', '', ''
303+
'', 'kale', '', '', '', 'radishes', '', '', '', '',
304+
'', 'carrots', '', '', '', ''
306305
],
307306
axis: 'day 1'
308307
}, {

0 commit comments

Comments
 (0)