@@ -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,26 @@ 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 rotate = false ;
354+ var scale = 1 ;
355+ if ( constrained ) {
352356 scale = ( orientation === 'h' ) ?
353357 Math . min ( 1 , barWidth / textBB . height ) :
354358 Math . min ( 1 , barWidth / textBB . width ) ;
359+ }
355360
356361 // compute text and target positions
357362 var textX = ( textBB . left + textBB . right ) / 2 ,
0 commit comments