Skip to content

Commit 8518ee9

Browse files
committed
refactor annotations.draw, add standoff and anchor with arrow
1 parent b823573 commit 8518ee9

File tree

10 files changed

+290
-225
lines changed

10 files changed

+290
-225
lines changed

src/components/annotations/annotation_defaults.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ module.exports = function handleAnnotationDefaults(annIn, annOut, fullLayout, op
7575
}
7676

7777
// xanchor, yanchor
78-
else coerce(axLetter + 'anchor');
78+
coerce(axLetter + 'anchor');
7979
}
8080

8181
// if you have one coordinate you should have both
@@ -86,6 +86,7 @@ module.exports = function handleAnnotationDefaults(annIn, annOut, fullLayout, op
8686
coerce('arrowhead');
8787
coerce('arrowsize');
8888
coerce('arrowwidth', ((borderOpacity && borderWidth) || 1) * 2);
89+
coerce('standoff');
8990

9091
// if you have one part of arrow length you should have both
9192
Lib.noneOrAll(annIn, annOut, ['ax', 'ay']);

src/components/annotations/arrow_paths.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121

2222
module.exports = [
2323
// no arrow
24-
'',
24+
{
25+
path: '',
26+
backoff: 0
27+
},
2528
// wide with flat back
2629
{
2730
path: 'M-2.4,-3V3L0.6,0Z',

src/components/annotations/attributes.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ module.exports = {
140140
role: 'style',
141141
description: 'Sets the width (in px) of annotation arrow.'
142142
},
143+
standoff: {
144+
valType: 'number',
145+
min: 0,
146+
dflt: 0,
147+
role: 'style',
148+
description: [
149+
'Sets a distance, in pixels, to move the arrowhead away from the',
150+
'position it is pointing at, for example to point at the edge of',
151+
'a marker independent of zoom.'
152+
].join(' ')
153+
},
143154
ax: {
144155
valType: 'any',
145156
role: 'info',
@@ -236,17 +247,17 @@ module.exports = {
236247
dflt: 'auto',
237248
role: 'info',
238249
description: [
239-
'Sets the annotation\'s horizontal position anchor',
250+
'Sets the text box\'s horizontal position anchor',
240251
'This anchor binds the `x` position to the *left*, *center*',
241252
'or *right* of the annotation.',
242253
'For example, if `x` is set to 1, `xref` to *paper* and',
243254
'`xanchor` to *right* then the right-most portion of the',
244255
'annotation lines up with the right-most edge of the',
245256
'plotting area.',
246257
'If *auto*, the anchor is equivalent to *center* for',
247-
'data-referenced annotations',
248-
'whereas for paper-referenced, the anchor picked corresponds',
249-
'to the closest side.'
258+
'data-referenced annotations or if there is an arrow,',
259+
'whereas for paper-referenced with no arrow, the anchor picked',
260+
'corresponds to the closest side.'
250261
].join(' ')
251262
},
252263
yref: {
@@ -286,17 +297,17 @@ module.exports = {
286297
dflt: 'auto',
287298
role: 'info',
288299
description: [
289-
'Sets the annotation\'s vertical position anchor',
300+
'Sets the text box\'s vertical position anchor',
290301
'This anchor binds the `y` position to the *top*, *middle*',
291302
'or *bottom* of the annotation.',
292303
'For example, if `y` is set to 1, `yref` to *paper* and',
293304
'`yanchor` to *top* then the top-most portion of the',
294305
'annotation lines up with the top-most edge of the',
295306
'plotting area.',
296307
'If *auto*, the anchor is equivalent to *middle* for',
297-
'data-referenced annotations',
298-
'whereas for paper-referenced, the anchor picked corresponds',
299-
'to the closest side.'
308+
'data-referenced annotations or if there is an arrow,',
309+
'whereas for paper-referenced with no arrow, the anchor picked',
310+
'corresponds to the closest side.'
300311
].join(' ')
301312
},
302313

src/components/annotations/calc_autorange.js

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,39 +47,47 @@ function annAutorange(gd) {
4747
// as the whole anno may include hidden text in its bbox
4848
fullLayout.annotations.forEach(function(ann) {
4949
var xa = Axes.getFromId(gd, ann.xref),
50-
ya = Axes.getFromId(gd, ann.yref);
51-
52-
if(!(xa || ya)) return;
53-
54-
var halfWidth = (ann._xsize || 0) / 2,
55-
xShift = ann._xshift || 0,
56-
halfHeight = (ann._ysize || 0) / 2,
57-
yShift = ann._yshift || 0,
58-
leftSize = halfWidth - xShift,
59-
rightSize = halfWidth + xShift,
60-
topSize = halfHeight - yShift,
61-
bottomSize = halfHeight + yShift;
62-
63-
if(ann.showarrow) {
64-
var headSize = 3 * ann.arrowsize * ann.arrowwidth;
65-
leftSize = Math.max(leftSize, headSize);
66-
rightSize = Math.max(rightSize, headSize);
67-
topSize = Math.max(topSize, headSize);
68-
bottomSize = Math.max(bottomSize, headSize);
69-
}
50+
ya = Axes.getFromId(gd, ann.yref),
51+
headSize = 3 * ann.arrowsize * ann.arrowwidth || 0;
7052

7153
if(xa && xa.autorange) {
72-
Axes.expand(xa, [xa.r2c(ann.x)], {
73-
ppadplus: rightSize,
74-
ppadminus: leftSize
75-
});
54+
if(ann.axref === ann.xref) {
55+
// expand for the arrowhead (padded by arrowhead)
56+
Axes.expand(xa, [xa.r2c(ann.x)], {
57+
ppadplus: headSize,
58+
ppadminus: headSize
59+
});
60+
// again for the textbox (padded by textbox)
61+
Axes.expand(xa, [xa.r2c(ann.ax)], {
62+
ppadplus: ann._xpadplus,
63+
ppadminus: ann._xpadminus
64+
});
65+
}
66+
else {
67+
Axes.expand(xa, [xa.r2c(ann.x)], {
68+
ppadplus: Math.max(ann._xpadplus, headSize),
69+
ppadminus: Math.max(ann._xpadminus, headSize)
70+
});
71+
}
7672
}
7773

7874
if(ya && ya.autorange) {
79-
Axes.expand(ya, [ya.r2c(ann.y)], {
80-
ppadplus: bottomSize,
81-
ppadminus: topSize
82-
});
75+
if(ann.ayref === ann.yref) {
76+
Axes.expand(ya, [ya.r2c(ann.y)], {
77+
ppadplus: headSize,
78+
ppadminus: headSize
79+
});
80+
Axes.expand(ya, [ya.r2c(ann.ay)], {
81+
ppadplus: ann._ypadplus,
82+
ppadminus: ann._ypadminus
83+
});
84+
}
85+
else {
86+
Axes.expand(ya, [ya.r2c(ann.y)], {
87+
ppadplus: Math.max(ann._ypadplus, headSize),
88+
ppadminus: Math.max(ann._ypadminus, headSize)
89+
});
90+
}
8391
}
8492
});
8593
}

0 commit comments

Comments
 (0)