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

Skip to content

Commit c45c074

Browse files
committed
hover on fills: constrain label to cartesian and ternary viewports
1 parent cda7f6c commit c45c074

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/traces/scatter/hover.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
107107
}
108108

109109
if(inside) {
110+
// constrain ymin/max to the visible plot, so the label goes
111+
// at the middle of the piece you can see
112+
ymin = Math.max(ymin, 0);
113+
ymax = Math.min(ymax, ya._length);
114+
110115
// find the overall left-most and right-most points of the
111116
// polygon(s) we're inside at their combined vertical midpoint.
112117
// This is where we will draw the hover label.
@@ -128,6 +133,10 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
128133
}
129134
}
130135

136+
// constrain xmin/max to the visible plot now too
137+
xmin = Math.max(xmin, 0);
138+
xmax = Math.min(xmax, xa._length);
139+
131140
// get only fill or line color for the hover color
132141
var color = Color.defaultLine;
133142
if(Color.opacity(trace.fillcolor)) color = trace.fillcolor;

src/traces/scatterternary/hover.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,29 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
1717
var scatterPointData = scatterHover(pointData, xval, yval, hovermode);
1818
if(!scatterPointData || scatterPointData[0].index === false) return;
1919

20+
var newPointData = scatterPointData[0];
21+
2022
// if hovering on a fill, we don't show any point data so the label is
21-
// unchanged from what scatter gives us.
22-
if(scatterPointData[0].index === undefined) return scatterPointData;
23+
// unchanged from what scatter gives us - except that it needs to
24+
// be constrained to the trianglular plot area, not just the rectangular
25+
// area defined by the synthetic x and y axes
26+
// TODO: in some cases the vertical middle of the shape is not within
27+
// the triangular viewport at all, so the label can become disconnected
28+
// from the shape entirely. But calculating what portion of the shape
29+
// is actually visible, as constrained by the diagonal axis lines, is not
30+
// so easy and anyway we lost the information we would have needed to do
31+
// this inside scatterHover.
32+
if(newPointData.index === undefined) {
33+
var yFracUp = 1 - (newPointData.y0 / pointData.ya._length),
34+
xLen = pointData.xa._length,
35+
xMin = xLen * yFracUp / 2,
36+
xMax = xLen - xMin;
37+
newPointData.x0 = Math.max(Math.min(newPointData.x0, xMax), xMin);
38+
newPointData.x1 = Math.max(Math.min(newPointData.x1, xMax), xMin);
39+
return scatterPointData;
40+
}
2341

24-
var newPointData = scatterPointData[0],
25-
cdi = newPointData.cd[newPointData.index];
42+
var cdi = newPointData.cd[newPointData.index];
2643

2744
newPointData.a = cdi.a;
2845
newPointData.b = cdi.b;

0 commit comments

Comments
 (0)