@@ -251,6 +251,9 @@ function plot(gd, plotinfo, cdModule, traceLayer, opts, makeOnCompleteCallback)
251
251
}
252
252
253
253
appendBarText ( gd , plotinfo , bar , cd , i , x0 , x1 , y0 , y1 , opts , makeOnCompleteCallback ) ;
254
+ if ( fullLayout . displaytotal ) {
255
+ appendBarTotal ( gd , plotinfo , bar , cd , i , x0 , x1 , y0 , y1 , opts , makeOnCompleteCallback ) ;
256
+ }
254
257
255
258
if ( plotinfo . layerClipId ) {
256
259
Drawing . hideOutsideRangePoint ( di , bar . select ( 'text' ) , xa , ya , trace . xcalendar , trace . ycalendar ) ;
@@ -275,7 +278,7 @@ function appendBarText(gd, plotinfo, bar, cd, i, x0, x1, y0, y1, opts, makeOnCom
275
278
var textPosition ;
276
279
277
280
function appendTextNode ( bar , text , font ) {
278
- var textSelection = Lib . ensureSingle ( bar , 'text' )
281
+ var textSelection = Lib . ensureSingle ( bar , 'text' , 'bartext-' + textPosition )
279
282
. text ( text )
280
283
. attr ( {
281
284
'class' : 'bartext bartext-' + textPosition ,
@@ -443,6 +446,48 @@ function appendBarText(gd, plotinfo, bar, cd, i, x0, x1, y0, y1, opts, makeOnCom
443
446
. attr ( 'transform' , Lib . getTextTransform ( transform ) ) ;
444
447
}
445
448
449
+ // total for stacked bars
450
+ function appendBarTotal ( gd , plotinfo , bar , cd , i , x0 , x1 , y0 , y1 , opts , makeOnCompleteCallback ) {
451
+ var fullLayout = gd . _fullLayout ;
452
+ // get trace attributes
453
+ var trace = cd [ 0 ] . trace ;
454
+ var isHorizontal = ( trace . orientation === 'h' ) ;
455
+ var inStackOrRelativeMode =
456
+ opts . mode === 'stack' ||
457
+ opts . mode === 'relative' ;
458
+ var calcBar = cd [ i ] ;
459
+ var isOutmostBar = ! inStackOrRelativeMode || calcBar . _outmost ;
460
+
461
+ if ( isOutmostBar ) {
462
+ var layoutFont = fullLayout . font ;
463
+ var font = style . getOutsideTextFont ( trace , i , layoutFont ) ;
464
+ var totalTemplate = fullLayout . totaltemplate ;
465
+ var obj = { total : isHorizontal ? calcBar . x : calcBar . y } ;
466
+ var totalText = Lib . texttemplateString ( totalTemplate , obj , fullLayout . _d2locale , { } , obj , trace . _meta || { } ) ;
467
+ var totalSelection = Lib . ensureSingle ( bar , 'text' , 'bartext-outside' )
468
+ . text ( totalText )
469
+ . attr ( {
470
+ 'class' : 'bartext bartext-outside' ,
471
+ 'text-anchor' : 'middle' ,
472
+ // prohibit tex interpretation until we can handle
473
+ // tex and regular text together
474
+ 'data-notex' : 1
475
+ } )
476
+ . call ( Drawing . font , font )
477
+ . call ( svgTextUtils . convertToTspans , gd ) ;
478
+
479
+ var textBB = Drawing . bBox ( totalSelection . node ( ) ) ;
480
+ var transform = toMoveOutsideBar ( x0 , x1 , y0 , y1 , textBB , {
481
+ isHorizontal : isHorizontal ,
482
+ constrained : false ,
483
+ angle : trace . textangle
484
+ } ) ;
485
+
486
+ transition ( totalSelection , fullLayout , opts , makeOnCompleteCallback )
487
+ . attr ( 'transform' , Lib . getTextTransform ( transform ) ) ;
488
+ }
489
+ }
490
+
446
491
function getRotateFromAngle ( angle ) {
447
492
return ( angle === 'auto' ) ? 0 : angle ;
448
493
}
0 commit comments