Skip to content

Improved category axes conversions #1748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 31, 2017
8 changes: 6 additions & 2 deletions src/components/annotations/annotation_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,12 @@ module.exports = function handleAnnotationDefaults(annIn, annOut, fullLayout, op

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

return annOut;
Expand Down
6 changes: 6 additions & 0 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ axes.coercePosition = function(containerOut, gd, coerce, axRef, attr, dflt) {
containerOut[attr] = ax.cleanPos(pos);
};

axes.cleanPosition = function(pos, gd, axRef) {
var ax = (axRef === 'paper' || axRef === 'pixel') ?
{ cleanPos: Lib.num } :
axes.getFromId(gd, axRef);

return ax.cleanPos(pos);
};

axes.getDataToCoordFunc = function(gd, trace, target, targetArray) {
Expand Down
32 changes: 32 additions & 0 deletions test/jasmine/tests/annotations_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,48 @@ describe('Test annotations', function() {
expect(layoutOut.annotations[0].ax).toEqual('2004-07-01');
});

it('should clean *xclick* and *yclick* values', function() {
var layoutIn = {
annotations: [{
clicktoshow: 'onoff',
xref: 'paper',
yref: 'paper',
xclick: '10',
yclick: '30'
}, {
clicktoshow: 'onoff',
xref: 'x',
yref: 'y',
xclick: '1',
yclick: '2017-13-50'
}, {
clicktoshow: 'onoff',
xref: 'x2',
yref: 'y2',
xclick: '2',
yclick: 'A'
}]
};

var layoutOut = {
xaxis: {type: 'linear', range: [0, 1]},
yaxis: {type: 'date', range: ['2000-01-01', '2018-01-01']},
xaxis2: {type: 'log', range: [1, 2]},
yaxis2: {type: 'category', range: [0, 1]}
};

['xaxis', 'xaxis2', 'yaxis', 'yaxis2'].forEach(function(k) {
Axes.setConvert(layoutOut[k]);
});

_supply(layoutIn, layoutOut);

expect(layoutOut.annotations[0]._xclick).toBe(10, 'paper x');
expect(layoutOut.annotations[0]._yclick).toBe(30, 'paper y');
expect(layoutOut.annotations[1]._xclick).toBe(1, 'linear');
expect(layoutOut.annotations[1]._yclick).toBe(undefined, 'invalid date');
expect(layoutOut.annotations[2]._xclick).toBe(2, 'log');
expect(layoutOut.annotations[2]._yclick).toBe('A', 'category');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

});
});
});
Expand Down