@@ -457,25 +457,16 @@ function toMoveInsideBar(x0, x1, y0, y1, textBB, opts) {
457
457
lx -= 2 * textpad ;
458
458
ly -= 2 * textpad ;
459
459
460
- var autoRotate = ( angle === 'auto' ) ;
461
- var isAutoRotated = false ;
462
- if ( autoRotate &&
460
+ var rotate = getRotateFromAngle ( angle ) ;
461
+ if ( ( angle === 'auto' ) &&
463
462
! ( textWidth <= lx && textHeight <= ly ) &&
464
463
( textWidth > lx || textHeight > ly ) && (
465
464
! ( textWidth > ly || textHeight > lx ) ||
466
465
( ( textWidth < textHeight ) !== ( lx < ly ) )
467
466
) ) {
468
- isAutoRotated = true ;
469
- }
470
-
471
- if ( isAutoRotated ) {
472
- // don't rotate yet only swap bar width with height
473
- var tmp = ly ;
474
- ly = lx ;
475
- lx = tmp ;
467
+ rotate += 90 ;
476
468
}
477
469
478
- var rotate = getRotateFromAngle ( angle ) ;
479
470
var t = getRotatedTextSize ( textBB , rotate ) ;
480
471
481
472
var scale = 1 ;
@@ -488,31 +479,42 @@ function toMoveInsideBar(x0, x1, y0, y1, textBB, opts) {
488
479
}
489
480
490
481
// compute text and target positions
482
+ var textX = ( textBB . left + textBB . right ) / 2 ;
483
+ var textY = ( textBB . top + textBB . bottom ) / 2 ;
491
484
var targetX = ( x0 + x1 ) / 2 ;
492
485
var targetY = ( y0 + y1 ) / 2 ;
486
+ var anchorX = 0 ;
487
+ var anchorY = 0 ;
493
488
if ( isStart || isEnd ) {
494
- textpad += 0.5 * scale * ( isHorizontal !== isAutoRotated ? t . x : t . y ) ;
489
+ var extrapad = ( isHorizontal ? t . x : t . y ) / 2 ;
490
+ var dir = isHorizontal ? dirSign ( x0 , x1 ) : dirSign ( y0 , y1 ) ;
495
491
496
492
if ( isHorizontal ) {
497
- textpad *= dirSign ( x0 , x1 ) ;
498
- targetX = isStart ? x0 + textpad : x1 - textpad ;
493
+ if ( isStart ) {
494
+ targetX = x0 + dir * textpad ;
495
+ anchorX = - dir * extrapad ;
496
+ } else {
497
+ targetX = x1 - dir * textpad ;
498
+ anchorX = dir * extrapad ;
499
+ }
499
500
} else {
500
- textpad *= dirSign ( y0 , y1 ) ;
501
- targetY = isStart ? y0 + textpad : y1 - textpad ;
501
+ if ( isStart ) {
502
+ targetY = y0 + dir * textpad ;
503
+ anchorY = - dir * extrapad ;
504
+ } else {
505
+ targetY = y1 - dir * textpad ;
506
+ anchorY = dir * extrapad ;
507
+ }
502
508
}
503
509
}
504
510
505
- var textX = ( textBB . left + textBB . right ) / 2 ;
506
- var textY = ( textBB . top + textBB . bottom ) / 2 ;
507
-
508
- // lastly apply auto rotation
509
- if ( isAutoRotated ) rotate += 90 ;
510
-
511
511
return {
512
512
textX : textX ,
513
513
textY : textY ,
514
514
targetX : targetX ,
515
515
targetY : targetY ,
516
+ anchorX : anchorX ,
517
+ anchorY : anchorY ,
516
518
scale : scale ,
517
519
rotate : rotate
518
520
} ;
@@ -549,24 +551,30 @@ function toMoveOutsideBar(x0, x1, y0, y1, textBB, opts) {
549
551
var t = getRotatedTextSize ( textBB , rotate ) ;
550
552
551
553
// compute text and target positions
552
- textpad += 0.5 * scale * ( isHorizontal ? t . x : t . y ) ;
554
+ var extrapad = ( isHorizontal ? t . x : t . y ) / 2 ;
555
+ var textX = ( textBB . left + textBB . right ) / 2 ;
556
+ var textY = ( textBB . top + textBB . bottom ) / 2 ;
553
557
var targetX = ( x0 + x1 ) / 2 ;
554
558
var targetY = ( y0 + y1 ) / 2 ;
559
+ var anchorX = 0 ;
560
+ var anchorY = 0 ;
555
561
562
+ var dir = isHorizontal ? dirSign ( x1 , x0 ) : dirSign ( y0 , y1 ) ;
556
563
if ( isHorizontal ) {
557
- targetX = x1 - textpad * dirSign ( x1 , x0 ) ;
564
+ targetX = x1 - dir * textpad ;
565
+ anchorX = dir * extrapad ;
558
566
} else {
559
- targetY = y1 + textpad * dirSign ( y0 , y1 ) ;
567
+ targetY = y1 + dir * textpad ;
568
+ anchorY = - dir * extrapad ;
560
569
}
561
570
562
- var textX = ( textBB . left + textBB . right ) / 2 ;
563
- var textY = ( textBB . top + textBB . bottom ) / 2 ;
564
-
565
571
return {
566
572
textX : textX ,
567
573
textY : textY ,
568
574
targetX : targetX ,
569
575
targetY : targetY ,
576
+ anchorX : anchorX ,
577
+ anchorY : anchorY ,
570
578
scale : scale ,
571
579
rotate : rotate
572
580
} ;
0 commit comments