@@ -19,6 +19,10 @@ var Drawing = require('../../components/drawing');
1919var Registry = require ( '../../registry' ) ;
2020var tickText = require ( '../../plots/cartesian/axes' ) . tickText ;
2121
22+ var uniformText = require ( './uniform_text' ) ;
23+ var recordMinTextSize = uniformText . recordMinTextSize ;
24+ var clearMinTextSize = uniformText . clearMinTextSize ;
25+
2226var style = require ( './style' ) ;
2327var helpers = require ( './helpers' ) ;
2428var constants = require ( './constants' ) ;
@@ -58,8 +62,8 @@ function getXY(di, xa, ya, isHorizontal) {
5862 return isHorizontal ? [ s , p ] : [ p , s ] ;
5963}
6064
61- function transition ( selection , opts , makeOnCompleteCallback ) {
62- if ( hasTransition ( opts ) ) {
65+ function transition ( selection , fullLayout , opts , makeOnCompleteCallback ) {
66+ if ( ! fullLayout . uniformtext . mode && hasTransition ( opts ) ) {
6367 var onComplete ;
6468 if ( makeOnCompleteCallback ) {
6569 onComplete = makeOnCompleteCallback ( ) ;
@@ -91,6 +95,9 @@ function plot(gd, plotinfo, cdModule, traceLayer, opts, makeOnCompleteCallback)
9195 gap : fullLayout . bargap ,
9296 groupgap : fullLayout . bargroupgap
9397 } ;
98+
99+ // don't clear bar when this is called from waterfall or funnel
100+ clearMinTextSize ( 'bar' , fullLayout ) ;
94101 }
95102
96103 var bartraces = Lib . makeTraceGroups ( traceLayer , cdModule , 'trace bars' ) . each ( function ( cd ) {
@@ -209,13 +216,13 @@ function plot(gd, plotinfo, cdModule, traceLayer, opts, makeOnCompleteCallback)
209216 y1 = fixpx ( y1 , y0 ) ;
210217 }
211218
212- var sel = transition ( Lib . ensureSingle ( bar , 'path' ) , opts , makeOnCompleteCallback ) ;
219+ var sel = transition ( Lib . ensureSingle ( bar , 'path' ) , fullLayout , opts , makeOnCompleteCallback ) ;
213220 sel
214221 . style ( 'vector-effect' , 'non-scaling-stroke' )
215222 . attr ( 'd' , 'M' + x0 + ',' + y0 + 'V' + y1 + 'H' + x1 + 'V' + y0 + 'Z' )
216223 . call ( Drawing . setClipUrl , plotinfo . layerClipId , gd ) ;
217224
218- if ( hasTransition ( opts ) ) {
225+ if ( ! fullLayout . uniformtext . mode && hasTransition ( opts ) ) {
219226 var styleFns = Drawing . makePointStyleFns ( trace ) ;
220227 Drawing . singlePointStyle ( di , sel , trace , styleFns , gd ) ;
221228 }
@@ -409,47 +416,39 @@ function appendBarText(gd, plotinfo, bar, cd, i, x0, x1, y0, y1, opts, makeOnCom
409416 recordMinTextSize ( trace . type , transform , fullLayout ) ;
410417 calcBar . transform = transform ;
411418
412- transition ( textSelection , opts , makeOnCompleteCallback )
419+ transition ( textSelection , fullLayout , opts , makeOnCompleteCallback )
413420 . attr ( 'transform' , Lib . getTextTransform ( transform ) ) ;
414421}
415422
416- function recordMinTextSize (
417- traceType , // in
418- transform , // inout
419- fullLayout // inout
420- ) {
421- if ( fullLayout . uniformtext . mode ) {
422- var minKey = '_' + traceType + 'Text_minsize' ;
423- var minSize = fullLayout . uniformtext . minsize ;
424- var size = transform . scale * transform . fontSize ;
425-
426- transform . hide = size < minSize ;
427-
428- fullLayout [ minKey ] = fullLayout [ minKey ] || Infinity ;
429- if ( ! transform . hide ) {
430- fullLayout [ minKey ] = Math . min (
431- fullLayout [ minKey ] ,
432- Math . max ( size , minSize )
433- ) ;
434- }
435- }
436- }
437-
438423function getRotateFromAngle ( angle ) {
439424 return ( angle === 'auto' ) ? 0 : angle ;
440425}
441426
427+ function getRotatedTextSize ( textBB , rotate ) {
428+ var a = Math . PI / 180 * rotate ;
429+ var absSin = Math . abs ( Math . sin ( a ) ) ;
430+ var absCos = Math . abs ( Math . cos ( a ) ) ;
431+
432+ return {
433+ x : textBB . width * absCos + textBB . height * absSin ,
434+ y : textBB . width * absSin + textBB . height * absCos
435+ } ;
436+ }
437+
442438function toMoveInsideBar ( x0 , x1 , y0 , y1 , textBB , opts ) {
443439 var isHorizontal = ! ! opts . isHorizontal ;
444440 var constrained = ! ! opts . constrained ;
445441 var angle = opts . angle || 0 ;
446- var anchor = opts . anchor || 0 ;
442+ var anchor = opts . anchor || 'end' ;
443+ var isEnd = anchor === 'end' ;
444+ var isStart = anchor === 'start' ;
447445
448446 var textWidth = textBB . width ;
449447 var textHeight = textBB . height ;
450448 var lx = Math . abs ( x1 - x0 ) ;
451449 var ly = Math . abs ( y1 - y0 ) ;
452450
451+ // compute remaining space
453452 var textpad = (
454453 lx > ( 2 * TEXTPAD ) &&
455454 ly > ( 2 * TEXTPAD )
@@ -458,67 +457,64 @@ function toMoveInsideBar(x0, x1, y0, y1, textBB, opts) {
458457 lx -= 2 * textpad ;
459458 ly -= 2 * textpad ;
460459
461- var autoRotate = ( angle === 'auto' ) ;
462- var isAutoRotated = false ;
463- if ( autoRotate &&
460+ var rotate = getRotateFromAngle ( angle ) ;
461+ if ( ( angle === 'auto' ) &&
464462 ! ( textWidth <= lx && textHeight <= ly ) &&
465463 ( textWidth > lx || textHeight > ly ) && (
466464 ! ( textWidth > ly || textHeight > lx ) ||
467465 ( ( textWidth < textHeight ) !== ( lx < ly ) )
468466 ) ) {
469- isAutoRotated = true ;
467+ rotate += 90 ;
470468 }
471469
472- if ( isAutoRotated ) {
473- // don't rotate yet only swap bar width with height
474- var tmp = ly ;
475- ly = lx ;
476- lx = tmp ;
477- }
470+ var t = getRotatedTextSize ( textBB , rotate ) ;
478471
479- var rotate = getRotateFromAngle ( angle ) ;
480- var absSin = Math . abs ( Math . sin ( Math . PI / 180 * rotate ) ) ;
481- var absCos = Math . abs ( Math . cos ( Math . PI / 180 * rotate ) ) ;
482-
483- // compute and apply text padding
484- var dx = Math . max ( lx * absCos , ly * absSin ) ;
485- var dy = Math . max ( lx * absSin , ly * absCos ) ;
486-
487- var scale = ( constrained ) ?
488- Math . min ( dx / textWidth , dy / textHeight ) :
489- Math . max ( absCos , absSin ) ;
490-
491- scale = Math . min ( 1 , scale ) ;
472+ var scale = 1 ;
473+ if ( constrained ) {
474+ scale = Math . min (
475+ 1 ,
476+ lx / t . x ,
477+ ly / t . y
478+ ) ;
479+ }
492480
493481 // compute text and target positions
482+ var textX = ( textBB . left + textBB . right ) / 2 ;
483+ var textY = ( textBB . top + textBB . bottom ) / 2 ;
494484 var targetX = ( x0 + x1 ) / 2 ;
495485 var targetY = ( y0 + y1 ) / 2 ;
496-
497- if ( anchor !== 'middle' ) { // case of 'start' or 'end'
498- var targetWidth = scale * ( isHorizontal !== isAutoRotated ? textHeight : textWidth ) ;
499- var targetHeight = scale * ( isHorizontal !== isAutoRotated ? textWidth : textHeight ) ;
500- textpad += 0.5 * ( targetWidth * absSin + targetHeight * absCos ) ;
486+ var anchorX = 0 ;
487+ var anchorY = 0 ;
488+ if ( isStart || isEnd ) {
489+ var extrapad = ( isHorizontal ? t . x : t . y ) / 2 ;
490+ var dir = isHorizontal ? dirSign ( x0 , x1 ) : dirSign ( y0 , y1 ) ;
501491
502492 if ( isHorizontal ) {
503- textpad *= dirSign ( x0 , x1 ) ;
504- targetX = ( anchor === 'start' ) ? 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+ }
505500 } else {
506- textpad *= dirSign ( y0 , y1 ) ;
507- targetY = ( anchor === 'start' ) ? 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+ }
508508 }
509509 }
510510
511- var textX = ( textBB . left + textBB . right ) / 2 ;
512- var textY = ( textBB . top + textBB . bottom ) / 2 ;
513-
514- // lastly apply auto rotation
515- if ( isAutoRotated ) rotate += 90 ;
516-
517511 return {
518512 textX : textX ,
519513 textY : textY ,
520514 targetX : targetX ,
521515 targetY : targetY ,
516+ anchorX : anchorX ,
517+ anchorY : anchorY ,
522518 scale : scale ,
523519 rotate : rotate
524520 } ;
@@ -552,31 +548,33 @@ function toMoveOutsideBar(x0, x1, y0, y1, textBB, opts) {
552548 }
553549
554550 var rotate = getRotateFromAngle ( angle ) ;
555- var absSin = Math . abs ( Math . sin ( Math . PI / 180 * rotate ) ) ;
556- var absCos = Math . abs ( Math . cos ( Math . PI / 180 * rotate ) ) ;
551+ var t = getRotatedTextSize ( textBB , rotate ) ;
557552
558553 // compute text and target positions
559- var targetWidth = scale * ( isHorizontal ? textHeight : textWidth ) ;
560- var targetHeight = scale * ( isHorizontal ? textWidth : textHeight ) ;
561- textpad += 0.5 * ( targetWidth * absSin + targetHeight * absCos ) ;
562-
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 ;
563557 var targetX = ( x0 + x1 ) / 2 ;
564558 var targetY = ( y0 + y1 ) / 2 ;
559+ var anchorX = 0 ;
560+ var anchorY = 0 ;
565561
562+ var dir = isHorizontal ? dirSign ( x1 , x0 ) : dirSign ( y0 , y1 ) ;
566563 if ( isHorizontal ) {
567- targetX = x1 - textpad * dirSign ( x1 , x0 ) ;
564+ targetX = x1 - dir * textpad ;
565+ anchorX = dir * extrapad ;
568566 } else {
569- targetY = y1 + textpad * dirSign ( y0 , y1 ) ;
567+ targetY = y1 + dir * textpad ;
568+ anchorY = - dir * extrapad ;
570569 }
571570
572- var textX = ( textBB . left + textBB . right ) / 2 ;
573- var textY = ( textBB . top + textBB . bottom ) / 2 ;
574-
575571 return {
576572 textX : textX ,
577573 textY : textY ,
578574 targetX : targetX ,
579575 targetY : targetY ,
576+ anchorX : anchorX ,
577+ anchorY : anchorY ,
580578 scale : scale ,
581579 rotate : rotate
582580 } ;
@@ -749,6 +747,5 @@ function calcTextinfo(cd, index, xa, ya) {
749747
750748module . exports = {
751749 plot : plot ,
752- toMoveInsideBar : toMoveInsideBar ,
753- recordMinTextSize : recordMinTextSize
750+ toMoveInsideBar : toMoveInsideBar
754751} ;
0 commit comments