Skip to content

Commit 52271e9

Browse files
authored
Merge pull request #1786 from plotly/hidden-annotations
hide off-plot annotations
2 parents a17165f + e5d45a2 commit 52271e9

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/components/annotations/draw.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
316316
annotationIsOffscreen = true;
317317
}
318318

319-
if(annotationIsOffscreen) return;
319+
if(annotationIsOffscreen) continue;
320320
}
321321
basePx = ax._offset + ax.r2p(options[axLetter]);
322322
autoAlignFraction = 0.5;

test/jasmine/tests/annotations_test.js

+44-1
Original file line numberDiff line numberDiff line change
@@ -552,13 +552,16 @@ describe('annotations log/linear axis changes', function() {
552552
describe('annotations autorange', function() {
553553
'use strict';
554554

555-
var mock = Lib.extendDeep({}, require('@mocks/annotations-autorange.json'));
555+
var mock;
556556
var gd;
557557

558558
beforeAll(function() {
559559
jasmine.addMatchers(customMatchers);
560+
});
560561

562+
beforeEach(function() {
561563
gd = createGraphDiv();
564+
mock = Lib.extendDeep({}, require('@mocks/annotations-autorange.json'));
562565
});
563566

564567
afterEach(destroyGraphDiv);
@@ -588,13 +591,30 @@ describe('annotations autorange', function() {
588591
expect(fullLayout.yaxis3.range).toBeCloseToArray(y3, PREC, 'yaxis3');
589592
}
590593

594+
function assertVisible(indices) {
595+
// right now we keep the annotation groups around when they're invisible,
596+
// they just don't have any graphical elements in them. Might be better
597+
// to get rid of the groups even, but this test will produce the right
598+
// results either way, showing that the annotation is or isn't drawn.
599+
for(var i = 0; i < gd.layout.annotations.length; i++) {
600+
var selectorBase = '.annotation[data-index="' + i + '"]';
601+
var annotationGraphicalItems = d3.selectAll(
602+
selectorBase + ' text,' +
603+
selectorBase + ' rect,' +
604+
selectorBase + ' path');
605+
expect(annotationGraphicalItems.size() > 0)
606+
.toBe(indices.indexOf(i) !== -1, selectorBase);
607+
}
608+
}
609+
591610
it('should adapt to relayout calls', function(done) {
592611
Plotly.plot(gd, mock).then(function() {
593612
assertRanges(
594613
[0.91, 2.09], [0.91, 2.09],
595614
['2000-11-13', '2001-04-21'], [-0.069, 3.917],
596615
[0.88, 2.05], [0.92, 2.08]
597616
);
617+
assertVisible([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]);
598618

599619
return Plotly.relayout(gd, {
600620
'annotations[0].visible': false,
@@ -608,6 +628,7 @@ describe('annotations autorange', function() {
608628
['2001-01-18', '2001-03-27'], [-0.069, 3.917],
609629
[1.44, 2.1], [0.92, 2.08]
610630
);
631+
assertVisible([1, 2, 3, 5, 6, 7, 9, 10, 11, 12, 13]);
611632

612633
return Plotly.relayout(gd, {
613634
'annotations[2].visible': false,
@@ -621,6 +642,7 @@ describe('annotations autorange', function() {
621642
['2001-01-31 23:59:59.999', '2001-02-01 00:00:00.001'], [-0.069, 3.917],
622643
[0.5, 2.5], [0.92, 2.08]
623644
);
645+
assertVisible([1, 3, 6, 7, 10, 11, 12, 13]);
624646

625647
return Plotly.relayout(gd, {
626648
'annotations[0].visible': true,
@@ -637,6 +659,27 @@ describe('annotations autorange', function() {
637659
['2000-11-13', '2001-04-21'], [-0.069, 3.917],
638660
[0.88, 2.05], [0.92, 2.08]
639661
);
662+
assertVisible([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]);
663+
664+
// check that off-plot annotations are hidden - zoom in to
665+
// only one of the four on each subplot
666+
return Plotly.relayout(gd, {
667+
'xaxis.range': [1.4, 1.6],
668+
'yaxis.range': [0.9, 1.1],
669+
'xaxis2.range': ['2001-01-15', '2001-02-15'],
670+
'yaxis2.range': [0.9, 1.1],
671+
'xaxis3.range': [1.9, 2.1],
672+
'yaxis3.range': [1.4, 1.6]
673+
});
674+
})
675+
.then(function() {
676+
assertRanges([1.4, 1.6], [0.9, 1.1],
677+
['2001-01-15', '2001-02-15'], [0.9, 1.1],
678+
[1.9, 2.1], [1.4, 1.6]
679+
);
680+
// only one annotation on each subplot, plus the two paper-referenced
681+
// are visible after zooming in
682+
assertVisible([3, 7, 9, 12, 13]);
640683
})
641684
.catch(failTest)
642685
.then(done);

0 commit comments

Comments
 (0)