Skip to content

Annotation additions: standoff, anchor with arrow, clicktoshow #1265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jan 18, 2017
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/components/annotations/annotation_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ module.exports = function handleAnnotationDefaults(annIn, annOut, fullLayout, op
}

// xanchor, yanchor
else coerce(axLetter + 'anchor');
coerce(axLetter + 'anchor');
}

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

// if you have one part of arrow length you should have both
Lib.noneOrAll(annIn, annOut, ['ax', 'ay']);
Expand Down
5 changes: 4 additions & 1 deletion src/components/annotations/arrow_paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@

module.exports = [
// no arrow
'',
{
path: '',
backoff: 0
},
// wide with flat back
{
path: 'M-2.4,-3V3L0.6,0Z',
Expand Down
27 changes: 19 additions & 8 deletions src/components/annotations/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ module.exports = {
role: 'style',
description: 'Sets the width (in px) of annotation arrow.'
},
standoff: {
valType: 'number',
min: 0,
dflt: 0,
role: 'style',
description: [
'Sets a distance, in pixels, to move the arrowhead away from the',
'position it is pointing at, for example to point at the edge of',
'a marker independent of zoom.'
].join(' ')
},
ax: {
valType: 'any',
role: 'info',
Expand Down Expand Up @@ -236,17 +247,17 @@ module.exports = {
dflt: 'auto',
role: 'info',
description: [
'Sets the annotation\'s horizontal position anchor',
'Sets the text box\'s horizontal position anchor',
'This anchor binds the `x` position to the *left*, *center*',
'or *right* of the annotation.',
'For example, if `x` is set to 1, `xref` to *paper* and',
'`xanchor` to *right* then the right-most portion of the',
'annotation lines up with the right-most edge of the',
'plotting area.',
'If *auto*, the anchor is equivalent to *center* for',
'data-referenced annotations',
'whereas for paper-referenced, the anchor picked corresponds',
'to the closest side.'
'data-referenced annotations or if there is an arrow,',
'whereas for paper-referenced with no arrow, the anchor picked',
'corresponds to the closest side.'
].join(' ')
},
yref: {
Expand Down Expand Up @@ -286,17 +297,17 @@ module.exports = {
dflt: 'auto',
role: 'info',
description: [
'Sets the annotation\'s vertical position anchor',
'Sets the text box\'s vertical position anchor',
'This anchor binds the `y` position to the *top*, *middle*',
'or *bottom* of the annotation.',
'For example, if `y` is set to 1, `yref` to *paper* and',
'`yanchor` to *top* then the top-most portion of the',
'annotation lines up with the top-most edge of the',
'plotting area.',
'If *auto*, the anchor is equivalent to *middle* for',
'data-referenced annotations',
'whereas for paper-referenced, the anchor picked corresponds',
'to the closest side.'
'data-referenced annotations or if there is an arrow,',
'whereas for paper-referenced with no arrow, the anchor picked',
'corresponds to the closest side.'
].join(' ')
},

Expand Down
66 changes: 37 additions & 29 deletions src/components/annotations/calc_autorange.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,41 +45,49 @@ function annAutorange(gd) {
// relative to their anchor points
// use the arrow and the text bg rectangle,
// as the whole anno may include hidden text in its bbox
fullLayout.annotations.forEach(function(ann) {
Lib.filterVisible(fullLayout.annotations).forEach(function(ann) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lib.filterVisible was unnecessary before because we didn't coerce any other attributes when visible was false, so these annotations wouldn't look like they're on axes. But now I need the position and reference attributes even when annotations aren't visible in order to determine what to show on clicks, hence this filter is needed too.

var xa = Axes.getFromId(gd, ann.xref),
ya = Axes.getFromId(gd, ann.yref);

if(!(xa || ya)) return;

var halfWidth = (ann._xsize || 0) / 2,
xShift = ann._xshift || 0,
halfHeight = (ann._ysize || 0) / 2,
yShift = ann._yshift || 0,
leftSize = halfWidth - xShift,
rightSize = halfWidth + xShift,
topSize = halfHeight - yShift,
bottomSize = halfHeight + yShift;

if(ann.showarrow) {
var headSize = 3 * ann.arrowsize * ann.arrowwidth;
leftSize = Math.max(leftSize, headSize);
rightSize = Math.max(rightSize, headSize);
topSize = Math.max(topSize, headSize);
bottomSize = Math.max(bottomSize, headSize);
}
ya = Axes.getFromId(gd, ann.yref),
headSize = 3 * ann.arrowsize * ann.arrowwidth || 0;

if(xa && xa.autorange) {
Axes.expand(xa, [xa.r2c(ann.x)], {
ppadplus: rightSize,
ppadminus: leftSize
});
if(ann.axref === ann.xref) {
// expand for the arrowhead (padded by arrowhead)
Axes.expand(xa, [xa.r2c(ann.x)], {
ppadplus: headSize,
ppadminus: headSize
});
// again for the textbox (padded by textbox)
Axes.expand(xa, [xa.r2c(ann.ax)], {
ppadplus: ann._xpadplus,
ppadminus: ann._xpadminus
});
}
else {
Axes.expand(xa, [xa.r2c(ann.x)], {
ppadplus: Math.max(ann._xpadplus, headSize),
ppadminus: Math.max(ann._xpadminus, headSize)
});
}
}

if(ya && ya.autorange) {
Axes.expand(ya, [ya.r2c(ann.y)], {
ppadplus: bottomSize,
ppadminus: topSize
});
if(ann.ayref === ann.yref) {
Axes.expand(ya, [ya.r2c(ann.y)], {
ppadplus: headSize,
ppadminus: headSize
});
Axes.expand(ya, [ya.r2c(ann.ay)], {
ppadplus: ann._ypadplus,
ppadminus: ann._ypadminus
});
}
else {
Axes.expand(ya, [ya.r2c(ann.y)], {
ppadplus: Math.max(ann._ypadplus, headSize),
ppadminus: Math.max(ann._ypadminus, headSize)
});
}
}
});
}
Loading