Skip to content

Commit ee3b72c

Browse files
committed
add Axes.cleanPosition method
- a little wrapper around ax.cleanPos - use it for annotations *xclick* and *yclick*
1 parent d8462eb commit ee3b72c

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

src/components/annotations/annotation_defaults.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,12 @@ module.exports = function handleAnnotationDefaults(annIn, annOut, fullLayout, op
8383

8484
// put the actual click data to bind to into private attributes
8585
// so we don't have to do this little bit of logic on every hover event
86-
annOut._xclick = (xClick === undefined) ? annOut.x : xClick;
87-
annOut._yclick = (yClick === undefined) ? annOut.y : yClick;
86+
annOut._xclick = (xClick === undefined) ?
87+
annOut.x :
88+
Axes.cleanPosition(xClick, gdMock, annOut.xref);
89+
annOut._yclick = (yClick === undefined) ?
90+
annOut.y :
91+
Axes.cleanPosition(yClick, gdMock, annOut.yref);
8892
}
8993

9094
return annOut;

src/plots/cartesian/axes.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ axes.coercePosition = function(containerOut, gd, coerce, axRef, attr, dflt) {
113113
containerOut[attr] = ax.cleanPos(pos);
114114
};
115115

116+
axes.cleanPosition = function(pos, gd, axRef) {
117+
var ax = (axRef === 'paper' || axRef === 'pixel') ?
118+
{ cleanPos: Lib.num } :
119+
axes.getFromId(gd, axRef);
120+
121+
return ax.cleanPos(pos);
116122
};
117123

118124
axes.getDataToCoordFunc = function(gd, trace, target, targetArray) {

test/jasmine/tests/annotations_test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,48 @@ describe('Test annotations', function() {
9898
expect(layoutOut.annotations[0].ax).toEqual('2004-07-01');
9999
});
100100

101+
it('should clean *xclick* and *yclick* values', function() {
101102
var layoutIn = {
102103
annotations: [{
104+
clicktoshow: 'onoff',
105+
xref: 'paper',
106+
yref: 'paper',
107+
xclick: '10',
108+
yclick: '30'
109+
}, {
110+
clicktoshow: 'onoff',
111+
xref: 'x',
112+
yref: 'y',
113+
xclick: '1',
114+
yclick: '2017-13-50'
115+
}, {
116+
clicktoshow: 'onoff',
117+
xref: 'x2',
118+
yref: 'y2',
119+
xclick: '2',
120+
yclick: 'A'
103121
}]
104122
};
105123

106124
var layoutOut = {
125+
xaxis: {type: 'linear', range: [0, 1]},
126+
yaxis: {type: 'date', range: ['2000-01-01', '2018-01-01']},
127+
xaxis2: {type: 'log', range: [1, 2]},
128+
yaxis2: {type: 'category', range: [0, 1]}
107129
};
108130

131+
['xaxis', 'xaxis2', 'yaxis', 'yaxis2'].forEach(function(k) {
132+
Axes.setConvert(layoutOut[k]);
133+
});
134+
109135
_supply(layoutIn, layoutOut);
110136

137+
expect(layoutOut.annotations[0]._xclick).toBe(10, 'paper x');
138+
expect(layoutOut.annotations[0]._yclick).toBe(30, 'paper y');
139+
expect(layoutOut.annotations[1]._xclick).toBe(1, 'linear');
140+
expect(layoutOut.annotations[1]._yclick).toBe(undefined, 'invalid date');
141+
expect(layoutOut.annotations[2]._xclick).toBe(2, 'log');
142+
expect(layoutOut.annotations[2]._yclick).toBe('A', 'category');
111143
});
112144
});
113145
});

0 commit comments

Comments
 (0)