Skip to content

Commit aff85e0

Browse files
committed
support arbitrary axis types for clicktoshow annotations
1 parent 96b3191 commit aff85e0

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

src/components/annotations/click.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,11 @@ function getToggleSets(gd, hoverData) {
9191
if(showMode) {
9292
for(j = 0; j < hoverLen; j++) {
9393
pointj = hoverData[j];
94-
if(pointj.x === anni._xclick && pointj.y === anni._yclick &&
95-
pointj.xaxis._id === anni.xref &&
96-
pointj.yaxis._id === anni.yref) {
94+
if(pointj.xaxis._id === anni.xref &&
95+
pointj.yaxis._id === anni.yref &&
96+
pointj.xaxis.d2r(pointj.x) === anni._xclick &&
97+
pointj.yaxis.d2r(pointj.y) === anni._yclick
98+
) {
9799
// match! toggle this annotation
98100
// regardless of its clicktoshow mode
99101
// but if it's onout mode, off is implicit

test/jasmine/tests/annotations_test.js

+58-2
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,8 @@ describe('annotations autorange', function() {
630630
describe('annotation clicktoshow', function() {
631631
var gd;
632632

633+
beforeEach(function() { gd = createGraphDiv(); });
634+
633635
afterEach(destroyGraphDiv);
634636

635637
function layout() {
@@ -716,8 +718,6 @@ describe('annotation clicktoshow', function() {
716718
var allIndices = layout().annotations.map(function(v, i) { return i; });
717719

718720
it('should select only clicktoshow annotations matching x, y, and axes of any point', function(done) {
719-
gd = createGraphDiv();
720-
721721
// first try to select without adding clicktoshow, both visible and invisible
722722
Plotly.plot(gd, data, layout())
723723
// clicktoshow is off initially, so it doesn't *expect* clicking will
@@ -754,6 +754,62 @@ describe('annotation clicktoshow', function() {
754754
.catch(failTest)
755755
.then(done);
756756
});
757+
758+
it('works on date and log axes', function(done) {
759+
Plotly.plot(gd, [{
760+
x: ['2016-01-01', '2016-01-02', '2016-01-03'],
761+
y: [1, 1, 3]
762+
}], {
763+
yaxis: {type: 'log'},
764+
annotations: [{
765+
x: '2016-01-02',
766+
y: 0,
767+
text: 'boo',
768+
showarrow: true,
769+
clicktoshow: 'onoff',
770+
visible: false
771+
}]
772+
})
773+
.then(function() {
774+
expect(gd._fullLayout.xaxis.type).toBe('date');
775+
expect(gd._fullLayout.yaxis.type).toBe('log');
776+
})
777+
.then(clickAndCheck({newPts: [['2016-01-02', 1]], newCTS: true, on: [0]}))
778+
.catch(failTest)
779+
.then(done);
780+
});
781+
782+
it('works on category axes', function(done) {
783+
Plotly.plot(gd, [{
784+
x: ['a', 'b', 'c'],
785+
y: [1, 2, 3]
786+
}], {
787+
annotations: [{
788+
x: 'b',
789+
y: 2,
790+
text: 'boo',
791+
showarrow: true,
792+
clicktoshow: 'onout',
793+
visible: false
794+
}, {
795+
// you can also use category serial numbers
796+
x: 2,
797+
y: 3,
798+
text: 'hoo',
799+
showarrow: true,
800+
clicktoshow: 'onout',
801+
visible: false
802+
}]
803+
})
804+
.then(function() {
805+
expect(gd._fullLayout.xaxis.type).toBe('category');
806+
expect(gd._fullLayout.yaxis.type).toBe('linear');
807+
})
808+
.then(clickAndCheck({newPts: [['b', 2]], newCTS: true, on: [0], step: 1}))
809+
.then(clickAndCheck({newPts: [['c', 3]], newCTS: true, on: [1], step: 2}))
810+
.catch(failTest)
811+
.then(done);
812+
});
757813
});
758814

759815
describe('annotation dragging', function() {

0 commit comments

Comments
 (0)