@@ -350,7 +350,6 @@ function getRotationFromAngle(angle) {
350
350
}
351
351
352
352
function getTransformToMoveInsideBar ( x0 , x1 , y0 , y1 , textBB , isHorizontal , constrained , angle , anchor ) {
353
- // compute text and target positions
354
353
var textWidth = textBB . width ;
355
354
var textHeight = textBB . height ;
356
355
var lx = Math . abs ( x1 - x0 ) ;
@@ -403,16 +402,14 @@ function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, isHorizontal, const
403
402
if ( anchor !== 'middle' ) { // case of 'start' or 'end'
404
403
var targetWidth = scale * ( isHorizontal !== isAutoRotated ? textHeight : textWidth ) ;
405
404
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 ) ;
409
406
410
407
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 ;
413
410
} 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 ;
416
413
}
417
414
}
418
415
@@ -426,47 +423,49 @@ function getTransformToMoveInsideBar(x0, x1, y0, y1, textBB, isHorizontal, const
426
423
}
427
424
428
425
function 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 ) ;
434
430
435
- var textpad = 0 ;
431
+ var textpad ;
436
432
// Keep the padding so the text doesn't sit right against
437
433
// 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 ;
440
438
}
441
- var absSin = Math . abs ( Math . sin ( Math . PI / 180 * rotation ) ) ;
442
- var absCos = Math . abs ( Math . cos ( Math . PI / 180 * rotation ) ) ;
443
439
444
440
// compute rotation and scale
445
441
var scale = 1 ;
446
442
if ( constrained ) {
447
443
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 ) ;
450
446
}
451
447
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 ) ) ;
458
451
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 ) ;
460
456
461
457
var targetX = ( x0 + x1 ) / 2 ;
462
458
var targetY = ( y0 + y1 ) / 2 ;
463
459
464
460
if ( isHorizontal ) {
465
- targetX = x1 - ( textpad + targetWidth / 2 ) * dirSign ( x1 , x0 ) ;
461
+ targetX = x1 - textpad * dirSign ( x1 , x0 ) ;
466
462
} else {
467
- targetY = y1 + ( textpad + targetHeight / 2 ) * dirSign ( y0 , y1 ) ;
463
+ targetY = y1 + textpad * dirSign ( y0 , y1 ) ;
468
464
}
469
465
466
+ var textX = ( textBB . left + textBB . right ) / 2 ;
467
+ var textY = ( textBB . top + textBB . bottom ) / 2 ;
468
+
470
469
return getTransform ( textX , textY , targetX , targetY , scale , rotation ) ;
471
470
}
472
471
0 commit comments