@@ -226,7 +226,7 @@ function appendBarText(gd, plotinfo, bar, calcTrace, i, x0, x1, y0, y1, opts) {
226
226
var trace = calcTrace [ 0 ] . trace ;
227
227
var isHorizontal = ( trace . orientation === 'h' ) ;
228
228
229
- var text = getText ( calcTrace , i , xa , ya ) ;
229
+ var text = getText ( fullLayout , calcTrace , i , xa , ya ) ;
230
230
textPosition = getTextPosition ( trace , i ) ;
231
231
232
232
// compute text position
@@ -516,14 +516,17 @@ function getTransform(textX, textY, targetX, targetY, scale, rotation) {
516
516
return transformTranslate + transformScale + transformRotate ;
517
517
}
518
518
519
- function getText ( calcTrace , index , xa , ya ) {
519
+ function getText ( fullLayout , calcTrace , index , xa , ya ) {
520
520
var trace = calcTrace [ 0 ] . trace ;
521
+ var texttemplate = trace . texttemplate ;
521
522
522
523
var value ;
523
- if ( ! trace . textinfo ) {
524
- value = helpers . getValue ( trace . text , index ) ;
525
- } else {
524
+ if ( texttemplate ) {
525
+ value = calcTexttemplate ( fullLayout , calcTrace , index , xa , ya ) ;
526
+ } else if ( trace . textinfo ) {
526
527
value = calcTextinfo ( calcTrace , index , xa , ya ) ;
528
+ } else {
529
+ value = helpers . getValue ( trace . text , index ) ;
527
530
}
528
531
529
532
return helpers . coerceString ( attributeText , value ) ;
@@ -534,6 +537,64 @@ function getTextPosition(trace, index) {
534
537
return helpers . coerceEnumerated ( attributeTextPosition , value ) ;
535
538
}
536
539
540
+ function calcTexttemplate ( fullLayout , calcTrace , index , xa , ya ) {
541
+ var trace = calcTrace [ 0 ] . trace ;
542
+ var texttemplate = Lib . castOption ( trace , index , 'texttemplate' ) ;
543
+ if ( ! texttemplate ) return '' ;
544
+ var isHorizontal = ( trace . orientation === 'h' ) ;
545
+ var isWaterfall = ( trace . type === 'waterfall' ) ;
546
+ var isFunnel = ( trace . type === 'funnel' ) ;
547
+
548
+ function formatLabel ( u ) {
549
+ var pAxis = isHorizontal ? ya : xa ;
550
+ return tickText ( pAxis , u , true ) . text ;
551
+ }
552
+
553
+ function formatNumber ( v ) {
554
+ var sAxis = isHorizontal ? xa : ya ;
555
+ return tickText ( sAxis , + v , true ) . text ;
556
+ }
557
+
558
+ var cdi = calcTrace [ index ] ;
559
+ var obj = { } ;
560
+
561
+ obj . label = cdi . p ;
562
+ obj . labelLabel = formatLabel ( cdi . p ) ;
563
+
564
+ var tx = Lib . castOption ( trace , cdi . i , 'text' ) ;
565
+ if ( tx === 0 || tx ) obj . text = tx ;
566
+
567
+ obj . value = cdi . s ;
568
+ obj . valueLabel = formatNumber ( cdi . s ) ;
569
+
570
+ obj . x = cdi . x ;
571
+ obj . y = cdi . y ;
572
+
573
+ if ( isWaterfall ) {
574
+ obj . delta = + cdi . rawS || cdi . s ;
575
+ obj . deltaLabel = formatNumber ( obj . delta ) ;
576
+ obj . final = cdi . v ;
577
+ obj . finalLabel = formatNumber ( obj . final ) ;
578
+ obj . initial = obj . final - obj . delta ;
579
+ obj . initialLabel = formatNumber ( obj . initial ) ;
580
+ }
581
+
582
+ if ( isFunnel ) {
583
+ obj . value = cdi . s ;
584
+ obj . valueLabel = formatNumber ( obj . value ) ;
585
+
586
+ obj . percentInitial = cdi . begR ;
587
+ obj . percentInitialLabel = Lib . formatPercent ( cdi . begR ) ;
588
+ obj . percentPrevious = cdi . difR ;
589
+ obj . percentPreviousLabel = Lib . formatPercent ( cdi . difR ) ;
590
+ obj . percentTotal = cdi . sumR ;
591
+ obj . percenTotalLabel = Lib . formatPercent ( cdi . sumR ) ;
592
+ }
593
+
594
+ return Lib . texttemplateString ( texttemplate , obj , fullLayout . _d3locale , obj , trace . _meta || { } ) ;
595
+ }
596
+
597
+ // TODO: calcTextinfo should build a texttemplate pass it to calcTexttemplate()
537
598
function calcTextinfo ( calcTrace , index , xa , ya ) {
538
599
var trace = calcTrace [ 0 ] . trace ;
539
600
var isHorizontal = ( trace . orientation === 'h' ) ;
0 commit comments