@@ -184,27 +184,51 @@ module.exports = function draw(gd) {
184
184
if ( anchorUtils . isRightAnchor ( opts ) ) {
185
185
lx -= opts . width ;
186
186
}
187
- if ( anchorUtils . isCenterAnchor ( opts ) ) {
187
+ else if ( anchorUtils . isCenterAnchor ( opts ) ) {
188
188
lx -= opts . width / 2 ;
189
189
}
190
190
191
191
if ( anchorUtils . isBottomAnchor ( opts ) ) {
192
192
ly -= opts . height ;
193
193
}
194
- if ( anchorUtils . isMiddleAnchor ( opts ) ) {
194
+ else if ( anchorUtils . isMiddleAnchor ( opts ) ) {
195
195
ly -= opts . height / 2 ;
196
196
}
197
197
198
+ lx = Math . round ( lx ) ;
199
+ ly = Math . round ( ly ) ;
200
+
201
+ // Make sure the legend top and bottom are visible
202
+ // (legends with a scroll bar are not allowed to stretch beyond the extended
203
+ // margins)
204
+ var lyMin = 0 ;
205
+ var lyMax = fullLayout . margin . t + fullLayout . height + fullLayout . margin . b ;
206
+ var legendHeight = opts . height ;
207
+ var legendHeightMax = gs . h ;
208
+
209
+
210
+ if ( legendHeight > legendHeightMax ) {
211
+ ly = gs . t ;
212
+ legendHeight = legendHeightMax ;
213
+ }
214
+ else {
215
+ if ( ly > lyMax ) ly = lyMax - legendHeight ;
216
+
217
+ if ( ly < lyMin ) ly = lyMin ;
218
+
219
+ legendHeight = Math . min ( lyMax - ly , opts . height ) ;
220
+ }
221
+
198
222
// Deal with scrolling
199
- var plotHeight = fullLayout . height - fullLayout . margin . b ,
200
- scrollheight = Math . min ( plotHeight - ly , opts . height ) ,
201
- scrollPosition = scrollBox . attr ( 'data-scroll' ) ? scrollBox . attr ( 'data-scroll' ) : 0 ;
223
+ var scrollPosition = scrollBox . attr ( 'data-scroll' ) ?
224
+ scrollBox . attr ( 'data-scroll' ) :
225
+ 0 ;
202
226
203
227
scrollBox . attr ( 'transform' , 'translate(0, ' + scrollPosition + ')' ) ;
204
228
205
229
bg . attr ( {
206
230
width : opts . width - 2 * opts . borderwidth ,
207
- height : scrollheight - 2 * opts . borderwidth ,
231
+ height : legendHeight - 2 * opts . borderwidth ,
208
232
x : opts . borderwidth ,
209
233
y : opts . borderwidth
210
234
} ) ;
@@ -213,15 +237,15 @@ module.exports = function draw(gd) {
213
237
214
238
clipPath . select ( 'rect' ) . attr ( {
215
239
width : opts . width ,
216
- height : scrollheight ,
240
+ height : legendHeight ,
217
241
x : 0 ,
218
242
y : 0
219
243
} ) ;
220
244
221
245
legend . call ( Drawing . setClipUrl , clipId ) ;
222
246
223
247
// If scrollbar should be shown.
224
- if ( opts . height - scrollheight > 0 && ! gd . _context . staticPlot ) {
248
+ if ( opts . height - legendHeight > 0 && ! gd . _context . staticPlot ) {
225
249
226
250
bg . attr ( {
227
251
width : opts . width - 2 * opts . borderwidth + constants . scrollBarWidth
@@ -243,21 +267,21 @@ module.exports = function draw(gd) {
243
267
scrollBox . attr ( 'data-scroll' , 0 ) ;
244
268
}
245
269
246
- scrollHandler ( 0 , scrollheight ) ;
270
+ scrollHandler ( 0 , legendHeight ) ;
247
271
248
272
legend . on ( 'wheel' , null ) ;
249
273
250
274
legend . on ( 'wheel' , function ( ) {
251
275
var e = d3 . event ;
252
276
e . preventDefault ( ) ;
253
- scrollHandler ( e . deltaY / 20 , scrollheight ) ;
277
+ scrollHandler ( e . deltaY / 20 , legendHeight ) ;
254
278
} ) ;
255
279
256
280
scrollBar . on ( '.drag' , null ) ;
257
281
scrollBox . on ( '.drag' , null ) ;
258
282
var drag = d3 . behavior . drag ( )
259
283
. on ( 'drag' , function ( ) {
260
- scrollHandler ( d3 . event . dy , scrollheight ) ;
284
+ scrollHandler ( d3 . event . dy , legendHeight ) ;
261
285
} ) ;
262
286
263
287
scrollBar . call ( drag ) ;
@@ -266,12 +290,12 @@ module.exports = function draw(gd) {
266
290
}
267
291
268
292
269
- function scrollHandler ( delta , scrollheight ) {
293
+ function scrollHandler ( delta , legendHeight ) {
270
294
271
- var scrollBarTrack = scrollheight - constants . scrollBarHeight - 2 * constants . scrollBarMargin ,
295
+ var scrollBarTrack = legendHeight - constants . scrollBarHeight - 2 * constants . scrollBarMargin ,
272
296
translateY = scrollBox . attr ( 'data-scroll' ) ,
273
- scrollBoxY = Lib . constrain ( translateY - delta , scrollheight - opts . height , 0 ) ,
274
- scrollBarY = - scrollBoxY / ( opts . height - scrollheight ) * scrollBarTrack + constants . scrollBarMargin ;
297
+ scrollBoxY = Lib . constrain ( translateY - delta , legendHeight - opts . height , 0 ) ,
298
+ scrollBarY = - scrollBoxY / ( opts . height - legendHeight ) * scrollBarTrack + constants . scrollBarMargin ;
275
299
276
300
scrollBox . attr ( 'data-scroll' , scrollBoxY ) ;
277
301
scrollBox . attr ( 'transform' , 'translate(0, ' + scrollBoxY + ')' ) ;
0 commit comments