Skip to content

Commit 9dd0896

Browse files
committed
better handling of text placement on rounded horizontal bars
1 parent 60c918e commit 9dd0896

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

src/traces/bar/plot.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ function plot(gd, plotinfo, cdModule, traceLayer, opts, makeOnCompleteCallback)
274274
// Default rectangular path (used if no rounding)
275275
var rectanglePath = 'M' + x0 + ',' + y0 + 'V' + y1 + 'H' + x1 + 'V' + y0 + 'Z';
276276
if(r && di.s) {
277-
// Bar has cornerradius
277+
// Bar has cornerradius, and nonzero size
278278
// Check amount of 'overhead' (bars stacked above this one)
279279
// to see whether we need to round or not
280280
var refPoint = Math.sign(di.s0) === 0 || Math.sign(di.s) === Math.sign(di.s0) ? di.s1 : di.s0;
@@ -299,6 +299,7 @@ function plot(gd, plotinfo, cdModule, traceLayer, opts, makeOnCompleteCallback)
299299
'A ' + r + ',' + r + ' 0 0 ' + cornersweep + ' ' + (x1 - r * xdir) + ',' + y0 +
300300
'Z';
301301
lyFunc = function(x) {
302+
if(x > r) return Math.abs(y1 - y0);
302303
var _dy2 = (x > 0) ? Math.sqrt(x * (2 * r - x)) : 0;
303304
return Math.abs(y1 - y0) - 2 * (r - _dy2);
304305
};
@@ -339,6 +340,7 @@ function plot(gd, plotinfo, cdModule, traceLayer, opts, makeOnCompleteCallback)
339340
'A ' + r + ',' + r + ' 0 0 ' + cornersweep + ' ' + (x1 - r * xdir) + ',' + y0 +
340341
'Z';
341342
lxFunc = function(y) {
343+
if(y > r) return Math.abs(x1 - x0);
342344
var _dx2 = (y > 0) ? Math.sqrt(y * (2 * r - y)) : 0;
343345
return Math.abs(x1 - x0) - 2 * (r - _dx2);
344346
};
@@ -453,6 +455,7 @@ function appendBarText(gd, plotinfo, bar, cd, i, x0, x1, y0, y1, lxFunc, lyFunc,
453455
var barColor = style.getBarColor(cd[i], trace);
454456
var insideTextFont = style.getInsideTextFont(trace, i, layoutFont, barColor);
455457
var outsideTextFont = style.getOutsideTextFont(trace, i, layoutFont);
458+
var insidetextanchor = trace.insidetextanchor || 'end';
456459

457460
// Special case: don't use the c2p(v, true) value on log size axes,
458461
// so that we can get correctly inside text scaling
@@ -475,9 +478,18 @@ function appendBarText(gd, plotinfo, bar, cd, i, x0, x1, y0, y1, lxFunc, lyFunc,
475478
}
476479
}
477480

481+
// Get width and height of bar at text position
482+
var lx, ly, refPos;
483+
if(isHorizontal) {
484+
refPos = (insidetextanchor === 'middle') ? Math.abs(x1 - x0) / 2 : TEXTPAD;
485+
lx = Math.abs(x1 - x0);
486+
ly = lyFunc ? lyFunc(refPos) : Math.abs(y1 - y0);
487+
} else {
488+
refPos = (insidetextanchor === 'middle') ? Math.abs(y1 - y0) / 2 : TEXTPAD;
489+
lx = lxFunc ? lxFunc(TEXTPAD) : Math.abs(x1 - x0);
490+
ly = Math.abs(y1 - y0);
491+
}
478492
// padding excluded
479-
var lx = lxFunc ? lxFunc(TEXTPAD) : Math.abs(x1 - x0);
480-
var ly = lyFunc ? lyFunc(TEXTPAD) : Math.abs(y1 - y0);
481493
var barWidth = lx - 2 * TEXTPAD;
482494
var barHeight = ly - 2 * TEXTPAD;
483495

@@ -568,7 +580,7 @@ function appendBarText(gd, plotinfo, bar, cd, i, x0, x1, y0, y1, lxFunc, lyFunc,
568580
isHorizontal: isHorizontal,
569581
constrained: constrained,
570582
angle: angle,
571-
anchor: trace.insidetextanchor
583+
anchor: insidetextanchor,
572584
});
573585
}
574586

@@ -604,7 +616,7 @@ function toMoveInsideBarWithCornerradius(x0, x1, y0, y1, textBB, lxFunc, lyFunc,
604616
var isHorizontal = !!opts.isHorizontal;
605617
var constrained = !!opts.constrained;
606618
var angle = opts.angle || 0;
607-
var anchor = opts.anchor || 'end';
619+
var anchor = opts.anchor;
608620
var isEnd = anchor === 'end';
609621
var isStart = anchor === 'start';
610622
var leftToRight = opts.leftToRight || 0; // left: -1, center: 0, right: 1
@@ -613,8 +625,17 @@ function toMoveInsideBarWithCornerradius(x0, x1, y0, y1, textBB, lxFunc, lyFunc,
613625

614626
var textWidth = textBB.width;
615627
var textHeight = textBB.height;
616-
var lx = lxFunc ? lxFunc(TEXTPAD) : Math.abs(x1 - x0);
617-
var ly = lyFunc ? lyFunc(TEXTPAD) : Math.abs(y1 - y0);
628+
629+
var lx, ly, refPos;
630+
if(isHorizontal) {
631+
refPos = (anchor === 'middle') ? Math.abs(x1 - x0) / 2 : TEXTPAD;
632+
lx = Math.abs(x1 - x0);
633+
ly = lyFunc ? lyFunc(refPos) : Math.abs(y1 - y0);
634+
} else {
635+
refPos = (anchor === 'middle') ? Math.abs(y1 - y0) / 2 : TEXTPAD;
636+
lx = lxFunc ? lxFunc(TEXTPAD) : Math.abs(x1 - x0);
637+
ly = Math.abs(y1 - y0);
638+
}
618639

619640
// compute remaining space
620641
var textpad = (

0 commit comments

Comments
 (0)