Skip to content

Commit 751d527

Browse files
committed
add new tests and fix cursor
1 parent 911ae0e commit 751d527

File tree

3 files changed

+50
-22
lines changed

3 files changed

+50
-22
lines changed

src/components/fx/hover.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,11 @@ function _hover(gd, evt, subplot, noHoverEvent) {
274274
if(hasUserCalledHover) {
275275
if('xpx' in evt) xpx = evt.xpx;
276276
else xpx = xaArray[0]._length / 2;
277+
if(!('offsetX' in evt)) evt.offsetX = xpx + xaArray[0]._offset;
277278

278279
if('ypx' in evt) ypx = evt.ypx;
279280
else ypx = yaArray[0]._length / 2;
281+
if(!('offsetY' in evt)) evt.offsetY = ypx + yaArray[0]._offset;
280282
}
281283
else {
282284
// fire the beforehover event and quit if it returns false
@@ -419,7 +421,7 @@ function _hover(gd, evt, subplot, noHoverEvent) {
419421
if(hoverData.length === 0) {
420422
pointData.distance = spikedistance;
421423
pointData.index = false;
422-
var closestPoints = trace._module.hoverPoints(pointData, xval, yval, 'closest');
424+
var closestPoints = trace._module.hoverPoints(pointData, xval, yval, 'closest', fullLayout._hoverlayer);
423425
if(closestPoints) {
424426
var tmpPoint;
425427
var closestVPoints = closestPoints.filter(function(point) {
@@ -1247,10 +1249,11 @@ function createSpikelines(closestPoints, opts) {
12471249

12481250
if(ySnap === 'cursor') {
12491251
hLinePointY = evt.offsetY;
1252+
hLinePointX = evt.offsetX;
12501253
} else {
12511254
hLinePointY = ya._offset + (hLinePoint.y0 + hLinePoint.y1) / 2;
1255+
hLinePointX = xa._offset + (hLinePoint.x0 + hLinePoint.x1) / 2;
12521256
}
1253-
hLinePointX = xa._offset + (hLinePoint.x0 + hLinePoint.x1) / 2;
12541257
var dfltHLineColor = tinycolor.readability(hLinePoint.color, contrastColor) < 1.5 ?
12551258
Color.contrast(contrastColor) : hLinePoint.color;
12561259
var yMode = ya.spikemode,
@@ -1322,12 +1325,13 @@ function createSpikelines(closestPoints, opts) {
13221325

13231326
if(xSnap === 'cursor') {
13241327
vLinePointX = evt.offsetX;
1328+
vLinePointY = evt.offsetY;
13251329
} else {
13261330
vLinePointX = xa._offset + (vLinePoint.x0 + vLinePoint.x1) / 2;
1331+
vLinePointY = ya._offset + (vLinePoint.y0 + vLinePoint.y1) / 2;
13271332
}
1328-
vLinePointY = ya._offset + (vLinePoint.y0 + vLinePoint.y1) / 2;
13291333
var dfltVLineColor = tinycolor.readability(vLinePoint.color, contrastColor) < 1.5 ?
1330-
Color.contrast(contrastColor) : vLinePoint.color;
1334+
Color.contrast(contrastColor) : vLinePoint.color;
13311335
var xMode = xa.spikemode,
13321336
xThickness = xa.spikethickness,
13331337
xColor = xa.spikecolor || dfltVLineColor,

test/jasmine/tests/hover_label_test.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -1842,7 +1842,17 @@ describe('hover distance', function() {
18421842
});
18431843
});
18441844

1845-
it('responds to hoverdistance change', function() {
1845+
it('responds to hoverdistance change part 1', function() {
1846+
var gd = document.getElementById('graph');
1847+
var evt = { xpx: 450, ypx: 155 };
1848+
gd._fullLayout.hoverdistance = 10;
1849+
1850+
Fx.hover('graph', evt, 'xy');
1851+
1852+
expect(gd._hoverdata).toEqual(undefined);
1853+
});
1854+
1855+
it('responds to hoverdistance change part 2', function() {
18461856
var gd = document.getElementById('graph');
18471857
var evt = { xpx: 450, ypx: 155 };
18481858
gd._fullLayout.hoverdistance = 30;
@@ -1857,7 +1867,8 @@ describe('hover distance', function() {
18571867
expect(hoverTrace.y).toEqual(3);
18581868

18591869
assertHoverLabelContent({
1860-
nums: '(2, 3)',
1870+
nums: '3',
1871+
axis: '2',
18611872
name: 'trace 0'
18621873
});
18631874
});

test/jasmine/tests/hover_spikeline_test.js

+29-16
Original file line numberDiff line numberDiff line change
@@ -135,43 +135,56 @@ describe('spikeline', function() {
135135
.then(done);
136136
});
137137

138-
it('automatically switch between toaxis and across spikemodes on switching the hovermodes', function(done) {
138+
it('draws lines and markers on enabled axes in the spikesnap "cursor" mode', function(done) {
139139
gd = createGraphDiv();
140-
var _mock = makeMock('toaxis', 'closest');
140+
var _mock = makeMock('toaxis', 'x');
141141

142-
Plotly.plot(gd, _mock).then(function() {
143-
_hover({xval: 2, yval: 3}, 'xy');
142+
_mock.layout.xaxis.spikesnap = 'cursor';
143+
_mock.layout.yaxis.spikesnap = 'cursor';
144+
_mock.layout.xaxis2.spikesnap = 'cursor';
145+
146+
Plotly.plot(gd, _mock)
147+
.then(function() {
148+
_set_spikedistance(200);
149+
})
150+
.then(function() {
151+
_hover({xpx: 120, ypx: 180}, 'xy');
144152
_assert(
145-
[[557, 401, 557, 250], [557, 401, 557, 250], [80, 250, 557, 250], [80, 250, 557, 250]],
146-
[[83, 250]]
153+
[[200, 401, 200, 280], [200, 401, 200, 280], [80, 280, 200, 280], [80, 280, 200, 280]],
154+
[[83, 280]]
147155
);
148156
})
149157
.then(function() {
150-
_hover({xval: 30, yval: 40}, 'x2y2');
158+
_hover({xpx: 31, ypx: 41}, 'x2y2');
151159
_assert(
152-
[[820, 220, 820, 167], [820, 220, 820, 167]],
160+
[[682, 220, 682, 156], [682, 220, 682, 156]],
153161
[]
154162
);
155163
})
156-
.then(function() {
157-
_set_hovermode('x');
158-
})
159-
.then(function() {
164+
.catch(fail)
165+
.then(done);
166+
});
167+
168+
it('doesn\'t switch between toaxis and across spikemodes on switching the hovermodes', function(done) {
169+
gd = createGraphDiv();
170+
var _mock = makeMock('toaxis', 'closest');
171+
172+
Plotly.plot(gd, _mock).then(function() {
160173
_hover({xval: 2, yval: 3}, 'xy');
161174
_assert(
162-
[[557, 100, 557, 401], [557, 100, 557, 401], [80, 250, 1033, 250], [80, 250, 1033, 250]],
175+
[[557, 401, 557, 250], [557, 401, 557, 250], [80, 250, 557, 250], [80, 250, 557, 250]],
163176
[[83, 250]]
164177
);
165178
})
166179
.then(function() {
167180
_hover({xval: 30, yval: 40}, 'x2y2');
168181
_assert(
169-
[[820, 115, 820, 220], [820, 115, 820, 220]],
182+
[[820, 220, 820, 167], [820, 220, 820, 167]],
170183
[]
171184
);
172185
})
173186
.then(function() {
174-
_set_hovermode('closest');
187+
_set_hovermode('x');
175188
})
176189
.then(function() {
177190
_hover({xval: 2, yval: 3}, 'xy');
@@ -191,7 +204,7 @@ describe('spikeline', function() {
191204
.then(done);
192205
});
193206

194-
it('increase the range of search for points to draw the spikelines', function(done) {
207+
it('increase the range of search for points to draw the spikelines on spikedistance change', function(done) {
195208
gd = createGraphDiv();
196209
var _mock = makeMock('toaxis', 'closest');
197210

0 commit comments

Comments
 (0)