Skip to content

Commit 0e3e6e2

Browse files
committed
simplify (Inside | Outside) transform logic in bar
1 parent 599de22 commit 0e3e6e2

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

src/traces/bar/plot.js

+24-24
Original file line numberDiff line numberDiff line change
@@ -424,18 +424,31 @@ function getRotateFromAngle(angle) {
424424
return (angle === 'auto') ? 0 : angle;
425425
}
426426

427+
function getRotatedTextSize(textBB, rotate) {
428+
var a = Math.PI / 180 * rotate;
429+
var absSin = Math.abs(Math.sin(a));
430+
var absCos = Math.abs(Math.cos(a));
431+
432+
return {
433+
x: textBB.width * absCos + textBB.height * absSin,
434+
y: textBB.width * absSin + textBB.height * absCos
435+
};
436+
}
437+
427438
function toMoveInsideBar(x0, x1, y0, y1, textBB, opts) {
428439
var isHorizontal = !!opts.isHorizontal;
429440
var constrained = !!opts.constrained;
430441
var angle = opts.angle || 0;
431-
var anchor = opts.anchor || 0;
442+
var anchor = opts.anchor || 'end';
443+
var isEnd = anchor === 'end';
444+
var isStart = anchor === 'start';
432445

433446
var textWidth = textBB.width;
434447
var textHeight = textBB.height;
435448
var lx = Math.abs(x1 - x0);
436449
var ly = Math.abs(y1 - y0);
437450

438-
// compute left space
451+
// compute remaining space
439452
var textpad = (
440453
lx > (2 * TEXTPAD) &&
441454
ly > (2 * TEXTPAD)
@@ -463,38 +476,29 @@ function toMoveInsideBar(x0, x1, y0, y1, textBB, opts) {
463476
}
464477

465478
var rotate = getRotateFromAngle(angle);
466-
var absSin = Math.abs(Math.sin(Math.PI / 180 * rotate));
467-
var absCos = Math.abs(Math.cos(Math.PI / 180 * rotate));
468-
469-
// compute text space
470-
var tx = textWidth * absCos + textHeight * absSin;
471-
var ty = textWidth * absSin + textHeight * absCos;
479+
var t = getRotatedTextSize(textBB, rotate);
472480

473481
var scale = 1;
474482
if(constrained) {
475483
scale = Math.min(
476484
1,
477-
lx / tx,
478-
ly / ty
485+
lx / t.x,
486+
ly / t.y
479487
);
480-
scale = Math.min(1, scale);
481488
}
482489

483490
// compute text and target positions
484491
var targetX = (x0 + x1) / 2;
485492
var targetY = (y0 + y1) / 2;
486-
487-
if(anchor !== 'middle') { // case of 'start' or 'end'
488-
var targetWidth = scale * (isHorizontal !== isAutoRotated ? textHeight : textWidth);
489-
var targetHeight = scale * (isHorizontal !== isAutoRotated ? textWidth : textHeight);
490-
textpad += 0.5 * (targetWidth * absSin + targetHeight * absCos);
493+
if(isStart || isEnd) {
494+
textpad += 0.5 * scale * (isHorizontal !== isAutoRotated ? t.x : t.y);
491495

492496
if(isHorizontal) {
493497
textpad *= dirSign(x0, x1);
494-
targetX = (anchor === 'start') ? x0 + textpad : x1 - textpad;
498+
targetX = isStart ? x0 + textpad : x1 - textpad;
495499
} else {
496500
textpad *= dirSign(y0, y1);
497-
targetY = (anchor === 'start') ? y0 + textpad : y1 - textpad;
501+
targetY = isStart ? y0 + textpad : y1 - textpad;
498502
}
499503
}
500504

@@ -542,14 +546,10 @@ function toMoveOutsideBar(x0, x1, y0, y1, textBB, opts) {
542546
}
543547

544548
var rotate = getRotateFromAngle(angle);
545-
var absSin = Math.abs(Math.sin(Math.PI / 180 * rotate));
546-
var absCos = Math.abs(Math.cos(Math.PI / 180 * rotate));
549+
var t = getRotatedTextSize(textBB, rotate);
547550

548551
// compute text and target positions
549-
var targetWidth = scale * (isHorizontal ? textHeight : textWidth);
550-
var targetHeight = scale * (isHorizontal ? textWidth : textHeight);
551-
textpad += 0.5 * (targetWidth * absSin + targetHeight * absCos);
552-
552+
textpad += 0.5 * scale * (isHorizontal ? t.x : t.y);
553553
var targetX = (x0 + x1) / 2;
554554
var targetY = (y0 + y1) / 2;
555555

0 commit comments

Comments
 (0)