Skip to content

Commit e5a4057

Browse files
committed
scatters with period ranges
1 parent 7c37e98 commit e5a4057

File tree

5 files changed

+183
-87
lines changed

5 files changed

+183
-87
lines changed

src/traces/scatter/calc.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ function calc(gd, trace) {
1818
var ya = Axes.getFromId(gd, trace.yaxis || 'y');
1919
var origX = xa.makeCalcdata(trace, 'x');
2020
var origY = ya.makeCalcdata(trace, 'y');
21-
var x = alignPeriod(trace, xa, 'x', origX).vals;
22-
var y = alignPeriod(trace, ya, 'y', origY).vals;
21+
var xObj = alignPeriod(trace, xa, 'x', origX);
22+
var yObj = alignPeriod(trace, ya, 'y', origY);
23+
var x = xObj.vals;
24+
var y = yObj.vals;
2325

2426
var serieslen = trace._length;
2527
var cd = new Array(serieslen);
@@ -64,9 +66,13 @@ function calc(gd, trace) {
6466

6567
if(hasPeriodX) {
6668
cdi.orig_x = origX[i]; // used by hover
69+
cdi.xEnd = xObj.ends[i];
70+
cdi.xStart = xObj.starts[i];
6771
}
6872
if(hasPeriodY) {
6973
cdi.orig_y = origY[i]; // used by hover
74+
cdi.yEnd = yObj.ends[i];
75+
cdi.yStart = yObj.starts[i];
7076
}
7177
} else if(stackGroupOpts && (isV ? xValid : yValid)) {
7278
// if we're stacking we need to hold on to all valid positions

src/traces/scatter/hover.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,54 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
1818
var hoveron = trace.hoveron || '';
1919
var minRad = (trace.mode.indexOf('markers') !== -1) ? 3 : 0.5;
2020

21+
var xPeriod = !!trace.xperiodalignment;
22+
var yPeriod = !!trace.yperiodalignment;
23+
2124
// look for points to hover on first, then take fills only if we
2225
// didn't find a point
26+
2327
if(hoveron.indexOf('points') !== -1) {
28+
// dx and dy are used in compare modes - here we want to always
29+
// prioritize the closest data point, at least as long as markers are
30+
// the same size or nonexistent, but still try to prioritize small markers too.
2431
var dx = function(di) {
25-
// dx and dy are used in compare modes - here we want to always
26-
// prioritize the closest data point, at least as long as markers are
27-
// the same size or nonexistent, but still try to prioritize small markers too.
32+
if(xPeriod) {
33+
var x0 = xa.c2p(di.xStart);
34+
var x1 = xa.c2p(di.xEnd);
35+
36+
return (
37+
xpx >= Math.min(x0, x1) &&
38+
xpx <= Math.max(x0, x1)
39+
) ? 0 : Infinity;
40+
}
41+
2842
var rad = Math.max(3, di.mrc || 0);
2943
var kink = 1 - 1 / rad;
3044
var dxRaw = Math.abs(xa.c2p(di.x) - xpx);
3145
return (dxRaw < rad) ? (kink * dxRaw / rad) : (dxRaw - rad + kink);
3246
};
3347
var dy = function(di) {
48+
if(yPeriod) {
49+
var y0 = ya.c2p(di.yStart);
50+
var y1 = ya.c2p(di.yEnd);
51+
52+
return (
53+
ypx >= Math.min(y0, y1) &&
54+
ypx <= Math.max(y0, y1)
55+
) ? 0 : Infinity;
56+
}
57+
3458
var rad = Math.max(3, di.mrc || 0);
3559
var kink = 1 - 1 / rad;
3660
var dyRaw = Math.abs(ya.c2p(di.y) - ypx);
3761
return (dyRaw < rad) ? (kink * dyRaw / rad) : (dyRaw - rad + kink);
3862
};
63+
64+
// scatter points: d.mrc is the calculated marker radius
65+
// adjust the distance so if you're inside the marker it
66+
// always will show up regardless of point size, but
67+
// prioritize smaller points
3968
var dxy = function(di) {
40-
// scatter points: d.mrc is the calculated marker radius
41-
// adjust the distance so if you're inside the marker it
42-
// always will show up regardless of point size, but
43-
// prioritize smaller points
4469
var rad = Math.max(minRad, di.mrc || 0);
4570
var dx = xa.c2p(di.x) - xpx;
4671
var dy = ya.c2p(di.y) - ypx;
@@ -87,9 +112,6 @@ module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
87112
hovertemplate: trace.hovertemplate
88113
});
89114

90-
if(di.orig_x !== undefined) pointData.xPeriod = di.x;
91-
if(di.orig_y !== undefined) pointData.yPeriod = di.y;
92-
93115
fillText(di, trace, pointData);
94116
Registry.getComponentMethod('errorbars', 'hoverInfo')(di, trace, pointData);
95117

src/traces/scattergl/calc.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,23 @@ module.exports = function calc(gd, trace) {
3131

3232
var origX = xa.makeCalcdata(trace, 'x');
3333
var origY = ya.makeCalcdata(trace, 'y');
34-
var x = alignPeriod(trace, xa, 'x', origX).vals;
35-
var y = alignPeriod(trace, ya, 'y', origY).vals;
34+
var xObj = alignPeriod(trace, xa, 'x', origX);
35+
var yObj = alignPeriod(trace, ya, 'y', origY);
36+
var x = xObj.vals;
37+
var y = yObj.vals;
3638
trace._x = x;
3739
trace._y = y;
3840

39-
if(trace.xperiodalignment) trace._origX = origX;
40-
if(trace.yperiodalignment) trace._origY = origY;
41+
if(trace.xperiodalignment) {
42+
trace._origX = origX;
43+
trace._xStarts = xObj.starts;
44+
trace._xEnds = xObj.ends;
45+
}
46+
if(trace.yperiodalignment) {
47+
trace._origY = origY;
48+
trace._yStarts = yObj.starts;
49+
trace._yEnds = yObj.ends;
50+
}
4151

4252
// we need hi-precision for scatter2d,
4353
// regl-scatter2d uses NaNs for bad/missing values

src/traces/scattergl/hover.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,39 @@ function hoverPoints(pointData, xval, yval, hovermode) {
4545

4646
var minDist = maxDistance;
4747
if(hovermode === 'x') {
48+
var xPeriod = !!trace.xperiodalignment;
49+
var yPeriod = !!trace.yperiodalignment;
50+
4851
for(i = 0; i < ids.length; i++) {
4952
k = ids[i];
5053
ptx = x[k];
54+
5155
dx = Math.abs(xa.c2p(ptx) - xpx);
56+
if(xPeriod) {
57+
var x0 = xa.c2p(trace._xStarts[k]);
58+
var x1 = xa.c2p(trace._xEnds[k]);
59+
60+
dx = (
61+
xpx >= Math.min(x0, x1) &&
62+
xpx <= Math.max(x0, x1)
63+
) ? 0 : Infinity;
64+
}
65+
5266
if(dx < minDist) {
5367
minDist = dx;
5468
pty = y[k];
5569
dy = ya.c2p(pty) - ypx;
70+
71+
if(yPeriod) {
72+
var y0 = ya.c2p(trace._yStarts[k]);
73+
var y1 = ya.c2p(trace._yEnds[k]);
74+
75+
dy = (
76+
ypx >= Math.min(y0, y1) &&
77+
ypx <= Math.max(y0, y1)
78+
) ? 0 : Infinity;
79+
}
80+
5681
dxy = Math.sqrt(dx * dx + dy * dy);
5782
closestId = ids[i];
5883
}
@@ -177,9 +202,6 @@ function calcHover(pointData, x, y, trace) {
177202
hovertemplate: di.ht
178203
});
179204

180-
if(origX) pointData2.xPeriod = di.x;
181-
if(origY) pointData2.yPeriod = di.y;
182-
183205
if(di.htx) pointData2.text = di.htx;
184206
else if(di.tx) pointData2.text = di.tx;
185207
else if(trace.text) pointData2.text = trace.text;

0 commit comments

Comments
 (0)