@@ -237,20 +237,22 @@ function appendBarText(gd, bar, calcTrace, i, x0, x1, y0, y1) {
237
237
}
238
238
239
239
// compute text transform
240
- var transform ;
240
+ var transform , constrained ;
241
241
if ( textPosition === 'outside' ) {
242
+ constrained = trace . constraintext === 'both' || trace . constraintext === 'outside' ;
242
243
transform = getTransformToMoveOutsideBar ( x0 , x1 , y0 , y1 , textBB ,
243
- orientation ) ;
244
+ orientation , constrained ) ;
244
245
}
245
246
else {
247
+ constrained = trace . constraintext === 'both' || trace . constraintext === 'inside' ;
246
248
transform = getTransformToMoveInsideBar ( x0 , x1 , y0 , y1 , textBB ,
247
- orientation ) ;
249
+ orientation , constrained ) ;
248
250
}
249
251
250
252
textSelection . attr ( 'transform' , transform ) ;
251
253
}
252
254
253
- function getTransformToMoveInsideBar ( x0 , x1 , y0 , y1 , textBB , orientation ) {
255
+ function getTransformToMoveInsideBar ( x0 , x1 , y0 , y1 , textBB , orientation , constrained ) {
254
256
// compute text and target positions
255
257
var textWidth = textBB . width ,
256
258
textHeight = textBB . height ,
@@ -289,12 +291,12 @@ function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, orientation) {
289
291
else if ( ( textWidth < textHeight ) === ( barWidth < barHeight ) ) {
290
292
// only scale is required
291
293
rotate = false ;
292
- scale = Math . min ( barWidth / textWidth , barHeight / textHeight ) ;
294
+ scale = constrained ? Math . min ( barWidth / textWidth , barHeight / textHeight ) : 1 ;
293
295
}
294
296
else {
295
297
// both scale and rotation are required
296
298
rotate = true ;
297
- scale = Math . min ( barHeight / textWidth , barWidth / textHeight ) ;
299
+ scale = constrained ? Math . min ( barHeight / textWidth , barWidth / textHeight ) : 1 ;
298
300
}
299
301
300
302
if ( rotate ) rotate = 90 ; // rotate clockwise
@@ -335,23 +337,25 @@ function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, orientation) {
335
337
return getTransform ( textX , textY , targetX , targetY , scale , rotate ) ;
336
338
}
337
339
338
- function getTransformToMoveOutsideBar ( x0 , x1 , y0 , y1 , textBB , orientation ) {
340
+ function getTransformToMoveOutsideBar ( x0 , x1 , y0 , y1 , textBB , orientation , constrained ) {
339
341
var barWidth = ( orientation === 'h' ) ?
340
342
Math . abs ( y1 - y0 ) :
341
343
Math . abs ( x1 - x0 ) ,
342
344
textpad ;
343
345
344
- // apply text padding if possible
346
+ // Keep the padding so the text doesn't sit right against
347
+ // the bars, but don't factor it into barWidth
345
348
if ( barWidth > 2 * TEXTPAD ) {
346
349
textpad = TEXTPAD ;
347
- barWidth -= 2 * textpad ;
348
350
}
349
351
350
352
// compute rotation and scale
351
- var rotate = false ,
353
+ var scale = 1 ;
354
+ if ( constrained ) {
352
355
scale = ( orientation === 'h' ) ?
353
356
Math . min ( 1 , barWidth / textBB . height ) :
354
357
Math . min ( 1 , barWidth / textBB . width ) ;
358
+ }
355
359
356
360
// compute text and target positions
357
361
var textX = ( textBB . left + textBB . right ) / 2 ,
@@ -360,14 +364,9 @@ function getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB, orientation) {
360
364
targetHeight ,
361
365
targetX ,
362
366
targetY ;
363
- if ( rotate ) {
364
- targetWidth = scale * textBB . height ;
365
- targetHeight = scale * textBB . width ;
366
- }
367
- else {
368
- targetWidth = scale * textBB . width ;
369
- targetHeight = scale * textBB . height ;
370
- }
367
+
368
+ targetWidth = scale * textBB . width ;
369
+ targetHeight = scale * textBB . height ;
371
370
372
371
if ( orientation === 'h' ) {
373
372
if ( x1 < x0 ) {
@@ -392,7 +391,7 @@ function getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB, orientation) {
392
391
}
393
392
}
394
393
395
- return getTransform ( textX , textY , targetX , targetY , scale , rotate ) ;
394
+ return getTransform ( textX , textY , targetX , targetY , scale , false ) ;
396
395
}
397
396
398
397
function getTransform ( textX , textY , targetX , targetY , scale , rotate ) {
0 commit comments