Skip to content

Commit f093ee7

Browse files
committed
fix text position for stacked rounded bars
1 parent 6f685c0 commit f093ee7

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/traces/bar/plot.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ function plot(gd, plotinfo, cdModule, traceLayer, opts, makeOnCompleteCallback)
396396
Drawing.singlePointStyle(di, sel, trace, styleFns, gd);
397397
}
398398

399-
appendBarText(gd, plotinfo, bar, cd, i, x0, x1, y0, y1, lxFunc, lyFunc, r, opts, makeOnCompleteCallback);
399+
appendBarText(gd, plotinfo, bar, cd, i, x0, x1, y0, y1, lxFunc, lyFunc, r, overhead, opts, makeOnCompleteCallback);
400400

401401
if(plotinfo.layerClipId) {
402402
Drawing.hideOutsideRangePoint(di, bar.select('text'), xa, ya, trace.xcalendar, trace.ycalendar);
@@ -413,7 +413,7 @@ function plot(gd, plotinfo, cdModule, traceLayer, opts, makeOnCompleteCallback)
413413
Registry.getComponentMethod('errorbars', 'plot')(gd, bartraces, plotinfo, opts);
414414
}
415415

416-
function appendBarText(gd, plotinfo, bar, cd, i, x0, x1, y0, y1, lxFunc, lyFunc, r, opts, makeOnCompleteCallback) {
416+
function appendBarText(gd, plotinfo, bar, cd, i, x0, x1, y0, y1, lxFunc, lyFunc, r, overhead, opts, makeOnCompleteCallback) {
417417
var xa = plotinfo.xaxis;
418418
var ya = plotinfo.yaxis;
419419

@@ -598,6 +598,7 @@ function appendBarText(gd, plotinfo, bar, cd, i, x0, x1, y0, y1, lxFunc, lyFunc,
598598
lyFunc: lyFunc,
599599
hasB: hasB,
600600
r: r,
601+
overhead: overhead,
601602
});
602603
}
603604

@@ -636,6 +637,7 @@ function toMoveInsideBar(x0, x1, y0, y1, textBB, opts) {
636637
var toLeft = 1 - toRight;
637638
var hasB = opts.hasB;
638639
var r = opts.r;
640+
var overhead = opts.overhead;
639641

640642
var textWidth = textBB.width;
641643
var textHeight = textBB.height;
@@ -666,8 +668,8 @@ function toMoveInsideBar(x0, x1, y0, y1, textBB, opts) {
666668

667669
var scale, padForRounding;
668670
// Scale text for rounded bars
669-
if(r && r > TEXTPAD) {
670-
var scaleAndPad = scaleTextForRoundedBar(x0, x1, y0, y1, t, r, isHorizontal, hasB);
671+
if(r && (r - overhead) > TEXTPAD) {
672+
var scaleAndPad = scaleTextForRoundedBar(x0, x1, y0, y1, t, r, overhead, isHorizontal, hasB);
671673
scale = scaleAndPad.scale;
672674
padForRounding = scaleAndPad.pad;
673675
// Scale text for non-rounded bars
@@ -736,12 +738,13 @@ function toMoveInsideBar(x0, x1, y0, y1, textBB, opts) {
736738
};
737739
}
738740

739-
function scaleTextForRoundedBar(x0, x1, y0, y1, t, r, isHorizontal, hasB) {
741+
function scaleTextForRoundedBar(x0, x1, y0, y1, t, r, overhead, isHorizontal, hasB) {
740742
var barWidth = Math.max(0, Math.abs(x1 - x0) - 2 * TEXTPAD);
741743
var barHeight = Math.max(0, Math.abs(y1 - y0) - 2 * TEXTPAD);
742744
var R = r - TEXTPAD;
743-
var rX = hasB ? R * 2 : (isHorizontal ? R : 2 * R);
744-
var rY = hasB ? R * 2 : (isHorizontal ? 2 * R : R);
745+
var clippedR = overhead ? R - Math.sqrt(R * R - (R - overhead) * (R - overhead)) : R;
746+
var rX = hasB ? R * 2 : (isHorizontal ? R - overhead : 2 * clippedR);
747+
var rY = hasB ? R * 2 : (isHorizontal ? 2 * clippedR : R - overhead);
745748
var a, b, c;
746749
var scale, pad;
747750

@@ -778,9 +781,9 @@ function scaleTextForRoundedBar(x0, x1, y0, y1, t, r, isHorizontal, hasB) {
778781
scale = Math.min(1, scale);
779782

780783
if(isHorizontal) {
781-
pad = R - Math.sqrt(R * R - (R - barHeight / 2 + t.y * scale / 2) * (R - barHeight / 2 + t.y * scale / 2));
784+
pad = Math.max(0, R - Math.sqrt(Math.max(0, R * R - (R - (barHeight - t.y * scale) / 2) * (R - (barHeight - t.y * scale) / 2))) - overhead);
782785
} else {
783-
pad = R - Math.sqrt(R * R - (R - barWidth / 2 + t.x * scale / 2) * (R - barWidth / 2 + t.x * scale / 2));
786+
pad = Math.max(0, R - Math.sqrt(Math.max(0, R * R - (R - (barWidth - t.x * scale) / 2) * (R - (barWidth - t.x * scale) / 2))) - overhead);
784787
}
785788

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

0 commit comments

Comments
 (0)