-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Annotation size & text alignment #1551
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
Changes from all commits
26b91b3
168ebee
efe8345
e30d23a
71112e6
e01a765
2e132bd
b21154b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,9 +72,14 @@ function drawOne(gd, index) { | |
var optionsIn = (layout.annotations || [])[index], | ||
options = fullLayout.annotations[index]; | ||
|
||
var annClipID = 'clip' + fullLayout._uid + '_ann' + index; | ||
|
||
// this annotation is gone - quit now after deleting it | ||
// TODO: use d3 idioms instead of deleting and redrawing every time | ||
if(!optionsIn || options.visible === false) return; | ||
if(!optionsIn || options.visible === false) { | ||
d3.selectAll('#' + annClipID).remove(); | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. easy. |
||
} | ||
|
||
var xa = Axes.getFromId(gd, options.xref), | ||
ya = Axes.getFromId(gd, options.yref), | ||
|
@@ -118,6 +123,18 @@ function drawOne(gd, index) { | |
.call(Color.stroke, options.bordercolor) | ||
.call(Color.fill, options.bgcolor); | ||
|
||
var isSizeConstrained = options.width || options.height; | ||
|
||
var annTextClip = fullLayout._defs.select('.clips') | ||
.selectAll('#' + annClipID) | ||
.data(isSizeConstrained ? [0] : []); | ||
|
||
annTextClip.enter().append('clipPath') | ||
.classed('annclip', true) | ||
.attr('id', annClipID) | ||
.append('rect'); | ||
annTextClip.exit().remove(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we add a jasmine test, checking that the clip paths are removed when removing an annotation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In fact they're not removed - they don't proliferate, ie if you repeatedly make 100 annotations and delete them, you'll never get more than 100 clip paths, but I didn't bother deleting clip paths corresponding to annotations past the end of the new array after removing some. I can revisit that though... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ha I see. We can revisit this when we shift to a more d3-idiomatic pattern in annotation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually was easy to remove the unneeded clip paths after all -> b21154b |
||
|
||
var font = options.font; | ||
|
||
var annText = annTextGroupInner.append('text') | ||
|
@@ -144,19 +161,21 @@ function drawOne(gd, index) { | |
// at the end, even if their position changes | ||
annText.selectAll('tspan.line').attr({y: 0, x: 0}); | ||
|
||
var mathjaxGroup = annTextGroupInner.select('.annotation-math-group'), | ||
hasMathjax = !mathjaxGroup.empty(), | ||
anntextBB = Drawing.bBox( | ||
(hasMathjax ? mathjaxGroup : annText).node()), | ||
annwidth = anntextBB.width, | ||
annheight = anntextBB.height, | ||
outerwidth = Math.round(annwidth + 2 * borderfull), | ||
outerheight = Math.round(annheight + 2 * borderfull); | ||
var mathjaxGroup = annTextGroupInner.select('.annotation-math-group'); | ||
var hasMathjax = !mathjaxGroup.empty(); | ||
var anntextBB = Drawing.bBox( | ||
(hasMathjax ? mathjaxGroup : annText).node()); | ||
var textWidth = anntextBB.width; | ||
var textHeight = anntextBB.height; | ||
var annWidth = options.width || textWidth; | ||
var annHeight = options.height || textHeight; | ||
var outerWidth = Math.round(annWidth + 2 * borderfull); | ||
var outerHeight = Math.round(annHeight + 2 * borderfull); | ||
|
||
|
||
// save size in the annotation object for use by autoscale | ||
options._w = annwidth; | ||
options._h = annheight; | ||
options._w = annWidth; | ||
options._h = annHeight; | ||
|
||
function shiftFraction(v, anchor) { | ||
if(anchor === 'auto') { | ||
|
@@ -181,8 +200,8 @@ function drawOne(gd, index) { | |
ax = Axes.getFromId(gd, axRef), | ||
dimAngle = (textangle + (axLetter === 'x' ? 0 : -90)) * Math.PI / 180, | ||
// note that these two can be either positive or negative | ||
annSizeFromWidth = outerwidth * Math.cos(dimAngle), | ||
annSizeFromHeight = outerheight * Math.sin(dimAngle), | ||
annSizeFromWidth = outerWidth * Math.cos(dimAngle), | ||
annSizeFromHeight = outerHeight * Math.sin(dimAngle), | ||
// but this one is the positive total size | ||
annSize = Math.abs(annSizeFromWidth) + Math.abs(annSizeFromHeight), | ||
anchor = options[axLetter + 'anchor'], | ||
|
@@ -299,22 +318,43 @@ function drawOne(gd, index) { | |
return; | ||
} | ||
|
||
var xShift = 0; | ||
var yShift = 0; | ||
|
||
if(options.align !== 'left') { | ||
xShift = (annWidth - textWidth) * (options.align === 'center' ? 0.5 : 1); | ||
} | ||
if(options.valign !== 'top') { | ||
yShift = (annHeight - textHeight) * (options.valign === 'middle' ? 0.5 : 1); | ||
} | ||
|
||
if(hasMathjax) { | ||
mathjaxGroup.select('svg').attr({x: borderfull - 1, y: borderfull}); | ||
mathjaxGroup.select('svg').attr({ | ||
x: borderfull + xShift - 1, | ||
y: borderfull + yShift | ||
}) | ||
.call(Drawing.setClipUrl, isSizeConstrained ? annClipID : null); | ||
} | ||
else { | ||
var texty = borderfull - anntextBB.top, | ||
textx = borderfull - anntextBB.left; | ||
annText.attr({x: textx, y: texty}); | ||
var texty = borderfull + yShift - anntextBB.top, | ||
textx = borderfull + xShift - anntextBB.left; | ||
annText.attr({ | ||
x: textx, | ||
y: texty | ||
}) | ||
.call(Drawing.setClipUrl, isSizeConstrained ? annClipID : null); | ||
annText.selectAll('tspan.line').attr({y: texty, x: textx}); | ||
} | ||
|
||
annTextClip.select('rect').call(Drawing.setRect, borderfull, borderfull, | ||
annWidth, annHeight); | ||
|
||
annTextBG.call(Drawing.setRect, borderwidth / 2, borderwidth / 2, | ||
outerwidth - borderwidth, outerheight - borderwidth); | ||
outerWidth - borderwidth, outerHeight - borderwidth); | ||
|
||
annTextGroupInner.call(Drawing.setTranslate, | ||
Math.round(annPosPx.x.text - outerwidth / 2), | ||
Math.round(annPosPx.y.text - outerheight / 2)); | ||
Math.round(annPosPx.x.text - outerWidth / 2), | ||
Math.round(annPosPx.y.text - outerHeight / 2)); | ||
|
||
/* | ||
* rotate text and background | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love it 👍