Skip to content

Commit 6f685c0

Browse files
committed
fix text scale and pad for rounded bars
1 parent 99352d0 commit 6f685c0

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/traces/bar/plot.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ function appendBarText(gd, plotinfo, bar, cd, i, x0, x1, y0, y1, lxFunc, lyFunc,
441441
var isHorizontal = (trace.orientation === 'h');
442442

443443
var text = getText(fullLayout, cd, i, xa, ya);
444+
444445
textPosition = getTextPosition(trace, i);
445446

446447
// compute text position
@@ -754,28 +755,32 @@ function scaleTextForRoundedBar(x0, x1, y0, y1, t, r, isHorizontal, hasB) {
754755
} else if(!hasB && isHorizontal) {
755756
// Case 3a (Quadratic case, two side corners are rounded)
756757
a = t.x * t.x + t.y * t.y / 4;
757-
b = t.y * (2 * R - barHeight) + 2 * t.x * (R - barWidth);
758-
c = (R - barHeight / 2) * (R - barHeight / 2) + (R - barWidth) * (R - barWidth) - R * R;
758+
b = -2 * t.x * (barWidth - R) - t.y * (barHeight / 2 - R);
759+
c = (barWidth - R) * (barWidth - R) + (barHeight / 2 - R) * (barHeight / 2 - R) - R * R;
760+
759761
scale = (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a);
760762
} else if(!hasB) {
761763
// Case 3b (Quadratic case, two top/bottom corners are rounded)
762764
a = t.x * t.x / 4 + t.y * t.y;
763-
b = 2 * t.x * (R - barHeight) + t.y * (2 * R - barWidth);
764-
c = (R - barHeight) * (R - barHeight) + (R - barWidth / 2) * (R - barWidth / 2) - R * R;
765+
b = -t.x * (barWidth / 2 - R) - 2 * t.y * (barHeight - R);
766+
c = (barWidth / 2 - R) * (barWidth / 2 - R) + (barHeight - R) * (barHeight - R) - R * R;
767+
765768
scale = (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a);
766769
} else {
767770
// Case 4 (Quadratic case, all four corners are rounded)
768-
// TODO: This gives a scale factor that's way too large, text overflows boundaries
769771
a = (t.x * t.x + t.y * t.y) / 4;
770-
b = t.y * (2 * R - barHeight) + t.x * (2 * R - barWidth);
771-
c = (R - barHeight / 2) * (R - barHeight / 2) + (R - barWidth / 2) * (R - barWidth / 2) - R * R;
772+
b = -t.x * (barWidth / 2 - R) - t.y * (barHeight / 2 - R);
773+
c = (barWidth / 2 - R) * (barWidth / 2 - R) + (barHeight / 2 - R) * (barHeight / 2 - R) - R * R;
772774
scale = (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a);
773775
}
774776

777+
// Scale should not be larger than 1
778+
scale = Math.min(1, scale);
779+
775780
if(isHorizontal) {
776-
pad = Math.max(0, R - Math.sqrt((barHeight - t.y * scale) * (R + (barHeight - t.y * scale) / 4))) || 0;
781+
pad = R - Math.sqrt(R * R - (R - barHeight / 2 + t.y * scale / 2) * (R - barHeight / 2 + t.y * scale / 2));
777782
} else {
778-
pad = Math.max(0, R - Math.sqrt((barWidth - t.x * scale) * (R + (barWidth - t.x * scale) / 4))) || 0;
783+
pad = R - Math.sqrt(R * R - (R - barWidth / 2 + t.x * scale / 2) * (R - barWidth / 2 + t.x * scale / 2));
779784
}
780785

781786
return { scale: scale, pad: pad };

0 commit comments

Comments
 (0)