@@ -237,20 +237,22 @@ function appendBarText(gd, bar, calcTrace, i, x0, x1, y0, y1) {
237237 }
238238
239239 // compute text transform
240- var transform ;
240+ var transform , constrained ;
241241 if ( textPosition === 'outside' ) {
242+ constrained = trace . constraintext === 'both' || trace . constraintext === 'outside' ;
242243 transform = getTransformToMoveOutsideBar ( x0 , x1 , y0 , y1 , textBB ,
243- orientation ) ;
244+ orientation , constrained ) ;
244245 }
245246 else {
247+ constrained = trace . constraintext === 'both' || trace . constraintext === 'inside' ;
246248 transform = getTransformToMoveInsideBar ( x0 , x1 , y0 , y1 , textBB ,
247- orientation ) ;
249+ orientation , constrained ) ;
248250 }
249251
250252 textSelection . attr ( 'transform' , transform ) ;
251253}
252254
253- function getTransformToMoveInsideBar ( x0 , x1 , y0 , y1 , textBB , orientation ) {
255+ function getTransformToMoveInsideBar ( x0 , x1 , y0 , y1 , textBB , orientation , constrained ) {
254256 // compute text and target positions
255257 var textWidth = textBB . width ,
256258 textHeight = textBB . height ,
@@ -289,12 +291,12 @@ function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, orientation) {
289291 else if ( ( textWidth < textHeight ) === ( barWidth < barHeight ) ) {
290292 // only scale is required
291293 rotate = false ;
292- scale = Math . min ( barWidth / textWidth , barHeight / textHeight ) ;
294+ scale = constrained ? Math . min ( barWidth / textWidth , barHeight / textHeight ) : 1 ;
293295 }
294296 else {
295297 // both scale and rotation are required
296298 rotate = true ;
297- scale = Math . min ( barHeight / textWidth , barWidth / textHeight ) ;
299+ scale = constrained ? Math . min ( barHeight / textWidth , barWidth / textHeight ) : 1 ;
298300 }
299301
300302 if ( rotate ) rotate = 90 ; // rotate clockwise
@@ -335,23 +337,25 @@ function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, orientation) {
335337 return getTransform ( textX , textY , targetX , targetY , scale , rotate ) ;
336338}
337339
338- function getTransformToMoveOutsideBar ( x0 , x1 , y0 , y1 , textBB , orientation ) {
340+ function getTransformToMoveOutsideBar ( x0 , x1 , y0 , y1 , textBB , orientation , constrained ) {
339341 var barWidth = ( orientation === 'h' ) ?
340342 Math . abs ( y1 - y0 ) :
341343 Math . abs ( x1 - x0 ) ,
342344 textpad ;
343345
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
345348 if ( barWidth > 2 * TEXTPAD ) {
346349 textpad = TEXTPAD ;
347- barWidth -= 2 * textpad ;
348350 }
349351
350352 // compute rotation and scale
351- var rotate = false ,
353+ var scale = 1 ;
354+ if ( constrained ) {
352355 scale = ( orientation === 'h' ) ?
353356 Math . min ( 1 , barWidth / textBB . height ) :
354357 Math . min ( 1 , barWidth / textBB . width ) ;
358+ }
355359
356360 // compute text and target positions
357361 var textX = ( textBB . left + textBB . right ) / 2 ,
@@ -360,14 +364,9 @@ function getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB, orientation) {
360364 targetHeight ,
361365 targetX ,
362366 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 ;
371370
372371 if ( orientation === 'h' ) {
373372 if ( x1 < x0 ) {
@@ -392,7 +391,7 @@ function getTransformToMoveOutsideBar(x0, x1, y0, y1, textBB, orientation) {
392391 }
393392 }
394393
395- return getTransform ( textX , textY , targetX , targetY , scale , rotate ) ;
394+ return getTransform ( textX , textY , targetX , targetY , scale , false ) ;
396395}
397396
398397function getTransform ( textX , textY , targetX , targetY , scale , rotate ) {
0 commit comments