@@ -128,7 +128,9 @@ module.exports = function draw(gd) {
128128 }
129129
130130 // Position and size the legend
131- var lyMin = 0 ,
131+ var lxMin = 0 ,
132+ lxMax = fullLayout . width ,
133+ lyMin = 0 ,
132134 lyMax = fullLayout . height ;
133135
134136 computeLegendDimensions ( gd , groups , traces ) ;
@@ -162,6 +164,20 @@ module.exports = function draw(gd) {
162164 ly -= opts . height / 2 ;
163165 }
164166
167+ // Make sure the legend left and right sides are visible
168+ var legendWidth = opts . width ,
169+ legendWidthMax = gs . w ;
170+
171+ if ( legendWidth > legendWidthMax ) {
172+ lx = gs . l ;
173+ legendWidth = legendWidthMax ;
174+ }
175+ else {
176+ if ( lx + legendWidth > lxMax ) lx = lxMax - legendWidth ;
177+ if ( lx < lxMin ) lx = lxMin ;
178+ legendWidth = Math . min ( lxMax - lx , opts . width ) ;
179+ }
180+
165181 // Make sure the legend top and bottom are visible
166182 // (legends with a scroll bar are not allowed to stretch beyond the extended
167183 // margins)
@@ -173,7 +189,7 @@ module.exports = function draw(gd) {
173189 legendHeight = legendHeightMax ;
174190 }
175191 else {
176- if ( ly > lyMax ) ly = lyMax - legendHeight ;
192+ if ( ly + legendHeight > lyMax ) ly = lyMax - legendHeight ;
177193 if ( ly < lyMin ) ly = lyMin ;
178194 legendHeight = Math . min ( lyMax - ly , opts . height ) ;
179195 }
@@ -183,7 +199,7 @@ module.exports = function draw(gd) {
183199 Lib . setTranslate ( legend , lx , ly ) ;
184200
185201 bg . attr ( {
186- width : opts . width - opts . borderwidth ,
202+ width : legendWidth - opts . borderwidth ,
187203 height : legendHeight - opts . borderwidth ,
188204 x : opts . borderwidth / 2 ,
189205 y : opts . borderwidth / 2
@@ -194,7 +210,7 @@ module.exports = function draw(gd) {
194210 Lib . setTranslate ( scrollBox , 0 , scrollPosition ) ;
195211
196212 clipPath . select ( 'rect' ) . attr ( {
197- width : opts . width - 2 * opts . borderwidth ,
213+ width : legendWidth - 2 * opts . borderwidth ,
198214 height : legendHeight - 2 * opts . borderwidth ,
199215 x : opts . borderwidth - scrollPosition ,
200216 y : opts . borderwidth
@@ -208,14 +224,14 @@ module.exports = function draw(gd) {
208224 // increase the background and clip-path width
209225 // by the scrollbar width and margin
210226 bg . attr ( {
211- width : opts . width -
227+ width : legendWidth -
212228 2 * opts . borderwidth +
213229 constants . scrollBarWidth +
214230 constants . scrollBarMargin
215231 } ) ;
216232
217233 clipPath . select ( 'rect' ) . attr ( {
218- width : opts . width -
234+ width : legendWidth -
219235 2 * opts . borderwidth +
220236 constants . scrollBarWidth +
221237 constants . scrollBarMargin
@@ -272,7 +288,7 @@ module.exports = function draw(gd) {
272288
273289 scrollBar . call (
274290 Drawing . setRect ,
275- opts . width ,
291+ legendWidth ,
276292 scrollBarY ,
277293 constants . scrollBarWidth ,
278294 constants . scrollBarHeight
0 commit comments