Skip to content

Commit 486d254

Browse files
committed
spikeline: select closest bar when spikedistance=-1
1 parent 703fde0 commit 486d254

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/traces/bar/hover.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ function hoverOnBars(pointData, xval, yval, hovermode) {
3939
var isClosest = (hovermode === 'closest');
4040
var isWaterfall = (trace.type === 'waterfall');
4141
var maxHoverDistance = pointData.maxHoverDistance;
42-
var maxSpikeDistance = pointData.maxSpikeDistance;
4342

4443
var posVal, sizeVal, posLetter, sizeLetter, dx, dy, pRangeCalc;
4544

@@ -161,7 +160,7 @@ function hoverOnBars(pointData, xval, yval, hovermode) {
161160
pointData.valueLabel = hoverLabelText(sa, pointData[sizeLetter + 'LabelVal']);
162161

163162
// spikelines always want "closest" distance regardless of hovermode
164-
pointData.spikeDistance = (sizeFn(di) + thisBarPositionFn(di)) / 2 + maxSpikeDistance - maxHoverDistance;
163+
pointData.spikeDistance = (sizeFn(di) + thisBarPositionFn(di)) / 2;
165164
// they also want to point to the data value, regardless of where the label goes
166165
// in case of bars shifted within groups
167166
pointData[posLetter + 'Spike'] = pa.c2p(di.p, true);

test/jasmine/tests/hover_spikeline_test.js

+23
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,29 @@ describe('spikeline hover', function() {
404404
.then(done);
405405
});
406406

407+
it('correctly select the closest bar even when setting spikedistance to -1', function(done) {
408+
var mock = require('@mocks/bar_stack-with-gaps');
409+
var mockCopy = Lib.extendDeep({}, mock);
410+
mockCopy.layout.xaxis.showspikes = true;
411+
mockCopy.layout.yaxis.showspikes = true;
412+
mockCopy.layout.spikedistance = -1;
413+
414+
Plotly.newPlot(gd, mockCopy)
415+
.then(function() {
416+
_hover({xpx: 600, ypx: 400});
417+
var lines = d3.selectAll('line.spikeline');
418+
expect(lines.size()).toBe(4);
419+
expect(lines[0][1].getAttribute('stroke')).toBe('#2ca02c');
420+
421+
_hover({xpx: 600, ypx: 200});
422+
lines = d3.selectAll('line.spikeline');
423+
expect(lines.size()).toBe(4);
424+
expect(lines[0][1].getAttribute('stroke')).toBe('#1f77b4');
425+
})
426+
.catch(failTest)
427+
.then(done);
428+
});
429+
407430
it('correctly responds to setting the spikedistance to 0 by disabling ' +
408431
'the search for points to draw the spikelines', function(done) {
409432
var _mock = makeMock('toaxis', 'closest');

0 commit comments

Comments
 (0)