Skip to content

Commit 7f40318

Browse files
committed
annotations: add 'visible' attribute
- so that the visibility of a given annotations can be turned off w/o having to remove to item or (in a hack) set 'opacity' to 0.
1 parent b0338bd commit 7f40318

File tree

6 files changed

+30
-5
lines changed

6 files changed

+30
-5
lines changed

src/components/annotations/annotation_defaults.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ module.exports = function handleAnnotationDefaults(annIn, fullLayout) {
2323
return Lib.coerce(annIn, annOut, attributes, attr, dflt);
2424
}
2525

26+
var visible = coerce('visible');
27+
28+
if(!visible) return annOut;
29+
2630
coerce('opacity');
2731
coerce('align');
2832
coerce('bgcolor');

src/components/annotations/attributes.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ var extendFlat = require('../../lib/extend').extendFlat;
1717
module.exports = {
1818
_isLinkedToArray: true,
1919

20+
visible: {
21+
valType: 'boolean',
22+
role: 'info',
23+
dflt: true,
24+
description: [
25+
'Determines whether or not this annotation is visible.'
26+
].join(' ')
27+
},
28+
2029
text: {
2130
valType: 'string',
2231
role: 'info',

src/components/annotations/calc_autorange.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var draw = require('./draw').draw;
1717

1818
module.exports = function calcAutorange(gd) {
1919
var fullLayout = gd._fullLayout,
20-
annotationList = fullLayout.annotations;
20+
annotationList = Lib.filterVisible(fullLayout.annotations);
2121

2222
if(!annotationList.length || !gd._fullData.length) return;
2323

src/components/annotations/draw.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ function draw(gd) {
4747
fullLayout._infolayer.selectAll('.annotation').remove();
4848

4949
for(var i = 0; i < fullLayout.annotations.length; i++) {
50-
drawOne(gd, i);
50+
if(fullLayout.annotations[i].visible) {
51+
drawOne(gd, i);
52+
}
5153
}
5254

5355
return Plots.previousPromises(gd);
@@ -140,8 +142,6 @@ function drawOne(gd, index, opt, value) {
140142
// where we fail here when they add/remove annotations
141143
if(!optionsIn) return;
142144

143-
var oldRef = {xref: optionsIn.xref, yref: optionsIn.yref};
144-
145145
// alter the input annotation as requested
146146
var optionsEdit = {};
147147
if(typeof opt === 'string' && opt) optionsEdit[opt] = value;
@@ -153,7 +153,11 @@ function drawOne(gd, index, opt, value) {
153153
Lib.nestedProperty(optionsIn, k).set(optionsEdit[k]);
154154
}
155155

156+
// return early in visible: false updates
157+
if(optionsIn.visible === false) return;
158+
156159
var gs = fullLayout._size;
160+
var oldRef = {xref: optionsIn.xref, yref: optionsIn.yref};
157161

158162
var axLetters = ['x', 'y'];
159163
for(i = 0; i < 2; i++) {

test/image/mocks/annotations.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
{"text":"right bottom","showarrow":false,"xref":"paper","yref":"paper","xanchor":"right","yanchor":"bottom","x":0.5,"y":1},
1818
{"text":"move with page","xref":"paper","yref":"paper","x":0.75,"y":1},
1919
{"text":"opacity","opacity":0.5,"x":5,"y":5},
20+
{"text":"not-visible", "visible": false},
2021
{"text":"left<br>justified","showarrow":false,"align":"left","x":1,"y":4},
2122
{"text":"center<br>justified","showarrow":false,"x":2,"y":4},
2223
{"text":"right<br>justified","showarrow":false,"align":"right","x":3,"y":4},

test/jasmine/tests/annotations_test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ describe('annotations relayout', function() {
4242
'use strict';
4343

4444
var mock = require('@mocks/annotations.json');
45-
var len = mock.layout.annotations.length;
4645
var gd;
4746

47+
// there is 1 visible: false item
48+
var len = mock.layout.annotations.length - 1;
49+
4850
beforeEach(function(done) {
4951
gd = createGraphDiv();
5052

@@ -78,6 +80,11 @@ describe('annotations relayout', function() {
7880
.then(function() {
7981
expect(countAnnotations()).toEqual(len - 1);
8082

83+
return Plotly.relayout(gd, 'annotations[' + 0 + '].visible', false);
84+
})
85+
.then(function() {
86+
expect(countAnnotations()).toEqual(len - 2);
87+
8188
return Plotly.relayout(gd, { annotations: [] });
8289
})
8390
.then(function() {

0 commit comments

Comments
 (0)