Skip to content

Commit 19f3800

Browse files
authored
Merge pull request #2949 from plotly/2782-sankey-disable-hover
disable hover for sankey traces when hovermode is false
2 parents bff736d + 3d32b6f commit 19f3800

File tree

2 files changed

+78
-25
lines changed

2 files changed

+78
-25
lines changed

src/traces/sankey/plot.js

+6
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ module.exports = function plot(gd, calcData) {
130130
};
131131

132132
var linkHover = function(element, d, sankey) {
133+
if(gd._fullLayout.hovermode === false) return;
133134
d3.select(element).call(linkHoveredStyle.bind(0, d, sankey, true));
134135
gd.emit('plotly_hover', {
135136
event: d3.event,
@@ -143,6 +144,7 @@ module.exports = function plot(gd, calcData) {
143144
var outgoingLabel = _(gd, 'outgoing flow count:') + ' ';
144145

145146
var linkHoverFollow = function(element, d) {
147+
if(gd._fullLayout.hovermode === false) return;
146148
var trace = d.link.trace;
147149
var rootBBox = gd._fullLayout._paperdiv.node().getBoundingClientRect();
148150
var boundingBox = element.getBoundingClientRect();
@@ -175,6 +177,7 @@ module.exports = function plot(gd, calcData) {
175177
};
176178

177179
var linkUnhover = function(element, d, sankey) {
180+
if(gd._fullLayout.hovermode === false) return;
178181
d3.select(element).call(linkNonHoveredStyle.bind(0, d, sankey, true));
179182
gd.emit('plotly_unhover', {
180183
event: d3.event,
@@ -193,6 +196,7 @@ module.exports = function plot(gd, calcData) {
193196
};
194197

195198
var nodeHover = function(element, d, sankey) {
199+
if(gd._fullLayout.hovermode === false) return;
196200
d3.select(element).call(nodeHoveredStyle, d, sankey);
197201
gd.emit('plotly_hover', {
198202
event: d3.event,
@@ -201,6 +205,7 @@ module.exports = function plot(gd, calcData) {
201205
};
202206

203207
var nodeHoverFollow = function(element, d) {
208+
if(gd._fullLayout.hovermode === false) return;
204209
var trace = d.node.trace;
205210
var nodeRect = d3.select(element).select('.' + cn.nodeRect);
206211
var rootBBox = gd._fullLayout._paperdiv.node().getBoundingClientRect();
@@ -236,6 +241,7 @@ module.exports = function plot(gd, calcData) {
236241
};
237242

238243
var nodeUnhover = function(element, d, sankey) {
244+
if(gd._fullLayout.hovermode === false) return;
239245
d3.select(element).call(nodeNonHoveredStyle, d, sankey);
240246
gd.emit('plotly_unhover', {
241247
event: d3.event,

test/jasmine/tests/sankey_test.js

+72-25
Original file line numberDiff line numberDiff line change
@@ -388,16 +388,16 @@ describe('sankey tests', function() {
388388
describe('Test hover/click interactions:', function() {
389389
afterEach(destroyGraphDiv);
390390

391+
function _hover(px, py) {
392+
mouseEvent('mousemove', px, py);
393+
mouseEvent('mouseover', px, py);
394+
Lib.clearThrottle();
395+
}
396+
391397
it('should show the correct hover labels', function(done) {
392398
var gd = createGraphDiv();
393399
var mockCopy = Lib.extendDeep({}, mock);
394400

395-
function _hover(px, py) {
396-
mouseEvent('mousemove', px, py);
397-
mouseEvent('mouseover', px, py);
398-
Lib.clearThrottle();
399-
}
400-
401401
Plotly.plot(gd, mockCopy).then(function() {
402402
_hover(404, 302);
403403

@@ -464,12 +464,6 @@ describe('sankey tests', function() {
464464
var mockCopy = Lib.extendDeep({}, mock);
465465
delete mockCopy.data[0].link.label;
466466

467-
function _hover(px, py) {
468-
mouseEvent('mousemove', px, py);
469-
mouseEvent('mouseover', px, py);
470-
Lib.clearThrottle();
471-
}
472-
473467
Plotly.plot(gd, mockCopy)
474468
.then(function() {
475469
_hover(450, 300);
@@ -482,6 +476,22 @@ describe('sankey tests', function() {
482476
.catch(failTest)
483477
.then(done);
484478
});
479+
480+
it('should not show labels if hovermode is false', function(done) {
481+
var gd = createGraphDiv();
482+
var mockCopy = Lib.extendDeep({}, mock);
483+
484+
Plotly.plot(gd, mockCopy).then(function() {
485+
return Plotly.relayout(gd, 'hovermode', false);
486+
})
487+
.then(function() {
488+
_hover(404, 302);
489+
490+
assertNoLabel();
491+
})
492+
.catch(failTest)
493+
.then(done);
494+
});
485495
});
486496

487497
describe('Test hover/click event data:', function() {
@@ -527,44 +537,52 @@ describe('sankey tests', function() {
527537
mouseEvent('mouseout', pos[0], pos[1]);
528538
});
529539

530-
it('should output correct hover/click/unhover event data', function(done) {
531-
var fig = Lib.extendDeep({}, mock);
540+
function _assert(d, expectedPtData) {
541+
expect(d.event).toBeDefined('original event reference');
532542

533-
function _assert(d, expectedPtData) {
534-
expect(d.event).toBeDefined('original event reference');
543+
var ptData = d.points[0];
544+
Object.keys(expectedPtData).forEach(function(k) {
545+
expect(ptData[k]).toBe(expectedPtData[k], 'point data for ' + k);
546+
});
547+
}
535548

536-
var ptData = d.points[0];
537-
Object.keys(expectedPtData).forEach(function(k) {
538-
expect(ptData[k]).toBe(expectedPtData[k], 'point data for ' + k);
539-
});
540-
}
549+
it('should output correct click event data', function(done) {
550+
var fig = Lib.extendDeep({}, mock);
541551

542552
Plotly.plot(gd, fig)
543-
.then(function() { return _hover('node'); })
553+
.then(function() { return _click('node'); })
544554
.then(function(d) {
545555
_assert(d, {
546556
curveNumber: 0,
547557
pointNumber: 4,
548558
label: 'Solid'
549559
});
550560
})
551-
.then(function() { return _hover('link'); })
561+
.then(function() { return _click('link'); })
552562
.then(function(d) {
553563
_assert(d, {
554564
curveNumber: 0,
555565
pointNumber: 61,
556566
value: 46.477
557567
});
558568
})
559-
.then(function() { return _click('node'); })
569+
.catch(failTest)
570+
.then(done);
571+
});
572+
573+
it('should output correct hover/unhover event data', function(done) {
574+
var fig = Lib.extendDeep({}, mock);
575+
576+
Plotly.plot(gd, fig)
577+
.then(function() { return _hover('node'); })
560578
.then(function(d) {
561579
_assert(d, {
562580
curveNumber: 0,
563581
pointNumber: 4,
564582
label: 'Solid'
565583
});
566584
})
567-
.then(function() { return _click('link'); })
585+
.then(function() { return _hover('link'); })
568586
.then(function(d) {
569587
_assert(d, {
570588
curveNumber: 0,
@@ -591,6 +609,30 @@ describe('sankey tests', function() {
591609
.catch(failTest)
592610
.then(done);
593611
});
612+
613+
it('should not output hover/unhover event data when hovermoder is false', function(done) {
614+
var fig = Lib.extendDeep({}, mock);
615+
616+
Plotly.plot(gd, fig)
617+
.then(function() { return Plotly.relayout(gd, 'hovermode', false); })
618+
.then(function() { return _hover('node'); })
619+
.then(failTest).catch(function(err) {
620+
expect(err).toBe('plotly_hover did not get called!');
621+
})
622+
.then(function() { return _unhover('node'); })
623+
.then(failTest).catch(function(err) {
624+
expect(err).toBe('plotly_unhover did not get called!');
625+
})
626+
.then(function() { return _hover('link'); })
627+
.then(failTest).catch(function(err) {
628+
expect(err).toBe('plotly_hover did not get called!');
629+
})
630+
.then(function() { return _unhover('link'); })
631+
.then(failTest).catch(function(err) {
632+
expect(err).toBe('plotly_unhover did not get called!');
633+
})
634+
.then(done);
635+
});
594636
});
595637
});
596638

@@ -620,3 +662,8 @@ function assertLabel(content, style) {
620662
fontColor: style[4]
621663
});
622664
}
665+
666+
function assertNoLabel() {
667+
var g = d3.selectAll('.hovertext');
668+
expect(g.size()).toBe(0);
669+
}

0 commit comments

Comments
 (0)