Skip to content

Commit a8fb653

Browse files
committed
adapt and 🔒 annotations to 'calcIfAutorange'-less edits!
1 parent 35d501d commit a8fb653

File tree

4 files changed

+75
-58
lines changed

4 files changed

+75
-58
lines changed

src/components/annotations/attributes.js

+22-22
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = templatedArray('annotation', {
1919
valType: 'boolean',
2020
role: 'info',
2121
dflt: true,
22-
editType: 'calcIfAutorange+arraydraw',
22+
editType: 'calc+arraydraw',
2323
description: [
2424
'Determines whether or not this annotation is visible.'
2525
].join(' ')
@@ -28,7 +28,7 @@ module.exports = templatedArray('annotation', {
2828
text: {
2929
valType: 'string',
3030
role: 'info',
31-
editType: 'calcIfAutorange+arraydraw',
31+
editType: 'calc+arraydraw',
3232
description: [
3333
'Sets the text associated with this annotation.',
3434
'Plotly uses a subset of HTML tags to do things like',
@@ -41,14 +41,14 @@ module.exports = templatedArray('annotation', {
4141
valType: 'angle',
4242
dflt: 0,
4343
role: 'style',
44-
editType: 'calcIfAutorange+arraydraw',
44+
editType: 'calc+arraydraw',
4545
description: [
4646
'Sets the angle at which the `text` is drawn',
4747
'with respect to the horizontal.'
4848
].join(' ')
4949
},
5050
font: fontAttrs({
51-
editType: 'calcIfAutorange+arraydraw',
51+
editType: 'calc+arraydraw',
5252
colorEditType: 'arraydraw',
5353
description: 'Sets the annotation text font.'
5454
}),
@@ -57,7 +57,7 @@ module.exports = templatedArray('annotation', {
5757
min: 1,
5858
dflt: null,
5959
role: 'style',
60-
editType: 'calcIfAutorange+arraydraw',
60+
editType: 'calc+arraydraw',
6161
description: [
6262
'Sets an explicit width for the text box. null (default) lets the',
6363
'text set the box width. Wider text will be clipped.',
@@ -69,7 +69,7 @@ module.exports = templatedArray('annotation', {
6969
min: 1,
7070
dflt: null,
7171
role: 'style',
72-
editType: 'calcIfAutorange+arraydraw',
72+
editType: 'calc+arraydraw',
7373
description: [
7474
'Sets an explicit height for the text box. null (default) lets the',
7575
'text set the box height. Taller text will be clipped.'
@@ -130,7 +130,7 @@ module.exports = templatedArray('annotation', {
130130
min: 0,
131131
dflt: 1,
132132
role: 'style',
133-
editType: 'calcIfAutorange+arraydraw',
133+
editType: 'calc+arraydraw',
134134
description: [
135135
'Sets the padding (in px) between the `text`',
136136
'and the enclosing border.'
@@ -141,7 +141,7 @@ module.exports = templatedArray('annotation', {
141141
min: 0,
142142
dflt: 1,
143143
role: 'style',
144-
editType: 'calcIfAutorange+arraydraw',
144+
editType: 'calc+arraydraw',
145145
description: [
146146
'Sets the width (in px) of the border enclosing',
147147
'the annotation `text`.'
@@ -152,7 +152,7 @@ module.exports = templatedArray('annotation', {
152152
valType: 'boolean',
153153
dflt: true,
154154
role: 'style',
155-
editType: 'calcIfAutorange+arraydraw',
155+
editType: 'calc+arraydraw',
156156
description: [
157157
'Determines whether or not the annotation is drawn with an arrow.',
158158
'If *true*, `text` is placed near the arrow\'s tail.',
@@ -197,7 +197,7 @@ module.exports = templatedArray('annotation', {
197197
min: 0.3,
198198
dflt: 1,
199199
role: 'style',
200-
editType: 'calcIfAutorange+arraydraw',
200+
editType: 'calc+arraydraw',
201201
description: [
202202
'Sets the size of the end annotation arrow head, relative to `arrowwidth`.',
203203
'A value of 1 (default) gives a head about 3x as wide as the line.'
@@ -208,7 +208,7 @@ module.exports = templatedArray('annotation', {
208208
min: 0.3,
209209
dflt: 1,
210210
role: 'style',
211-
editType: 'calcIfAutorange+arraydraw',
211+
editType: 'calc+arraydraw',
212212
description: [
213213
'Sets the size of the start annotation arrow head, relative to `arrowwidth`.',
214214
'A value of 1 (default) gives a head about 3x as wide as the line.'
@@ -218,15 +218,15 @@ module.exports = templatedArray('annotation', {
218218
valType: 'number',
219219
min: 0.1,
220220
role: 'style',
221-
editType: 'calcIfAutorange+arraydraw',
221+
editType: 'calc+arraydraw',
222222
description: 'Sets the width (in px) of annotation arrow line.'
223223
},
224224
standoff: {
225225
valType: 'number',
226226
min: 0,
227227
dflt: 0,
228228
role: 'style',
229-
editType: 'calcIfAutorange+arraydraw',
229+
editType: 'calc+arraydraw',
230230
description: [
231231
'Sets a distance, in pixels, to move the end arrowhead away from the',
232232
'position it is pointing at, for example to point at the edge of',
@@ -240,7 +240,7 @@ module.exports = templatedArray('annotation', {
240240
min: 0,
241241
dflt: 0,
242242
role: 'style',
243-
editType: 'calcIfAutorange+arraydraw',
243+
editType: 'calc+arraydraw',
244244
description: [
245245
'Sets a distance, in pixels, to move the start arrowhead away from the',
246246
'position it is pointing at, for example to point at the edge of',
@@ -252,7 +252,7 @@ module.exports = templatedArray('annotation', {
252252
ax: {
253253
valType: 'any',
254254
role: 'info',
255-
editType: 'calcIfAutorange+arraydraw',
255+
editType: 'calc+arraydraw',
256256
description: [
257257
'Sets the x component of the arrow tail about the arrow head.',
258258
'If `axref` is `pixel`, a positive (negative) ',
@@ -265,7 +265,7 @@ module.exports = templatedArray('annotation', {
265265
ay: {
266266
valType: 'any',
267267
role: 'info',
268-
editType: 'calcIfAutorange+arraydraw',
268+
editType: 'calc+arraydraw',
269269
description: [
270270
'Sets the y component of the arrow tail about the arrow head.',
271271
'If `ayref` is `pixel`, a positive (negative) ',
@@ -332,7 +332,7 @@ module.exports = templatedArray('annotation', {
332332
x: {
333333
valType: 'any',
334334
role: 'info',
335-
editType: 'calcIfAutorange+arraydraw',
335+
editType: 'calc+arraydraw',
336336
description: [
337337
'Sets the annotation\'s x position.',
338338
'If the axis `type` is *log*, then you must take the',
@@ -350,7 +350,7 @@ module.exports = templatedArray('annotation', {
350350
values: ['auto', 'left', 'center', 'right'],
351351
dflt: 'auto',
352352
role: 'info',
353-
editType: 'calcIfAutorange+arraydraw',
353+
editType: 'calc+arraydraw',
354354
description: [
355355
'Sets the text box\'s horizontal position anchor',
356356
'This anchor binds the `x` position to the *left*, *center*',
@@ -369,7 +369,7 @@ module.exports = templatedArray('annotation', {
369369
valType: 'number',
370370
dflt: 0,
371371
role: 'style',
372-
editType: 'calcIfAutorange+arraydraw',
372+
editType: 'calc+arraydraw',
373373
description: [
374374
'Shifts the position of the whole annotation and arrow to the',
375375
'right (positive) or left (negative) by this many pixels.'
@@ -395,7 +395,7 @@ module.exports = templatedArray('annotation', {
395395
y: {
396396
valType: 'any',
397397
role: 'info',
398-
editType: 'calcIfAutorange+arraydraw',
398+
editType: 'calc+arraydraw',
399399
description: [
400400
'Sets the annotation\'s y position.',
401401
'If the axis `type` is *log*, then you must take the',
@@ -413,7 +413,7 @@ module.exports = templatedArray('annotation', {
413413
values: ['auto', 'top', 'middle', 'bottom'],
414414
dflt: 'auto',
415415
role: 'info',
416-
editType: 'calcIfAutorange+arraydraw',
416+
editType: 'calc+arraydraw',
417417
description: [
418418
'Sets the text box\'s vertical position anchor',
419419
'This anchor binds the `y` position to the *top*, *middle*',
@@ -432,7 +432,7 @@ module.exports = templatedArray('annotation', {
432432
valType: 'number',
433433
dflt: 0,
434434
role: 'style',
435-
editType: 'calcIfAutorange+arraydraw',
435+
editType: 'calc+arraydraw',
436436
description: [
437437
'Shifts the position of the whole annotation and arrow up',
438438
'(positive) or down (negative) by this many pixels.'

src/components/annotations/calc_autorange.js

+10-24
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,11 @@ var draw = require('./draw').draw;
1616

1717

1818
module.exports = function calcAutorange(gd) {
19-
var fullLayout = gd._fullLayout,
20-
annotationList = Lib.filterVisible(fullLayout.annotations);
21-
22-
if(!annotationList.length || !gd._fullData.length) return;
23-
24-
var annotationAxes = {};
25-
annotationList.forEach(function(ann) {
26-
annotationAxes[ann.xref] = 1;
27-
annotationAxes[ann.yref] = 1;
28-
});
19+
var fullLayout = gd._fullLayout;
20+
var annotationList = Lib.filterVisible(fullLayout.annotations);
2921

30-
for(var axId in annotationAxes) {
31-
var ax = Axes.getFromId(gd, axId);
32-
if(ax && ax.autorange) {
33-
return Lib.syncOrAsync([
34-
draw,
35-
annAutorange
36-
], gd);
37-
}
22+
if(annotationList.length && gd._fullData.length) {
23+
return Lib.syncOrAsync([draw, annAutorange], gd);
3824
}
3925
};
4026

@@ -46,14 +32,14 @@ function annAutorange(gd) {
4632
// use the arrow and the text bg rectangle,
4733
// as the whole anno may include hidden text in its bbox
4834
Lib.filterVisible(fullLayout.annotations).forEach(function(ann) {
49-
var xa = Axes.getFromId(gd, ann.xref),
50-
ya = Axes.getFromId(gd, ann.yref),
51-
headSize = 3 * ann.arrowsize * ann.arrowwidth || 0,
52-
startHeadSize = 3 * ann.startarrowsize * ann.arrowwidth || 0;
35+
var xa = Axes.getFromId(gd, ann.xref);
36+
var ya = Axes.getFromId(gd, ann.yref);
37+
var headSize = 3 * ann.arrowsize * ann.arrowwidth || 0;
38+
var startHeadSize = 3 * ann.startarrowsize * ann.arrowwidth || 0;
5339

5440
var headPlus, headMinus, startHeadPlus, startHeadMinus;
5541

56-
if(xa && xa.autorange) {
42+
if(xa) {
5743
headPlus = headSize + ann.xshift;
5844
headMinus = headSize - ann.xshift;
5945
startHeadPlus = startHeadSize + ann.xshift;
@@ -81,7 +67,7 @@ function annAutorange(gd) {
8167
}
8268
}
8369

84-
if(ya && ya.autorange) {
70+
if(ya) {
8571
headPlus = headSize - ann.yshift;
8672
headMinus = headSize + ann.yshift;
8773
startHeadPlus = startHeadSize - ann.yshift;

src/components/annotations/draw.js

+6-12
Original file line numberDiff line numberDiff line change
@@ -300,25 +300,17 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
300300
* otherwise the text anchor point
301301
*/
302302
if(ax) {
303-
/*
304-
* hide the annotation if it's pointing outside the visible plot
305-
* as long as the axis isn't autoranged - then we need to draw it
306-
* anyway to get its bounding box. When we're dragging, an axis can
307-
* still look autoranged even though it won't be when the drag finishes.
308-
*/
303+
// check if annotation is off screen, to bypass DOM manipulations
309304
var posFraction = ax.r2fraction(options[axLetter]);
310-
if((gd._dragging || !ax.autorange) && (posFraction < 0 || posFraction > 1)) {
305+
if(posFraction < 0 || posFraction > 1) {
311306
if(tailRef === axRef) {
312307
posFraction = ax.r2fraction(options['a' + axLetter]);
313308
if(posFraction < 0 || posFraction > 1) {
314309
annotationIsOffscreen = true;
315310
}
316-
}
317-
else {
311+
} else {
318312
annotationIsOffscreen = true;
319313
}
320-
321-
if(annotationIsOffscreen) continue;
322314
}
323315
basePx = ax._offset + ax.r2p(options[axLetter]);
324316
autoAlignFraction = 0.5;
@@ -402,7 +394,9 @@ function drawRaw(gd, options, index, subplotId, xa, ya) {
402394
options['_' + axLetter + 'shift'] = textShift;
403395
}
404396

405-
if(annotationIsOffscreen) {
397+
// We have everything we need for calcAutorange at this point,
398+
// we can safely exit - unless we're currently dragging the plot
399+
if(!gd._dragging && annotationIsOffscreen) {
406400
annTextGroupInner.remove();
407401
return;
408402
}

test/jasmine/tests/annotations_test.js

+37
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,43 @@ describe('annotations autorange', function() {
737737
.catch(failTest)
738738
.then(done);
739739
});
740+
741+
it('should propagate axis autorange changes when axis ranges are set', function(done) {
742+
function _assert(msg, xrng, yrng) {
743+
var fullLayout = gd._fullLayout;
744+
expect(fullLayout.xaxis.range).toBeCloseToArray(xrng, 1, msg + ' xrng');
745+
expect(fullLayout.yaxis.range).toBeCloseToArray(yrng, 1, msg + ' yrng');
746+
}
747+
748+
Plotly.plot(gd, [{y: [1, 2]}], {
749+
xaxis: {range: [0, 2]},
750+
yaxis: {range: [0, 2]},
751+
annotations: [{
752+
text: 'a',
753+
x: 3, y: 3
754+
}]
755+
})
756+
.then(function() {
757+
_assert('set rng / small tx', [0, 2], [0, 2]);
758+
return Plotly.relayout(gd, 'annotations[0].text', 'loooooooooooooooooooooooong');
759+
})
760+
.then(function() {
761+
_assert('set rng / big tx', [0, 2], [0, 2]);
762+
return Plotly.relayout(gd, {
763+
'xaxis.autorange': true,
764+
'yaxis.autorange': true
765+
});
766+
})
767+
.then(function() {
768+
_assert('auto rng / big tx', [-0.22, 3.57], [0.84, 3.365]);
769+
return Plotly.relayout(gd, 'annotations[0].text', 'a');
770+
})
771+
.then(function() {
772+
_assert('auto rng / small tx', [-0.18, 3.035], [0.84, 3.365]);
773+
})
774+
.catch(failTest)
775+
.then(done);
776+
});
740777
});
741778

742779
describe('annotation clicktoshow', function() {

0 commit comments

Comments
 (0)