Skip to content

Commit 43c1181

Browse files
committed
Recompute hover on click to increase click robustness
1 parent 2105fd4 commit 43c1181

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

src/plots/cartesian/graph_interact.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ fx.init = function(gd) {
149149
};
150150

151151
maindrag.onclick = function(evt) {
152-
fx.click(gd, evt);
152+
fx.click(gd, evt, subplot);
153153
};
154154

155155
// corner draggers
@@ -300,8 +300,8 @@ fx.hover = function(gd, evt, subplot) {
300300

301301
// The actual implementation is here:
302302

303-
function hover(gd, evt, subplot) {
304-
if(subplot === 'pie') {
303+
function hover(gd, evt, subplot, noHoverEvent) {
304+
if(subplot === 'pie' && !noHoverEvent) {
305305
gd.emit('plotly_hover', {
306306
event: evt.originalEvent,
307307
points: [evt]
@@ -635,7 +635,7 @@ function hover(gd, evt, subplot) {
635635
}
636636

637637
// don't emit events if called manually
638-
if(!evt.target || !hoverChanged(gd, evt, oldhoverdata)) return;
638+
if(!evt.target || noHoverEvent || !hoverChanged(gd, evt, oldhoverdata)) return;
639639

640640
if(oldhoverdata) {
641641
gd.emit('plotly_unhover', {
@@ -1526,9 +1526,13 @@ function hoverChanged(gd, evt, oldhoverdata) {
15261526
}
15271527

15281528
// on click
1529-
fx.click = function(gd, evt) {
1529+
fx.click = function(gd, evt, subplot) {
15301530
var annotationsDone = Registry.getComponentMethod('annotations', 'onClick')(gd, gd._hoverdata);
15311531

1532+
// The true flag at the end causes it to re-run the hover computation to figure out *which*
1533+
// point is being clicked. Without this, clicking is somewhat unreliable.
1534+
fx.hover(gd, evt, subplot, true);
1535+
15321536
function emitClick() { gd.emit('plotly_click', {points: gd._hoverdata, event: evt}); }
15331537

15341538
if(gd._hoverdata && evt && evt.target) {

src/plots/ternary/ternary.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ proto.initInteractions = function() {
631631
};
632632

633633
dragger.onclick = function(evt) {
634-
fx.click(gd, evt);
634+
fx.click(gd, evt, _this.id);
635635
};
636636

637637
dragElement.init(dragOptions);

test/jasmine/tests/hover_label_test.js

+17
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,23 @@ describe('hover info', function() {
544544
Plotly.plot(gd, data, layout).then(done);
545545
});
546546

547+
it('should skip the hover event if explicitly instructed', function(done) {
548+
var hoverHandler = jasmine.createSpy();
549+
gd.on('plotly_hover', hoverHandler);
550+
551+
var gdBB = gd.getBoundingClientRect();
552+
var event = {clientX: gdBB.left + 300, clientY: gdBB.top + 200};
553+
554+
Promise.resolve().then(function() {
555+
Fx.hover(gd, event, 'xy', true);
556+
})
557+
.then(function() {
558+
expect(hoverHandler).not.toHaveBeenCalled();
559+
})
560+
.catch(fail)
561+
.then(done);
562+
});
563+
547564
it('should emit events only if the event looks user-driven', function(done) {
548565
var hoverHandler = jasmine.createSpy();
549566
gd.on('plotly_hover', hoverHandler);

0 commit comments

Comments
 (0)