From aff85e078f35ea3c7d21e27ba44348cb1d9a0024 Mon Sep 17 00:00:00 2001 From: alexcjohnson Date: Fri, 17 Mar 2017 18:26:30 -0400 Subject: [PATCH] support arbitrary axis types for clicktoshow annotations --- src/components/annotations/click.js | 8 ++-- test/jasmine/tests/annotations_test.js | 60 +++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/components/annotations/click.js b/src/components/annotations/click.js index 8fe77ce8286..eb6c26f12c5 100644 --- a/src/components/annotations/click.js +++ b/src/components/annotations/click.js @@ -91,9 +91,11 @@ function getToggleSets(gd, hoverData) { if(showMode) { for(j = 0; j < hoverLen; j++) { pointj = hoverData[j]; - if(pointj.x === anni._xclick && pointj.y === anni._yclick && - pointj.xaxis._id === anni.xref && - pointj.yaxis._id === anni.yref) { + if(pointj.xaxis._id === anni.xref && + pointj.yaxis._id === anni.yref && + pointj.xaxis.d2r(pointj.x) === anni._xclick && + pointj.yaxis.d2r(pointj.y) === anni._yclick + ) { // match! toggle this annotation // regardless of its clicktoshow mode // but if it's onout mode, off is implicit diff --git a/test/jasmine/tests/annotations_test.js b/test/jasmine/tests/annotations_test.js index cc9629d245f..ce0cc760c10 100644 --- a/test/jasmine/tests/annotations_test.js +++ b/test/jasmine/tests/annotations_test.js @@ -630,6 +630,8 @@ describe('annotations autorange', function() { describe('annotation clicktoshow', function() { var gd; + beforeEach(function() { gd = createGraphDiv(); }); + afterEach(destroyGraphDiv); function layout() { @@ -716,8 +718,6 @@ describe('annotation clicktoshow', function() { var allIndices = layout().annotations.map(function(v, i) { return i; }); it('should select only clicktoshow annotations matching x, y, and axes of any point', function(done) { - gd = createGraphDiv(); - // first try to select without adding clicktoshow, both visible and invisible Plotly.plot(gd, data, layout()) // clicktoshow is off initially, so it doesn't *expect* clicking will @@ -754,6 +754,62 @@ describe('annotation clicktoshow', function() { .catch(failTest) .then(done); }); + + it('works on date and log axes', function(done) { + Plotly.plot(gd, [{ + x: ['2016-01-01', '2016-01-02', '2016-01-03'], + y: [1, 1, 3] + }], { + yaxis: {type: 'log'}, + annotations: [{ + x: '2016-01-02', + y: 0, + text: 'boo', + showarrow: true, + clicktoshow: 'onoff', + visible: false + }] + }) + .then(function() { + expect(gd._fullLayout.xaxis.type).toBe('date'); + expect(gd._fullLayout.yaxis.type).toBe('log'); + }) + .then(clickAndCheck({newPts: [['2016-01-02', 1]], newCTS: true, on: [0]})) + .catch(failTest) + .then(done); + }); + + it('works on category axes', function(done) { + Plotly.plot(gd, [{ + x: ['a', 'b', 'c'], + y: [1, 2, 3] + }], { + annotations: [{ + x: 'b', + y: 2, + text: 'boo', + showarrow: true, + clicktoshow: 'onout', + visible: false + }, { + // you can also use category serial numbers + x: 2, + y: 3, + text: 'hoo', + showarrow: true, + clicktoshow: 'onout', + visible: false + }] + }) + .then(function() { + expect(gd._fullLayout.xaxis.type).toBe('category'); + expect(gd._fullLayout.yaxis.type).toBe('linear'); + }) + .then(clickAndCheck({newPts: [['b', 2]], newCTS: true, on: [0], step: 1})) + .then(clickAndCheck({newPts: [['c', 3]], newCTS: true, on: [1], step: 2})) + .catch(failTest) + .then(done); + }); }); describe('annotation dragging', function() {