Skip to content

Commit 222b5fe

Browse files
committed
improve scattergl hover and tests
1 parent 69d6af9 commit 222b5fe

File tree

5 files changed

+59
-11
lines changed

5 files changed

+59
-11
lines changed

src/traces/scattergl/calc.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,16 @@ module.exports = function calc(gd, trace) {
3737
var stash = {};
3838
var i, xx, yy;
3939

40-
var x = xa.makeCalcdata(trace, 'x');
41-
var y = ya.makeCalcdata(trace, 'y');
42-
x = alignPeriod(trace, xa, 'x', x);
43-
y = alignPeriod(trace, ya, 'y', y);
40+
var origX = xa.makeCalcdata(trace, 'x');
41+
var origY = ya.makeCalcdata(trace, 'y');
42+
var x = alignPeriod(trace, xa, 'x', origX);
43+
var y = alignPeriod(trace, ya, 'y', origY);
4444
trace._x = x;
4545
trace._y = y;
4646

47+
if(trace.xperiodalignment) trace._origX = origX;
48+
if(trace.yperiodalignment) trace._origY = origY;
49+
4750
// we need hi-precision for scatter2d,
4851
// regl-scatter2d uses NaNs for bad/missing values
4952
var positions = new Array(len2);

src/traces/scattergl/hover.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,19 @@ function calcHover(pointData, x, y, trace) {
161161
var fakeCd = {};
162162
fakeCd[pointData.index] = di;
163163

164+
var origX = trace._origX;
165+
var origY = trace._origY;
166+
164167
var pointData2 = Lib.extendFlat({}, pointData, {
165168
color: getTraceColor(trace, di),
166169

167170
x0: xp - rad,
168171
x1: xp + rad,
169-
xLabelVal: di.x,
172+
xLabelVal: origX ? origX[id] : di.x,
170173

171174
y0: yp - rad,
172175
y1: yp + rad,
173-
yLabelVal: di.y,
176+
yLabelVal: origY ? origY[id] : di.y,
174177

175178
cd: fakeCd,
176179
distance: minDist,
8.07 KB
Loading

test/image/mocks/gl2d_period_positioning.json

+9-5
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@
33
{
44
"name": "scattergl",
55
"type": "scattergl",
6-
"y": ["2001-01-01", "2002-01-01", "2003-01-01", "2004-01-01"],
7-
"x": ["2001-01-01", "2002-01-01", "2003-01-01", "2004-01-01"],
6+
"x": ["2001-01-01", "2002-01-01", "2003-01-01", "2004-01-01", "2005-01-01", "2006-01-01"],
7+
"y": ["1970-01-01", "1970-02-01", "1970-03-01", "1970-04-01", "1970-05-01", "1970-06-01"],
88
"xperiod": "M12",
99
"xperiodalignment": "middle",
10-
"yperiod": "M12",
10+
"yperiod": "M1",
1111
"yperiodalignment": "middle"
1212
}
1313
],
1414
"layout": {
15-
"width": 500,
16-
"height": 250,
15+
"width": 600,
16+
"height": 300,
17+
"margin": {
18+
"t": 30,
19+
"b": 30
20+
},
1721
"showlegend": true,
1822
"hovermode": "closest",
1923

test/jasmine/tests/gl2d_click_test.js

+38
Original file line numberDiff line numberDiff line change
@@ -629,3 +629,41 @@ describe('Test hover and click interactions', function() {
629629
.then(done);
630630
});
631631
});
632+
633+
describe('hover with (x|y)period positioning', function() {
634+
'use strict';
635+
636+
var gd;
637+
638+
beforeEach(function() {
639+
gd = createGraphDiv();
640+
});
641+
642+
afterEach(destroyGraphDiv);
643+
644+
function _hover(x, y) {
645+
delete gd._hoverdata;
646+
Lib.clearThrottle();
647+
mouseEvent('mousemove', x, y);
648+
}
649+
650+
it('@gl shows hover info for scattergl', function(done) {
651+
Plotly.newPlot(gd, require('@mocks/gl2d_period_positioning.json'))
652+
.then(function() { _hover(100, 255); })
653+
.then(function() {
654+
assertHoverLabelContent({
655+
name: '',
656+
nums: '(Jan 2001, Jan 1, 1970)'
657+
});
658+
})
659+
.then(function() { _hover(470, 45); })
660+
.then(function() {
661+
assertHoverLabelContent({
662+
name: '',
663+
nums: '(Jan 2006, Jun 1, 1970)'
664+
});
665+
})
666+
.catch(failTest)
667+
.then(done);
668+
});
669+
});

0 commit comments

Comments
 (0)