Skip to content

Commit 4335ba0

Browse files
committed
more permissive text inside/outside logic for rounded bars
1 parent c52890c commit 4335ba0

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

src/traces/bar/plot.js

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -473,19 +473,6 @@ function appendBarText(gd, plotinfo, bar, cd, i, x0, x1, y0, y1, r, overhead, op
473473
var barWidth = lx - 2 * TEXTPAD;
474474
var barHeight = ly - 2 * TEXTPAD;
475475

476-
// If corners are rounded, subtract extra from barWidth and barHeight
477-
// to account for rounding
478-
if(barIsRounded) {
479-
if(hasB) {
480-
barWidth -= 2 * r;
481-
barHeight -= 2 * r;
482-
} else if(isHorizontal) {
483-
barWidth -= r - overhead;
484-
} else {
485-
barHeight -= r - overhead;
486-
}
487-
}
488-
489476
var textSelection;
490477

491478
var textBB;
@@ -511,17 +498,31 @@ function appendBarText(gd, plotinfo, bar, cd, i, x0, x1, y0, y1, r, overhead, op
511498
textHeight = textBB.height;
512499

513500
var textHasSize = (textWidth > 0 && textHeight > 0);
514-
var fitsInside = (textWidth <= barWidth && textHeight <= barHeight);
515-
var fitsInsideIfRotated = (textWidth <= barHeight && textHeight <= barWidth);
516-
var fitsInsideIfShrunk = (isHorizontal) ?
517-
(barWidth >= textWidth * (barHeight / textHeight)) :
518-
(barHeight >= textHeight * (barWidth / textWidth));
519-
520-
if(textHasSize && (
521-
fitsInside ||
522-
fitsInsideIfRotated ||
523-
fitsInsideIfShrunk)
524-
) {
501+
502+
var fitsInside;
503+
if(barIsRounded) {
504+
// If bar is rounded, check if text fits between rounded corners
505+
if(hasB) {
506+
fitsInside = (
507+
textfitsInsideBar(barWidth - 2 * r, barHeight, textWidth, textHeight, isHorizontal) ||
508+
textfitsInsideBar(barWidth, barHeight - 2 * r, textWidth, textHeight, isHorizontal)
509+
);
510+
} else if(isHorizontal) {
511+
fitsInside = (
512+
textfitsInsideBar(barWidth - (r - overhead), barHeight, textWidth, textHeight, isHorizontal) ||
513+
textfitsInsideBar(barWidth, barHeight - 2 * (r - overhead), textWidth, textHeight, isHorizontal)
514+
);
515+
} else {
516+
fitsInside = (
517+
textfitsInsideBar(barWidth, barHeight - (r - overhead), textWidth, textHeight, isHorizontal) ||
518+
textfitsInsideBar(barWidth - 2 * (r - overhead), barHeight, textWidth, textHeight, isHorizontal)
519+
);
520+
}
521+
} else {
522+
fitsInside = textfitsInsideBar(barWidth, barHeight, textWidth, textHeight, isHorizontal);
523+
}
524+
525+
if(textHasSize && fitsInside) {
525526
textPosition = 'inside';
526527
} else {
527528
textPosition = 'outside';
@@ -589,6 +590,16 @@ function appendBarText(gd, plotinfo, bar, cd, i, x0, x1, y0, y1, r, overhead, op
589590
Lib.setTransormAndDisplay(s, transform);
590591
}
591592

593+
function textfitsInsideBar(barWidth, barHeight, textWidth, textHeight, isHorizontal) {
594+
if(barWidth < 0 || barHeight < 0) return false;
595+
var fitsInside = (textWidth <= barWidth && textHeight <= barHeight);
596+
var fitsInsideIfRotated = (textWidth <= barHeight && textHeight <= barWidth);
597+
var fitsInsideIfShrunk = (isHorizontal) ?
598+
(barWidth >= textWidth * (barHeight / textHeight)) :
599+
(barHeight >= textHeight * (barWidth / textWidth));
600+
return fitsInside || fitsInsideIfRotated || fitsInsideIfShrunk;
601+
}
602+
592603
function getRotateFromAngle(angle) {
593604
return (angle === 'auto') ? 0 : angle;
594605
}

0 commit comments

Comments
 (0)