Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 903cefc

Browse files
committed
unified hoverlabel: fix position to fit on screen
1 parent 47c4f29 commit 903cefc

File tree

4 files changed

+40
-31
lines changed

4 files changed

+40
-31
lines changed

src/components/fx/hover.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -924,15 +924,15 @@ function createHoverText(hoverData, opts, gd) {
924924

925925
// Show a single hover label
926926
if(['xunified', 'yunified'].indexOf(hovermode) !== -1) {
927-
// similarly to compare mode, we remove the "close but not quite" points
927+
// similarly to compare mode, we remove the "close but not quite together" points
928928
hoverData = filterClosePoints(hoverData);
929929
var mockLayoutIn = {
930930
showlegend: true,
931931
legend: {
932932
title: {text: t0},
933-
bgcolor: '#fff',
934-
borderwidth: 1,
935-
bordercolor: '#aaa'
933+
bgcolor: '#fff', // TODO: use layout.hoverlabel.bg or layout.paper_bgcolor
934+
borderwidth: 1, // TODO: use default or 1
935+
bordercolor: '#aaa' // TODO: use default
936936
}
937937
};
938938
var mockLayoutOut = {};
@@ -958,6 +958,29 @@ function createHoverText(hoverData, opts, gd) {
958958
var tbb = legendContainer.node().getBoundingClientRect();
959959
lx += xa._offset;
960960
ly += ya._offset - tbb.height / 2;
961+
962+
// Change horizontal alignment to end up on screen
963+
var txWidth = tbb.width + 2 * HOVERTEXTPAD;
964+
var anchorStartOK = lx + txWidth <= outerWidth;
965+
var anchorEndOK = lx - txWidth >= 0;
966+
if(!anchorStartOK && anchorEndOK) {
967+
lx -= txWidth;
968+
} else {
969+
lx += 2 * HOVERTEXTPAD;
970+
}
971+
972+
// Change vertical alignement to end up on screen
973+
var txHeight = tbb.height + 2 * HOVERTEXTPAD;
974+
var overflowTop = ly <= outerTop;
975+
var overflowBottom = ly + txHeight >= outerHeight;
976+
var canFit = txHeight <= outerHeight;
977+
if(canFit) {
978+
if(overflowTop) {
979+
ly = outerTop - tbb.top + 2 * HOVERTEXTPAD;
980+
} else if(overflowBottom) {
981+
ly = outerHeight - txHeight;
982+
}
983+
}
961984
legendContainer.attr('transform', 'translate(' + lx + ',' + ly + ')');
962985

963986
return legendContainer;
@@ -1180,6 +1203,7 @@ function getHoverLabelText(d, showCommonLabel, hovermode, fullLayout, t0, g) {
11801203
}
11811204
return [text, name];
11821205
}
1206+
11831207
// Make groups of touching points, and within each group
11841208
// move each point so that no labels overlap, but the average
11851209
// label position is the same as it was before moving. Indicentally,

src/components/legend/draw.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ module.exports = function draw(gd, layer, opts) {
159159
scrollBar.on('.drag', null);
160160
legend.on('wheel', null);
161161

162-
if(opts._height <= opts._maxHeight || gd._context.staticPlot) {
162+
if(!main || opts._height <= opts._maxHeight || gd._context.staticPlot) {
163163
// if scrollbar should not be shown.
164164
bg.attr({
165165
width: opts._width - bw,
11.1 KB
Loading

test/image/mocks/hovermode_xunified.json

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,42 @@
11
{
22
"data": [
33
{
4-
"x": [
5-
0,
6-
1,
7-
2,
8-
3,
9-
4,
10-
5,
11-
6,
12-
7,
13-
8
14-
],
154
"y": [
165
0,
176
3,
187
6,
198
4,
20-
5,
9+
10,
2110
2,
2211
3,
2312
5,
24-
4
13+
4,
14+
0,
15+
5
2516
],
2617
"type": "scatter"
2718
},
2819
{
29-
"x": [
30-
0,
31-
1,
32-
2,
33-
3,
34-
4,
35-
5,
36-
6,
37-
7,
38-
8
39-
],
4020
"y": [
4121
0,
4222
4,
4323
7,
4424
8,
45-
3,
25+
10,
4626
6,
4727
3,
4828
3,
49-
4
29+
4,
30+
0,
31+
5
5032
],
5133
"type": "scatter"
5234
}
5335
],
5436
"layout": {
37+
"margin": {"t": 0, "l": 0, "r": 0, "b": 0},
38+
"xaxis": {"range": [0, 10]},
39+
"yaxis": {"range": [0, 10]},
5540
"showlegend": false,
5641
"hovermode": "xunified"
5742
}

0 commit comments

Comments
 (0)