Skip to content

Commit 6de4c75

Browse files
authored
Merge pull request #4665 from plotly/spike-hover-congruence
introduce spikesnap mode "hovered data"
2 parents c9d0052 + 67b5953 commit 6de4c75

File tree

5 files changed

+34
-10
lines changed

5 files changed

+34
-10
lines changed

src/components/fx/hover.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ function _hover(gd, evt, subplot, noHoverEvent) {
520520
if(closestPoints && closestPoints.length) {
521521
var tmpPoint;
522522
var closestVPoints = closestPoints.filter(function(point) {
523-
return point.xa.showspikes;
523+
return point.xa.showspikes && point.xa.spikesnap !== 'hovered data';
524524
});
525525
if(closestVPoints.length) {
526526
var closestVPt = closestVPoints[0];
@@ -533,7 +533,7 @@ function _hover(gd, evt, subplot, noHoverEvent) {
533533
}
534534

535535
var closestHPoints = closestPoints.filter(function(point) {
536-
return point.ya.showspikes;
536+
return point.ya.showspikes && point.ya.spikesnap !== 'hovered data';
537537
});
538538
if(closestHPoints.length) {
539539
var closestHPt = closestHPoints[0];

src/plots/cartesian/layout_attributes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ module.exports = {
580580
},
581581
spikesnap: {
582582
valType: 'enumerated',
583-
values: ['data', 'cursor'],
583+
values: ['data', 'cursor', 'hovered data'],
584584
dflt: 'data',
585585
role: 'style',
586586
editType: 'none',

src/plots/cartesian/layout_defaults.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
255255
var spikethickness = coerce2('spikethickness', unifiedHover ? 1.5 : undefined);
256256
var spikedash = coerce2('spikedash', unifiedHover ? 'dot' : undefined);
257257
var spikemode = coerce2('spikemode', unifiedHover ? 'across' : undefined);
258-
var spikesnap = coerce2('spikesnap');
258+
var spikesnap = coerce2('spikesnap', unifiedHover ? 'hovered data' : undefined);
259259
var showSpikes = coerce('showspikes', !!unifiedSpike || !!spikecolor || !!spikethickness || !!spikedash || !!spikemode || !!spikesnap);
260260

261261
if(!showSpikes) {

test/jasmine/tests/hover_label_test.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -4044,6 +4044,7 @@ describe('hovermode: (x|y)unified', function() {
40444044
expect(ax.spikethickness).toBe(1.5);
40454045
expect(ax.spikedash).toBe('dot');
40464046
expect(ax.spikecolor).toBe('red');
4047+
expect(ax.spikesnap).toBe('hovered data');
40474048
expect(gd._fullLayout.yaxis.showspike).toBeFalse;
40484049
})
40494050
.catch(failTest)
@@ -4060,6 +4061,7 @@ describe('hovermode: (x|y)unified', function() {
40604061
expect(ax.spikethickness).toBe(1.5);
40614062
expect(ax.spikedash).toBe('dot');
40624063
expect(ax.spikecolor).toBe('red');
4064+
expect(ax.spikesnap).toBe('hovered data');
40634065
expect(gd._fullLayout.yaxis.showspike).toBeFalse;
40644066
})
40654067
.catch(failTest)
@@ -4251,17 +4253,12 @@ describe('hovermode: (x|y)unified', function() {
42514253
Plotly.newPlot(gd, mockCopy)
42524254
.then(function(gd) {
42534255
_hover(gd, {xval: 1});
4254-
// assertLabel({title: 'B', items: ['asdf', 'asdf']});
4256+
42554257
assertSymbol([
42564258
['rgb(0, 255, 0)', '0px', ''],
42574259
['rgb(255, 255, 0)', '5px', 'rgb(0, 0, 127)']
42584260
]);
42594261
})
4260-
.then(function() {
4261-
_hover(gd, {xval: 4});
4262-
// assertLabel({title: 'E', items: ['asdf', 'asdf']});
4263-
// assertSymbol([['rgb(168, 140, 33)', '4px', 'rgb(111, 193, 115)']]);
4264-
})
42654262
.catch(failTest)
42664263
.then(done);
42674264
});

test/jasmine/tests/hover_spikeline_test.js

+27
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,33 @@ describe('spikeline hover', function() {
298298
.then(done);
299299
});
300300

301+
it('does not show spikes if no points are hovered in the spikesnap "hovered data" mode', function(done) {
302+
var _mock = makeMock('toaxis', 'x');
303+
Plotly.newPlot(gd, _mock)
304+
.then(function() {
305+
_setSpikedistance(-1);
306+
})
307+
.then(function() {
308+
_hover({xval: 1.5});
309+
_assert(
310+
[[558, 401, 558, 251], [80, 251, 558, 251]], [[83, 251]]
311+
);
312+
return Plotly.relayout(gd, 'xaxis.spikesnap', 'hovered data');
313+
})
314+
.then(function() {
315+
_hover({xval: 1.5});
316+
_assert([[80, 251, 558, 251]], [[83, 251]]);
317+
318+
return Plotly.relayout(gd, 'yaxis.spikesnap', 'hovered data');
319+
})
320+
.then(function() {
321+
_hover({xval: 1.5});
322+
_assert([], []);
323+
})
324+
.catch(failTest)
325+
.then(done);
326+
});
327+
301328
it('doesn\'t switch between toaxis and across spikemodes on switching the hovermodes', function(done) {
302329
var _mock = makeMock('toaxis', 'closest');
303330

0 commit comments

Comments
 (0)