Skip to content

Commit 38cd9e2

Browse files
authored
Merge pull request #4081 from plotly/make-bar-text-transform-reusable-for-transitions
Revise the output of bar text transform functions
2 parents 76f7652 + 6a84220 commit 38cd9e2

File tree

2 files changed

+49
-26
lines changed

2 files changed

+49
-26
lines changed

src/traces/bar/plot.js

+45-23
Original file line numberDiff line numberDiff line change
@@ -340,32 +340,32 @@ function appendBarText(gd, plotinfo, bar, calcTrace, i, x0, x1, y0, y1, opts) {
340340
trace.constraintext === 'both' ||
341341
trace.constraintext === 'outside';
342342

343-
transform = getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB, {
343+
transform = getTransform(toMoveOutsideBar(x0, x1, y0, y1, textBB, {
344344
isHorizontal: isHorizontal,
345345
constrained: constrained,
346346
angle: trace.textangle
347-
});
347+
}));
348348
} else {
349349
constrained =
350350
trace.constraintext === 'both' ||
351351
trace.constraintext === 'inside';
352352

353-
transform = getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, {
353+
transform = getTransform(toMoveInsideBar(x0, x1, y0, y1, textBB, {
354354
isHorizontal: isHorizontal,
355355
constrained: constrained,
356356
angle: trace.textangle,
357357
anchor: trace.insidetextanchor
358-
});
358+
}));
359359
}
360360

361361
textSelection.attr('transform', transform);
362362
}
363363

364-
function getRotationFromAngle(angle) {
364+
function getRotateFromAngle(angle) {
365365
return (angle === 'auto') ? 0 : angle;
366366
}
367367

368-
function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, opts) {
368+
function toMoveInsideBar(x0, x1, y0, y1, textBB, opts) {
369369
var isHorizontal = !!opts.isHorizontal;
370370
var constrained = !!opts.constrained;
371371
var angle = opts.angle || 0;
@@ -402,9 +402,9 @@ function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, opts) {
402402
lx = tmp;
403403
}
404404

405-
var rotation = getRotationFromAngle(angle);
406-
var absSin = Math.abs(Math.sin(Math.PI / 180 * rotation));
407-
var absCos = Math.abs(Math.cos(Math.PI / 180 * rotation));
405+
var rotate = getRotateFromAngle(angle);
406+
var absSin = Math.abs(Math.sin(Math.PI / 180 * rotate));
407+
var absCos = Math.abs(Math.cos(Math.PI / 180 * rotate));
408408

409409
// compute and apply text padding
410410
var dx = Math.max(lx * absCos, ly * absSin);
@@ -438,12 +438,19 @@ function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, opts) {
438438
var textY = (textBB.top + textBB.bottom) / 2;
439439

440440
// lastly apply auto rotation
441-
if(isAutoRotated) rotation += 90;
442-
443-
return getTransform(textX, textY, targetX, targetY, scale, rotation);
441+
if(isAutoRotated) rotate += 90;
442+
443+
return {
444+
textX: textX,
445+
textY: textY,
446+
targetX: targetX,
447+
targetY: targetY,
448+
scale: scale,
449+
rotate: rotate
450+
};
444451
}
445452

446-
function getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB, opts) {
453+
function toMoveOutsideBar(x0, x1, y0, y1, textBB, opts) {
447454
var isHorizontal = !!opts.isHorizontal;
448455
var constrained = !!opts.constrained;
449456
var angle = opts.angle || 0;
@@ -462,17 +469,17 @@ function getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB, opts) {
462469
textpad = (lx > 2 * TEXTPAD) ? TEXTPAD : 0;
463470
}
464471

465-
// compute rotation and scale
472+
// compute rotate and scale
466473
var scale = 1;
467474
if(constrained) {
468475
scale = (isHorizontal) ?
469476
Math.min(1, ly / textHeight) :
470477
Math.min(1, lx / textWidth);
471478
}
472479

473-
var rotation = getRotationFromAngle(angle);
474-
var absSin = Math.abs(Math.sin(Math.PI / 180 * rotation));
475-
var absCos = Math.abs(Math.cos(Math.PI / 180 * rotation));
480+
var rotate = getRotateFromAngle(angle);
481+
var absSin = Math.abs(Math.sin(Math.PI / 180 * rotate));
482+
var absCos = Math.abs(Math.cos(Math.PI / 180 * rotate));
476483

477484
// compute text and target positions
478485
var targetWidth = scale * (isHorizontal ? textHeight : textWidth);
@@ -491,10 +498,24 @@ function getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB, opts) {
491498
var textX = (textBB.left + textBB.right) / 2;
492499
var textY = (textBB.top + textBB.bottom) / 2;
493500

494-
return getTransform(textX, textY, targetX, targetY, scale, rotation);
501+
return {
502+
textX: textX,
503+
textY: textY,
504+
targetX: targetX,
505+
targetY: targetY,
506+
scale: scale,
507+
rotate: rotate
508+
};
495509
}
496510

497-
function getTransform(textX, textY, targetX, targetY, scale, rotation) {
511+
function getTransform(opts) {
512+
var textX = opts.textX;
513+
var textY = opts.textY;
514+
var targetX = opts.targetX;
515+
var targetY = opts.targetY;
516+
var scale = opts.scale;
517+
var rotate = opts.rotate;
518+
498519
var transformScale;
499520
var transformRotate;
500521
var transformTranslate;
@@ -505,8 +526,8 @@ function getTransform(textX, textY, targetX, targetY, scale, rotation) {
505526
transformScale = '';
506527
}
507528

508-
transformRotate = (rotation) ?
509-
'rotate(' + rotation + ' ' + textX + ' ' + textY + ') ' : '';
529+
transformRotate = (rotate) ?
530+
'rotate(' + rotate + ' ' + textX + ' ' + textY + ') ' : '';
510531

511532
// Note that scaling also affects the center of the text box
512533
var translateX = (targetX - scale * textX);
@@ -610,6 +631,7 @@ function calcTextinfo(calcTrace, index, xa, ya) {
610631

611632
module.exports = {
612633
plot: plot,
613-
getTransformToMoveInsideBar: getTransformToMoveInsideBar,
614-
getTransformToMoveOutsideBar: getTransformToMoveOutsideBar
634+
getTransform: getTransform,
635+
toMoveInsideBar: toMoveInsideBar,
636+
toMoveOutsideBar: toMoveOutsideBar
615637
};

src/traces/funnelarea/plot.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ var Lib = require('../../lib');
1515
var svgTextUtils = require('../../lib/svg_text_utils');
1616

1717
var barPlot = require('../bar/plot');
18-
var getTransformToMoveInsideBar = barPlot.getTransformToMoveInsideBar;
18+
var getTransform = barPlot.getTransform;
19+
var toMoveInsideBar = barPlot.toMoveInsideBar;
1920

2021
var pieHelpers = require('../pie/helpers');
2122
var piePlot = require('../pie/plot');
@@ -113,12 +114,12 @@ module.exports = function plot(gd, cdModule) {
113114
x0 = Math.max(pt.TL[0], pt.BL[0]);
114115
x1 = Math.min(pt.TR[0], pt.BR[0]);
115116

116-
transform = getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, {
117+
transform = getTransform(toMoveInsideBar(x0, x1, y0, y1, textBB, {
117118
isHorizontal: true,
118119
constrained: true,
119120
angle: 0,
120121
anchor: 'middle'
121-
});
122+
}));
122123

123124
sliceText.attr('transform',
124125
'translate(' + cx + ',' + cy + ')' + transform

0 commit comments

Comments
 (0)