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

Skip to content

Commit ea123a6

Browse files
committed
Enable hovermode x
1 parent f219206 commit ea123a6

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
lines changed

src/traces/scattergl/index.js

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ ScatterRegl.calc = function calc(container, trace) {
5151
var yaxis = Axes.getFromId(container, trace.yaxis);
5252
var markerOpts = trace.marker;
5353

54-
// FIXME: find a better way to obtain subplot object from trace
54+
// FIXME: is it the best way to obtain subplot object from trace
5555
var subplot = layout._plots[trace.xaxis + trace.yaxis];
5656

5757
// makeCalcdata runs d2c (data-to-coordinate) on every point
@@ -440,6 +440,11 @@ ScatterRegl.calc = function calc(container, trace) {
440440
}
441441
}
442442
}
443+
//expand no-markers axes
444+
else {
445+
Axes.expand(xaxis, stash.rawx, { padded: true });
446+
Axes.expand(yaxis, stash.rawy, { padded: true });
447+
}
443448

444449

445450
// make sure scene exists
@@ -703,10 +708,10 @@ ScatterRegl.plot = function plot(container, subplot, cdata) {
703708
var yaxis = Axes.getFromId(container, trace.yaxis || 'y');
704709

705710
var range = [
706-
xaxis.range[0],
707-
yaxis.range[0],
708-
xaxis.range[1],
709-
yaxis.range[1]
711+
xaxis._rl[0],
712+
yaxis._rl[0],
713+
xaxis._rl[1],
714+
yaxis._rl[1]
710715
];
711716

712717
var viewport = [
@@ -766,7 +771,7 @@ ScatterRegl.plot = function plot(container, subplot, cdata) {
766771
};
767772

768773

769-
ScatterRegl.hoverPoints = function hover(pointData, xval, yval) {
774+
ScatterRegl.hoverPoints = function hover(pointData, xval, yval, hovermode) {
770775
var cd = pointData.cd,
771776
stash = cd[0].t,
772777
trace = cd[0].trace,
@@ -775,6 +780,7 @@ ScatterRegl.hoverPoints = function hover(pointData, xval, yval) {
775780
positions = stash.positions,
776781
x = stash.rawx,
777782
y = stash.rawy,
783+
scene = stash.scene,
778784
xpx = xa.c2p(xval),
779785
ypx = ya.c2p(yval),
780786
ids;
@@ -787,7 +793,18 @@ ScatterRegl.hoverPoints = function hover(pointData, xval, yval) {
787793
// );
788794

789795
//FIXME: this works only for the case of linear points
790-
ids = stash.tree.within(xval, yval, MAXDIST / xa._m);
796+
if (hovermode === 'x') {
797+
ids = stash.tree.range(
798+
xa.p2c(xpx - MAXDIST), ya._rl[0],
799+
xa.p2c(xpx + MAXDIST), ya._rl[1]
800+
);
801+
}
802+
else {
803+
ids = stash.tree.range(
804+
xa.p2c(xpx - MAXDIST), ya.p2c(ypx + MAXDIST),
805+
xa.p2c(xpx + MAXDIST), ya.p2c(ypx - MAXDIST)
806+
);
807+
}
791808
}
792809
else if (stash.ids) {
793810
ids = stash.ids;
@@ -796,17 +813,29 @@ ScatterRegl.hoverPoints = function hover(pointData, xval, yval) {
796813

797814
// pick the id closest to the point
798815
// note that point possibly may not be found
799-
var min = MAXDIST, id, ptx, pty;
800-
801-
for(var i = 0; i < ids.length; i++) {
802-
ptx = x[ids[i]];
803-
pty = y[ids[i]];
804-
var dx = xa.c2p(ptx) - xpx, dy = ya.c2p(pty) - ypx;
805-
806-
var dist = Math.sqrt(dx * dx + dy * dy);
807-
if(dist < min) {
808-
min = dist;
809-
id = ids[i];
816+
var min = MAXDIST, id, ptx, pty, i;
817+
818+
if (hovermode === 'x') {
819+
for(i = 0; i < ids.length; i++) {
820+
ptx = x[ids[i]];
821+
var dx = Math.abs(xa.c2p(ptx) - xpx);
822+
if(dx < min) {
823+
min = dx;
824+
id = ids[i];
825+
}
826+
}
827+
}
828+
else {
829+
for(i = 0; i < ids.length; i++) {
830+
ptx = x[ids[i]];
831+
pty = y[ids[i]];
832+
var dx = xa.c2p(ptx) - xpx, dy = ya.c2p(pty) - ypx;
833+
834+
var dist = Math.sqrt(dx * dx + dy * dy);
835+
if(dist < min) {
836+
min = dist;
837+
id = ids[i];
838+
}
810839
}
811840
}
812841

0 commit comments

Comments
 (0)