@@ -117,8 +117,11 @@ module.exports = function draw(gd) {
117117 }
118118 } )
119119 . each ( function ( d ) {
120- drawTexts ( gd , this , d [ 0 ] ) ;
121- setupTraceToggle ( gd , this , d [ 0 ] ) ;
120+ var legendItem = d [ 0 ] ;
121+
122+ d3 . select ( this )
123+ . call ( drawTexts , gd , legendItem )
124+ . call ( setupTraceToggle , gd , legendItem ) ;
122125 } ) ;
123126
124127 if ( gd . firstRender ) {
@@ -311,14 +314,14 @@ module.exports = function draw(gd) {
311314 }
312315} ;
313316
314- function drawTexts ( gd , context , legendItem ) {
317+ function drawTexts ( g , gd , legendItem ) {
315318 var fullLayout = gd . _fullLayout ,
316319 trace = legendItem . trace ,
317320 isPie = Plots . traceIs ( trace , 'pie' ) ,
318321 traceIndex = trace . index ,
319322 name = isPie ? legendItem . label : trace . name ;
320323
321- var text = d3 . select ( context ) . selectAll ( 'text.legendtext' )
324+ var text = g . selectAll ( 'text.legendtext' )
322325 . data ( [ 0 ] ) ;
323326 text . enter ( ) . append ( 'text' ) . classed ( 'legendtext' , true ) ;
324327 text . attr ( {
@@ -334,7 +337,7 @@ function drawTexts(gd, context, legendItem) {
334337 function textLayout ( s ) {
335338 Plotly . util . convertToTspans ( s , function ( ) {
336339 s . selectAll ( 'tspan.line' ) . attr ( { x : s . attr ( 'x' ) } ) ;
337- computeTextDimensions ( gd , context , legendItem ) ;
340+ g . call ( computeTextDimensions , gd , legendItem ) ;
338341 } ) ;
339342 }
340343
@@ -352,12 +355,12 @@ function drawTexts(gd, context, legendItem) {
352355 else text . call ( textLayout ) ;
353356}
354357
355- function setupTraceToggle ( gd , container , legendItem ) {
358+ function setupTraceToggle ( g , gd , legendItem ) {
356359 var hiddenSlices = gd . _fullLayout . hiddenlabels ?
357360 gd . _fullLayout . hiddenlabels . slice ( ) :
358361 [ ] ;
359362
360- var traceToggle = d3 . select ( container ) . selectAll ( 'rect' )
363+ var traceToggle = g . selectAll ( 'rect' )
361364 . data ( [ 0 ] ) ;
362365
363366 traceToggle . enter ( ) . append ( 'rect' )
@@ -402,11 +405,10 @@ function setupTraceToggle(gd, container, legendItem) {
402405 } ) ;
403406}
404407
405- function computeTextDimensions ( gd , container , legendItem ) {
406- var opts = gd . _fullLayout . legend ,
407- g = d3 . select ( container ) ,
408- bg = g . selectAll ( '.legendtoggle' ) ,
408+ function computeTextDimensions ( g , gd , legendItem ) {
409+ var bg = g . selectAll ( '.legendtoggle' ) ,
409410 mathjaxGroup = g . select ( 'g[class*=math-group]' ) ,
411+ opts = gd . _fullLayout . legend ,
410412 lineHeight = opts . font . size * 1.3 ,
411413 height ,
412414 width ;
0 commit comments