@@ -128,7 +128,9 @@ module.exports = function draw(gd) {
128
128
}
129
129
130
130
// Position and size the legend
131
- var lyMin = 0 ,
131
+ var lxMin = 0 ,
132
+ lxMax = fullLayout . width ,
133
+ lyMin = 0 ,
132
134
lyMax = fullLayout . height ;
133
135
134
136
computeLegendDimensions ( gd , groups , traces ) ;
@@ -162,6 +164,20 @@ module.exports = function draw(gd) {
162
164
ly -= opts . height / 2 ;
163
165
}
164
166
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
+
165
181
// Make sure the legend top and bottom are visible
166
182
// (legends with a scroll bar are not allowed to stretch beyond the extended
167
183
// margins)
@@ -173,7 +189,7 @@ module.exports = function draw(gd) {
173
189
legendHeight = legendHeightMax ;
174
190
}
175
191
else {
176
- if ( ly > lyMax ) ly = lyMax - legendHeight ;
192
+ if ( ly + legendHeight > lyMax ) ly = lyMax - legendHeight ;
177
193
if ( ly < lyMin ) ly = lyMin ;
178
194
legendHeight = Math . min ( lyMax - ly , opts . height ) ;
179
195
}
@@ -183,7 +199,7 @@ module.exports = function draw(gd) {
183
199
Lib . setTranslate ( legend , lx , ly ) ;
184
200
185
201
bg . attr ( {
186
- width : opts . width - opts . borderwidth ,
202
+ width : legendWidth - opts . borderwidth ,
187
203
height : legendHeight - opts . borderwidth ,
188
204
x : opts . borderwidth / 2 ,
189
205
y : opts . borderwidth / 2
@@ -194,7 +210,7 @@ module.exports = function draw(gd) {
194
210
Lib . setTranslate ( scrollBox , 0 , scrollPosition ) ;
195
211
196
212
clipPath . select ( 'rect' ) . attr ( {
197
- width : opts . width - 2 * opts . borderwidth ,
213
+ width : legendWidth - 2 * opts . borderwidth ,
198
214
height : legendHeight - 2 * opts . borderwidth ,
199
215
x : opts . borderwidth - scrollPosition ,
200
216
y : opts . borderwidth
@@ -208,14 +224,14 @@ module.exports = function draw(gd) {
208
224
// increase the background and clip-path width
209
225
// by the scrollbar width and margin
210
226
bg . attr ( {
211
- width : opts . width -
227
+ width : legendWidth -
212
228
2 * opts . borderwidth +
213
229
constants . scrollBarWidth +
214
230
constants . scrollBarMargin
215
231
} ) ;
216
232
217
233
clipPath . select ( 'rect' ) . attr ( {
218
- width : opts . width -
234
+ width : legendWidth -
219
235
2 * opts . borderwidth +
220
236
constants . scrollBarWidth +
221
237
constants . scrollBarMargin
@@ -272,7 +288,7 @@ module.exports = function draw(gd) {
272
288
273
289
scrollBar . call (
274
290
Drawing . setRect ,
275
- opts . width ,
291
+ legendWidth ,
276
292
scrollBarY ,
277
293
constants . scrollBarWidth ,
278
294
constants . scrollBarHeight
0 commit comments