Skip to content

Commit 5c8e7b4

Browse files
committed
bar: update criteria for 'textposition: 'auto'`
* 'auto' will draw a label inside a bar if larger than drawn outside.
1 parent a1ef694 commit 5c8e7b4

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/traces/bar/plot.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ function appendBarText(gd, bar, calcTrace, i, x0, x1, y0, y1) {
165165
}
166166

167167
// get trace attributes
168-
var trace = calcTrace[0].trace;
168+
var trace = calcTrace[0].trace,
169+
orientation = trace.orientation;
169170

170171
var text = getText(trace, i);
171172
if(!text) return;
@@ -211,8 +212,12 @@ function appendBarText(gd, bar, calcTrace, i, x0, x1, y0, y1) {
211212
fitsInside =
212213
(textWidth <= barWidth && textHeight <= barHeight),
213214
fitsInsideIfRotated =
214-
(textWidth <= barHeight && textHeight <= barWidth);
215-
if(textHasSize && (fitsInside || fitsInsideIfRotated)) {
215+
(textWidth <= barHeight && textHeight <= barWidth),
216+
fitsInsideIfShrunk = (orientation === 'h') ?
217+
(barWidth >= textWidth * (barHeight / textHeight)) :
218+
(barHeight >= textHeight * (barWidth / textWidth));
219+
if(textHasSize &&
220+
(fitsInside || fitsInsideIfRotated || fitsInsideIfShrunk)) {
216221
textPosition = 'inside';
217222
}
218223
else {
@@ -243,11 +248,11 @@ function appendBarText(gd, bar, calcTrace, i, x0, x1, y0, y1) {
243248
var transform;
244249
if(textPosition === 'outside') {
245250
transform = getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB,
246-
trace.orientation);
251+
orientation);
247252
}
248253
else {
249254
transform = getTransformToMoveInsideBar(x0, x1, y0, y1, textBB,
250-
trace.orientation);
255+
orientation);
251256
}
252257

253258
textSelection.attr('transform', transform);
@@ -268,7 +273,7 @@ function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, orientation) {
268273

269274
// apply text padding
270275
var textpad;
271-
if(barWidth > 2 * TEXTPAD && barHeight > 2 * TEXTPAD) {
276+
if(barWidth > (2 * TEXTPAD) && barHeight > (2 * TEXTPAD)) {
272277
textpad = TEXTPAD;
273278
barWidth -= 2 * textpad;
274279
barHeight -= 2 * textpad;

0 commit comments

Comments
 (0)