@@ -350,7 +350,6 @@ function getRotationFromAngle(angle) {
350350}
351351
352352function getTransformToMoveInsideBar ( x0 , x1 , y0 , y1 , textBB , isHorizontal , constrained , angle , anchor ) {
353- // compute text and target positions
354353 var textWidth = textBB . width ;
355354 var textHeight = textBB . height ;
356355 var lx = Math . abs ( x1 - x0 ) ;
@@ -403,16 +402,14 @@ function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, isHorizontal, const
403402 if ( anchor !== 'middle' ) { // case of 'start' or 'end'
404403 var targetWidth = scale * ( isHorizontal !== isAutoRotated ? textHeight : textWidth ) ;
405404 var targetHeight = scale * ( isHorizontal !== isAutoRotated ? textWidth : textHeight ) ;
406- var move = targetWidth * absSin + targetHeight * absCos ;
407-
408- var offset = textpad + move / 2 ;
405+ textpad += 0.5 * ( targetWidth * absSin + targetHeight * absCos ) ;
409406
410407 if ( isHorizontal ) {
411- offset *= dirSign ( x0 , x1 ) ;
412- targetX = ( anchor === 'start' ) ? x0 + offset : x1 - offset ;
408+ textpad *= dirSign ( x0 , x1 ) ;
409+ targetX = ( anchor === 'start' ) ? x0 + textpad : x1 - textpad ;
413410 } else {
414- offset *= dirSign ( y0 , y1 ) ;
415- targetY = ( anchor === 'start' ) ? y0 + offset : y1 - offset ;
411+ textpad *= dirSign ( y0 , y1 ) ;
412+ targetY = ( anchor === 'start' ) ? y0 + textpad : y1 - textpad ;
416413 }
417414 }
418415
@@ -426,47 +423,49 @@ function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, isHorizontal, const
426423}
427424
428425function getTransformToMoveOutsideBar ( x0 , x1 , y0 , y1 , textBB , isHorizontal , constrained , angle ) {
429- var barWidth = ( isHorizontal ) ?
430- Math . abs ( y1 - y0 ) :
431- Math . abs ( x1 - x0 ) ;
432-
433- var rotation = getRotationFromAngle ( angle ) ;
426+ var textWidth = textBB . width ;
427+ var textHeight = textBB . height ;
428+ var lx = Math . abs ( x1 - x0 ) ;
429+ var ly = Math . abs ( y1 - y0 ) ;
434430
435- var textpad = 0 ;
431+ var textpad ;
436432 // Keep the padding so the text doesn't sit right against
437433 // the bars, but don't factor it into barWidth
438- if ( barWidth > 2 * TEXTPAD ) {
439- textpad = TEXTPAD ;
434+ if ( isHorizontal ) {
435+ textpad = ( ly > 2 * TEXTPAD ) ? TEXTPAD : 0 ;
436+ } else {
437+ textpad = ( lx > 2 * TEXTPAD ) ? TEXTPAD : 0 ;
440438 }
441- var absSin = Math . abs ( Math . sin ( Math . PI / 180 * rotation ) ) ;
442- var absCos = Math . abs ( Math . cos ( Math . PI / 180 * rotation ) ) ;
443439
444440 // compute rotation and scale
445441 var scale = 1 ;
446442 if ( constrained ) {
447443 scale = ( isHorizontal ) ?
448- Math . min ( 1 , barWidth / textBB . height ) :
449- Math . min ( 1 , barWidth / textBB . width ) ;
444+ Math . min ( 1 , ly / textHeight ) :
445+ Math . min ( 1 , lx / textWidth ) ;
450446 }
451447
452- // compute text and target positions
453- var textX = ( textBB . left + textBB . right ) / 2 ;
454- var textY = ( textBB . top + textBB . bottom ) / 2 ;
455-
456- var targetWidth = scale * textBB . width ;
457- var targetHeight = scale * textBB . height ;
448+ var rotation = getRotationFromAngle ( angle ) ;
449+ var absSin = Math . abs ( Math . sin ( Math . PI / 180 * rotation ) ) ;
450+ var absCos = Math . abs ( Math . cos ( Math . PI / 180 * rotation ) ) ;
458451
459- textpad += 0.5 * Math . min ( absSin * targetHeight , absCos * targetWidth ) ;
452+ // compute text and target positions
453+ var targetWidth = scale * ( isHorizontal ? textHeight : textWidth ) ;
454+ var targetHeight = scale * ( isHorizontal ? textWidth : textHeight ) ;
455+ textpad += 0.5 * ( targetWidth * absSin + targetHeight * absCos ) ;
460456
461457 var targetX = ( x0 + x1 ) / 2 ;
462458 var targetY = ( y0 + y1 ) / 2 ;
463459
464460 if ( isHorizontal ) {
465- targetX = x1 - ( textpad + targetWidth / 2 ) * dirSign ( x1 , x0 ) ;
461+ targetX = x1 - textpad * dirSign ( x1 , x0 ) ;
466462 } else {
467- targetY = y1 + ( textpad + targetHeight / 2 ) * dirSign ( y0 , y1 ) ;
463+ targetY = y1 + textpad * dirSign ( y0 , y1 ) ;
468464 }
469465
466+ var textX = ( textBB . left + textBB . right ) / 2 ;
467+ var textY = ( textBB . top + textBB . bottom ) / 2 ;
468+
470469 return getTransform ( textX , textY , targetX , targetY , scale , rotation ) ;
471470}
472471
0 commit comments