@@ -27,6 +27,7 @@ var anchorUtils = require('./anchor_utils');
2727
2828module . exports = function draw ( gd ) {
2929 var fullLayout = gd . _fullLayout ;
30+ var clipId = 'legend' + fullLayout . _uid ;
3031
3132 if ( ! fullLayout . _infolayer || ! gd . calcdata ) return ;
3233
@@ -36,6 +37,8 @@ module.exports = function draw(gd) {
3637
3738 if ( ! fullLayout . showlegend || ! legendData . length ) {
3839 fullLayout . _infolayer . selectAll ( '.legend' ) . remove ( ) ;
40+ fullLayout . _topdefs . select ( '#' + clipId ) . remove ( ) ;
41+
3942 Plots . autoMargin ( gd , 'legend' ) ;
4043 return ;
4144 }
@@ -52,6 +55,12 @@ module.exports = function draw(gd) {
5255 'pointer-events' : 'all'
5356 } ) ;
5457
58+ var clipPath = fullLayout . _topdefs . selectAll ( '#' + clipId )
59+ . data ( [ 0 ] )
60+ . enter ( ) . append ( 'clipPath' )
61+ . attr ( 'id' , clipId )
62+ . append ( 'rect' ) ;
63+
5564 var bg = legend . selectAll ( 'rect.bg' )
5665 . data ( [ 0 ] ) ;
5766
@@ -201,16 +210,14 @@ module.exports = function draw(gd) {
201210
202211 legend . attr ( 'transform' , 'translate(' + lx + ',' + ly + ')' ) ;
203212
204- var clipPath = selectClipPath ( gd ) ;
205-
206213 clipPath . attr ( {
207214 width : opts . width ,
208215 height : scrollheight ,
209216 x : 0 ,
210217 y : 0
211218 } ) ;
212219
213- legend . call ( Drawing . setClipUrl , constants . clipId ) ;
220+ legend . call ( Drawing . setClipUrl , clipId ) ;
214221
215222 // If scrollbar should be shown.
216223 if ( gd . firstRender && opts . height - scrollheight > 0 && ! gd . _context . staticPlot ) {
@@ -470,21 +477,3 @@ function repositionLegend(gd, traces) {
470477 t : opts . height * ( { bottom : 1 , middle : 0.5 } [ yanchor ] || 0 )
471478 } ) ;
472479}
473-
474- function selectClipPath ( gd ) {
475- var container = gd . _fullLayout . _infolayer . node ( ) . parentNode ;
476-
477- var defs = d3 . select ( container ) . selectAll ( 'defs' )
478- . data ( [ 0 ] ) ;
479-
480- defs . enter ( ) . append ( 'defs' ) ;
481-
482- var clipPath = defs . selectAll ( '#' + constants . clipId )
483- . data ( [ 0 ] ) ;
484-
485- var path = clipPath . enter ( ) . append ( 'clipPath' )
486- . attr ( 'id' , constants . clipId )
487- . append ( 'rect' ) ;
488-
489- return path ;
490- }
0 commit comments