Skip to content

Commit efd6abd

Browse files
committed
bar: don't apply text padding if bar too small
* This change ensures inside texts can be drawn, even inside bars smaller than the text padding.
1 parent 13f0f6a commit efd6abd

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

src/traces/bar/plot.js

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ function appendBarText(gd, bar, calcTrace, i, x0, x1, y0, y1) {
215215

216216
barWidth = Math.abs(x1 - x0) - 2 * TEXTPAD, // padding excluded
217217
barHeight = Math.abs(y1 - y0) - 2 * TEXTPAD, // padding excluded
218-
barIsTooSmall = (barWidth <= 0 || barHeight <= 0),
219218

220219
textSelection,
221220
textBB,
@@ -226,10 +225,6 @@ function appendBarText(gd, bar, calcTrace, i, x0, x1, y0, y1) {
226225
if(!isOutmostBar) textPosition = 'inside';
227226
}
228227

229-
if(textPosition === 'inside') {
230-
if(barIsTooSmall) return;
231-
}
232-
233228
if(textPosition === 'auto') {
234229
if(isOutmostBar) {
235230
// draw text using insideTextFont and check if it fits inside bar
@@ -253,8 +248,7 @@ function appendBarText(gd, bar, calcTrace, i, x0, x1, y0, y1) {
253248
textSelection = null;
254249
}
255250
}
256-
else if(!barIsTooSmall) textPosition = 'inside';
257-
else return;
251+
else textPosition = 'inside';
258252
}
259253

260254
if(!textSelection) {
@@ -292,13 +286,22 @@ function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, orientation) {
292286
textHeight = textBB.height,
293287
textX = (textBB.left + textBB.right) / 2,
294288
textY = (textBB.top + textBB.bottom) / 2,
295-
barWidth = Math.abs(x1 - x0) - 2 * TEXTPAD,
296-
barHeight = Math.abs(y1 - y0) - 2 * TEXTPAD,
289+
barWidth = Math.abs(x1 - x0),
290+
barHeight = Math.abs(y1 - y0),
297291
targetWidth,
298292
targetHeight,
299293
targetX,
300294
targetY;
301295

296+
// apply text padding
297+
var textpad;
298+
if(barWidth > 2 * TEXTPAD && barHeight > 2 * TEXTPAD) {
299+
textpad = TEXTPAD;
300+
barWidth -= 2 * textpad;
301+
barHeight -= 2 * textpad;
302+
}
303+
else textpad = 0;
304+
302305
// compute rotation and scale
303306
var needsRotating,
304307
scale;
@@ -337,23 +340,23 @@ function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, orientation) {
337340
if(orientation === 'h') {
338341
if(x1 < x0) {
339342
// bar end is on the left hand side
340-
targetX = x1 + TEXTPAD + targetWidth / 2;
343+
targetX = x1 + textpad + targetWidth / 2;
341344
targetY = (y0 + y1) / 2;
342345
}
343346
else {
344-
targetX = x1 - TEXTPAD - targetWidth / 2;
347+
targetX = x1 - textpad - targetWidth / 2;
345348
targetY = (y0 + y1) / 2;
346349
}
347350
}
348351
else {
349352
if(y1 > y0) {
350353
// bar end is on the bottom
351354
targetX = (x0 + x1) / 2;
352-
targetY = y1 - TEXTPAD - targetHeight / 2;
355+
targetY = y1 - textpad - targetHeight / 2;
353356
}
354357
else {
355358
targetX = (x0 + x1) / 2;
356-
targetY = y1 + TEXTPAD + targetHeight / 2;
359+
targetY = y1 + textpad + targetHeight / 2;
357360
}
358361
}
359362

@@ -367,16 +370,23 @@ function getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB, orientation) {
367370
textWidth,
368371
textHeight;
369372
if(orientation === 'h') {
370-
barWidth = Math.abs(y1 - y0) - 2 * TEXTPAD;
373+
barWidth = Math.abs(y1 - y0);
371374
textWidth = textBB.height;
372375
textHeight = textBB.width;
373376
}
374377
else {
375-
barWidth = Math.abs(x1 - x0) - 2 * TEXTPAD;
378+
barWidth = Math.abs(x1 - x0);
376379
textWidth = textBB.width;
377380
textHeight = textBB.height;
378381
}
379382

383+
// apply text padding
384+
var textpad;
385+
if(barWidth > 2 * TEXTPAD) {
386+
textpad = TEXTPAD;
387+
barWidth -= 2 * textpad;
388+
}
389+
380390
// compute rotation and scale
381391
var needsRotating,
382392
scale;
@@ -422,23 +432,23 @@ function getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB, orientation) {
422432
if(orientation === 'h') {
423433
if(x1 < x0) {
424434
// bar end is on the left hand side
425-
targetX = x1 - TEXTPAD - targetWidth / 2;
435+
targetX = x1 - textpad - targetWidth / 2;
426436
targetY = (y0 + y1) / 2;
427437
}
428438
else {
429-
targetX = x1 + TEXTPAD + targetWidth / 2;
439+
targetX = x1 + textpad + targetWidth / 2;
430440
targetY = (y0 + y1) / 2;
431441
}
432442
}
433443
else {
434444
if(y1 > y0) {
435445
// bar end is on the bottom
436446
targetX = (x0 + x1) / 2;
437-
targetY = y1 + TEXTPAD + targetHeight / 2;
447+
targetY = y1 + textpad + targetHeight / 2;
438448
}
439449
else {
440450
targetX = (x0 + x1) / 2;
441-
targetY = y1 - TEXTPAD - targetHeight / 2;
451+
targetY = y1 - textpad - targetHeight / 2;
442452
}
443453
}
444454

0 commit comments

Comments
 (0)