Skip to content

Commit 12f199a

Browse files
committed
fix and improve closest pick in compare modes
1 parent 58353f7 commit 12f199a

File tree

2 files changed

+139
-6
lines changed

2 files changed

+139
-6
lines changed

src/components/fx/hover.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,17 +666,30 @@ function _hover(gd, evt, subplot, noHoverEvent) {
666666

667667
var finalPoints = [];
668668
var seen = {};
669+
var id = 0;
669670
var insert = function(hd) {
670671
var type = hd.trace.type;
671-
var key = (
672+
var multiplePoints = (
672673
type === 'box' ||
673674
type === 'violin' ||
674675
type === 'ohlc' ||
675676
type === 'candlestick'
676-
) ? hoverDataKey(hd) : hd.trace.index;
677+
);
678+
679+
var key = multiplePoints ? hoverDataKey(hd) : hd.trace.index;
677680
if(!seen[key]) {
678-
seen[key] = true;
681+
id++;
682+
seen[key] = id;
679683
finalPoints.push(hd);
684+
} else {
685+
var oldId = seen[key] - 1;
686+
if(
687+
Math.abs(winningPoint.distance - hd.distance) <
688+
Math.abs(winningPoint.distance - finalPoints[oldId].distance)
689+
) {
690+
// replace with closest
691+
finalPoints[oldId] = hd;
692+
}
680693
}
681694
};
682695

test/jasmine/tests/hover_label_test.js

Lines changed: 123 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5319,6 +5319,126 @@ describe('hovermode: (x|y)unified', function() {
53195319
.then(done, done.fail);
53205320
});
53215321

5322+
it('period with hover distance -1 include closest not farthest', function(done) {
5323+
Plotly.newPlot(gd, {
5324+
data: [
5325+
{
5326+
name: 'bar',
5327+
type: 'bar',
5328+
x: [
5329+
'2017-07-01',
5330+
'2017-10-01',
5331+
'2018-01-01',
5332+
],
5333+
xhoverformat: 'Q%q',
5334+
xperiod: 'M3',
5335+
y: [
5336+
12,
5337+
15,
5338+
18
5339+
]
5340+
},
5341+
{
5342+
name: 'scatter',
5343+
type: 'scatter',
5344+
x: [
5345+
'2017-07-01',
5346+
'2017-08-01',
5347+
'2017-09-01',
5348+
'2017-10-01',
5349+
'2017-11-01',
5350+
'2017-12-01',
5351+
'2018-01-01',
5352+
'2018-02-01',
5353+
'2018-03-01',
5354+
],
5355+
xhoverformat: '%b',
5356+
xperiod: 'M1',
5357+
y: [
5358+
1,
5359+
2,
5360+
3,
5361+
4,
5362+
5,
5363+
6,
5364+
7,
5365+
8,
5366+
9,
5367+
]
5368+
}
5369+
],
5370+
layout: {
5371+
showlegend: false,
5372+
width: 600,
5373+
height: 400,
5374+
hovermode: 'x unified',
5375+
hoverdistance: -1,
5376+
xaxis: {
5377+
dtick: 'M1',
5378+
showgrid: true,
5379+
ticklabelmode: 'period',
5380+
type: 'date'
5381+
}
5382+
}
5383+
})
5384+
.then(function(gd) {
5385+
_hover(gd, { xpx: 50, ypx: 200 });
5386+
assertLabel({title: 'Jul', items: [
5387+
'bar : (Q3, 12)',
5388+
'scatter : 1'
5389+
]});
5390+
5391+
_hover(gd, { xpx: 75, ypx: 200 });
5392+
assertLabel({title: 'Aug', items: [
5393+
'bar : (Q3, 12)',
5394+
'scatter : 2'
5395+
]});
5396+
5397+
_hover(gd, { xpx: 100, ypx: 200 });
5398+
assertLabel({title: 'Sep', items: [
5399+
'bar : (Q3, 12)',
5400+
'scatter : 3'
5401+
]});
5402+
5403+
_hover(gd, { xpx: 150, ypx: 200 });
5404+
assertLabel({title: 'Oct', items: [
5405+
'bar : (Q4, 15)',
5406+
'scatter : 4'
5407+
]});
5408+
5409+
_hover(gd, { xpx: 200, ypx: 200 });
5410+
assertLabel({title: 'Nov', items: [
5411+
'bar : (Q4, 15)',
5412+
'scatter : 5'
5413+
]});
5414+
5415+
_hover(gd, { xpx: 250, ypx: 200 });
5416+
assertLabel({title: 'Dec', items: [
5417+
'bar : (Q4, 15)',
5418+
'scatter : 6'
5419+
]});
5420+
5421+
_hover(gd, { xpx: 300, ypx: 200 });
5422+
assertLabel({title: 'Jan', items: [
5423+
'bar : (Q1, 18)',
5424+
'scatter : 7'
5425+
]});
5426+
5427+
_hover(gd, { xpx: 350, ypx: 200 });
5428+
assertLabel({title: 'Feb', items: [
5429+
'bar : (Q1, 18)',
5430+
'scatter : 8'
5431+
]});
5432+
5433+
_hover(gd, { xpx: 400, ypx: 200 });
5434+
assertLabel({title: 'Mar', items: [
5435+
'bar : (Q1, 18)',
5436+
'scatter : 9'
5437+
]});
5438+
})
5439+
.then(done, done.fail);
5440+
});
5441+
53225442
it('should have the same traceorder as the legend', function(done) {
53235443
var mock = require('@mocks/stacked_area.json');
53245444
var mockCopy = Lib.extendDeep({}, mock);
@@ -5347,10 +5467,10 @@ describe('hovermode: (x|y)unified', function() {
53475467
.then(function(gd) {
53485468
_hover(gd, {curveNumber: 0});
53495469

5350-
assertLabel({title: 'Apr 13, 2014, 15:21:15', items: [
5470+
assertLabel({title: 'Apr 13, 2014, 15:21:11', items: [
53515471
'Outdoor (wun... : (Apr 13, 2014, 15:26:12, 69.4)',
5352-
'1st Floor (N... : 74.8',
5353-
'2nd Floor (R... : (Apr 13, 2014, 15:21:11, 73.625)',
5472+
'1st Floor (N... : (Apr 13, 2014, 15:21:15, 74.8)',
5473+
'2nd Floor (R... : 73.625',
53545474
'Attic (Ardui... : (Apr 13, 2014, 15:26:34, 98.49)'
53555475
]});
53565476
})

0 commit comments

Comments
 (0)