diff --git a/src/components/annotations/draw.js b/src/components/annotations/draw.js index a9cf34861d2..4a6e3af1e15 100644 --- a/src/components/annotations/draw.js +++ b/src/components/annotations/draw.js @@ -316,7 +316,7 @@ function drawRaw(gd, options, index, subplotId, xa, ya) { annotationIsOffscreen = true; } - if(annotationIsOffscreen) return; + if(annotationIsOffscreen) continue; } basePx = ax._offset + ax.r2p(options[axLetter]); autoAlignFraction = 0.5; diff --git a/test/jasmine/tests/annotations_test.js b/test/jasmine/tests/annotations_test.js index 5a0e4fbd2f0..cb06aaced0a 100644 --- a/test/jasmine/tests/annotations_test.js +++ b/test/jasmine/tests/annotations_test.js @@ -552,13 +552,16 @@ describe('annotations log/linear axis changes', function() { describe('annotations autorange', function() { 'use strict'; - var mock = Lib.extendDeep({}, require('@mocks/annotations-autorange.json')); + var mock; var gd; beforeAll(function() { jasmine.addMatchers(customMatchers); + }); + beforeEach(function() { gd = createGraphDiv(); + mock = Lib.extendDeep({}, require('@mocks/annotations-autorange.json')); }); afterEach(destroyGraphDiv); @@ -588,6 +591,22 @@ describe('annotations autorange', function() { expect(fullLayout.yaxis3.range).toBeCloseToArray(y3, PREC, 'yaxis3'); } + function assertVisible(indices) { + // right now we keep the annotation groups around when they're invisible, + // they just don't have any graphical elements in them. Might be better + // to get rid of the groups even, but this test will produce the right + // results either way, showing that the annotation is or isn't drawn. + for(var i = 0; i < gd.layout.annotations.length; i++) { + var selectorBase = '.annotation[data-index="' + i + '"]'; + var annotationGraphicalItems = d3.selectAll( + selectorBase + ' text,' + + selectorBase + ' rect,' + + selectorBase + ' path'); + expect(annotationGraphicalItems.size() > 0) + .toBe(indices.indexOf(i) !== -1, selectorBase); + } + } + it('should adapt to relayout calls', function(done) { Plotly.plot(gd, mock).then(function() { assertRanges( @@ -595,6 +614,7 @@ describe('annotations autorange', function() { ['2000-11-13', '2001-04-21'], [-0.069, 3.917], [0.88, 2.05], [0.92, 2.08] ); + assertVisible([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]); return Plotly.relayout(gd, { 'annotations[0].visible': false, @@ -608,6 +628,7 @@ describe('annotations autorange', function() { ['2001-01-18', '2001-03-27'], [-0.069, 3.917], [1.44, 2.1], [0.92, 2.08] ); + assertVisible([1, 2, 3, 5, 6, 7, 9, 10, 11, 12, 13]); return Plotly.relayout(gd, { 'annotations[2].visible': false, @@ -621,6 +642,7 @@ describe('annotations autorange', function() { ['2001-01-31 23:59:59.999', '2001-02-01 00:00:00.001'], [-0.069, 3.917], [0.5, 2.5], [0.92, 2.08] ); + assertVisible([1, 3, 6, 7, 10, 11, 12, 13]); return Plotly.relayout(gd, { 'annotations[0].visible': true, @@ -637,6 +659,27 @@ describe('annotations autorange', function() { ['2000-11-13', '2001-04-21'], [-0.069, 3.917], [0.88, 2.05], [0.92, 2.08] ); + assertVisible([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]); + + // check that off-plot annotations are hidden - zoom in to + // only one of the four on each subplot + return Plotly.relayout(gd, { + 'xaxis.range': [1.4, 1.6], + 'yaxis.range': [0.9, 1.1], + 'xaxis2.range': ['2001-01-15', '2001-02-15'], + 'yaxis2.range': [0.9, 1.1], + 'xaxis3.range': [1.9, 2.1], + 'yaxis3.range': [1.4, 1.6] + }); + }) + .then(function() { + assertRanges([1.4, 1.6], [0.9, 1.1], + ['2001-01-15', '2001-02-15'], [0.9, 1.1], + [1.9, 2.1], [1.4, 1.6] + ); + // only one annotation on each subplot, plus the two paper-referenced + // are visible after zooming in + assertVisible([3, 7, 9, 12, 13]); }) .catch(failTest) .then(done);