@@ -78,9 +78,9 @@ module.exports = function draw(gd) {
78
78
'shape-rendering' : 'crispEdges'
79
79
} ) ;
80
80
81
- bg . call ( Color . stroke , opts . bordercolor ) ;
82
- bg . call ( Color . fill , opts . bgcolor ) ;
83
- bg . style ( 'stroke-width' , opts . borderwidth + 'px' ) ;
81
+ bg . call ( Color . stroke , opts . bordercolor )
82
+ . call ( Color . fill , opts . bgcolor )
83
+ . style ( 'stroke-width' , opts . borderwidth + 'px' ) ;
84
84
85
85
var scrollBox = legend . selectAll ( 'g.scrollbox' )
86
86
. data ( [ 0 ] ) ;
@@ -94,10 +94,10 @@ module.exports = function draw(gd) {
94
94
scrollBar . enter ( ) . append ( 'rect' )
95
95
. attr ( {
96
96
'class' : 'scrollbar' ,
97
- 'rx' : 20 ,
98
- 'ry' : 2 ,
99
- ' width' : 0 ,
100
- ' height' : 0
97
+ rx : 20 ,
98
+ ry : 3 ,
99
+ width : 0 ,
100
+ height : 0
101
101
} )
102
102
. call ( Color . fill , '#808BA4' ) ;
103
103
@@ -207,12 +207,9 @@ module.exports = function draw(gd) {
207
207
// legend, background and border, scroll box and scroll bar
208
208
Drawing . setTranslate ( legend , lx , ly ) ;
209
209
210
- var scrollBarYMax = legendHeight -
211
- constants . scrollBarHeight -
212
- 2 * constants . scrollBarMargin ,
213
- scrollBoxYMax = opts . _height - legendHeight ,
214
- scrollBarY ,
215
- scrollBoxY ;
210
+ // to be safe, remove previous listeners
211
+ scrollBar . on ( '.drag' , null ) ;
212
+ legend . on ( 'wheel' , null ) ;
216
213
217
214
if ( opts . _height <= legendHeight || gd . _context . staticPlot ) {
218
215
// if scrollbar should not be shown.
@@ -232,11 +229,21 @@ module.exports = function draw(gd) {
232
229
y : opts . borderwidth
233
230
} ) ;
234
231
235
- scrollBox . call ( Drawing . setClipUrl , clipId ) ;
232
+ Drawing . setClipUrl ( scrollBox , clipId ) ;
233
+
234
+ Drawing . setRect ( scrollBar , 0 , 0 , 0 , 0 ) ;
235
+ delete opts . _scrollY ;
236
236
}
237
237
else {
238
- scrollBarY = constants . scrollBarMargin ,
239
- scrollBoxY = scrollBox . attr ( 'data-scroll' ) || 0 ;
238
+ var scrollBarHeight = Math . max ( constants . scrollBarMinHeight ,
239
+ legendHeight * legendHeight / opts . _height ) ;
240
+ var scrollBarYMax = legendHeight -
241
+ scrollBarHeight -
242
+ 2 * constants . scrollBarMargin ;
243
+ var scrollBoxYMax = opts . _height - legendHeight ;
244
+ var scrollRatio = scrollBarYMax / scrollBoxYMax ;
245
+
246
+ var scrollBoxY = Math . min ( opts . _scrollY || 0 , scrollBoxYMax ) ;
240
247
241
248
// increase the background and clip-path width
242
249
// by the scrollbar width and margin
@@ -257,60 +264,58 @@ module.exports = function draw(gd) {
257
264
constants . scrollBarMargin ,
258
265
height : legendHeight - 2 * opts . borderwidth ,
259
266
x : opts . borderwidth ,
260
- y : opts . borderwidth - scrollBoxY
267
+ y : opts . borderwidth + scrollBoxY
261
268
} ) ;
262
269
263
- scrollBox . call ( Drawing . setClipUrl , clipId ) ;
270
+ Drawing . setClipUrl ( scrollBox , clipId ) ;
264
271
265
- if ( firstRender ) scrollHandler ( scrollBarY , scrollBoxY ) ;
272
+ scrollHandler ( scrollBoxY , scrollBarHeight , scrollRatio ) ;
266
273
267
- legend . on ( 'wheel' , null ) ; // to be safe, remove previous listeners
268
274
legend . on ( 'wheel' , function ( ) {
269
275
scrollBoxY = Lib . constrain (
270
- scrollBox . attr ( 'data-scroll' ) -
276
+ opts . _scrollY +
271
277
d3 . event . deltaY / scrollBarYMax * scrollBoxYMax ,
272
- - scrollBoxYMax , 0 ) ;
273
- scrollBarY = constants . scrollBarMargin -
274
- scrollBoxY / scrollBoxYMax * scrollBarYMax ;
275
- scrollHandler ( scrollBarY , scrollBoxY ) ;
276
- if ( scrollBoxY !== 0 && scrollBoxY !== - scrollBoxYMax ) {
278
+ 0 , scrollBoxYMax ) ;
279
+ scrollHandler ( scrollBoxY , scrollBarHeight , scrollRatio ) ;
280
+ if ( scrollBoxY !== 0 && scrollBoxY !== scrollBoxYMax ) {
277
281
d3 . event . preventDefault ( ) ;
278
282
}
279
283
} ) ;
280
284
281
- // to be safe, remove previous listeners
282
- scrollBar . on ( '.drag' , null ) ;
283
- scrollBox . on ( '.drag' , null ) ;
285
+ var eventY0 , scrollBoxY0 ;
284
286
285
- var drag = d3 . behavior . drag ( ) . on ( 'drag' , function ( ) {
286
- scrollBarY = Lib . constrain (
287
- d3 . event . y - constants . scrollBarHeight / 2 ,
288
- constants . scrollBarMargin ,
289
- constants . scrollBarMargin + scrollBarYMax ) ;
290
- scrollBoxY = - ( scrollBarY - constants . scrollBarMargin ) /
291
- scrollBarYMax * scrollBoxYMax ;
292
- scrollHandler ( scrollBarY , scrollBoxY ) ;
287
+ var drag = d3 . behavior . drag ( )
288
+ . on ( 'dragstart' , function ( ) {
289
+ eventY0 = d3 . event . sourceEvent . clientY ;
290
+ scrollBoxY0 = scrollBoxY ;
291
+ } )
292
+ . on ( 'drag' , function ( ) {
293
+ var e = d3 . event . sourceEvent ;
294
+ if ( e . buttons === 2 || e . ctrlKey ) return ;
295
+
296
+ scrollBoxY = Lib . constrain (
297
+ ( e . clientY - eventY0 ) / scrollRatio + scrollBoxY0 ,
298
+ 0 , scrollBoxYMax ) ;
299
+ scrollHandler ( scrollBoxY , scrollBarHeight , scrollRatio ) ;
293
300
} ) ;
294
301
295
302
scrollBar . call ( drag ) ;
296
- scrollBox . call ( drag ) ;
297
303
}
298
304
299
305
300
- function scrollHandler ( scrollBarY , scrollBoxY ) {
301
- scrollBox
302
- . attr ( 'data-scroll' , scrollBoxY )
303
- . call ( Drawing . setTranslate , 0 , scrollBoxY ) ;
306
+ function scrollHandler ( scrollBoxY , scrollBarHeight , scrollRatio ) {
307
+ opts . _scrollY = gd . _fullLayout . legend . _scrollY = scrollBoxY ;
308
+ Drawing . setTranslate ( scrollBox , 0 , - scrollBoxY ) ;
304
309
305
- scrollBar . call (
306
- Drawing . setRect ,
310
+ Drawing . setRect (
311
+ scrollBar ,
307
312
legendWidth ,
308
- scrollBarY ,
313
+ constants . scrollBarMargin + scrollBoxY * scrollRatio ,
309
314
constants . scrollBarWidth ,
310
- constants . scrollBarHeight
315
+ scrollBarHeight
311
316
) ;
312
317
clipPath . select ( 'rect' ) . attr ( {
313
- y : opts . borderwidth - scrollBoxY
318
+ y : opts . borderwidth + scrollBoxY
314
319
} ) ;
315
320
}
316
321
@@ -434,7 +439,7 @@ function drawTexts(g, gd) {
434
439
return Plotly . restyle ( gd , update , traceIndex ) ;
435
440
} ) ;
436
441
} else {
437
- text . call ( textLayout ) ;
442
+ textLayout ( text ) ;
438
443
}
439
444
}
440
445
@@ -664,7 +669,7 @@ function computeLegendDimensions(gd, groups, traces) {
664
669
var legendItem = d [ 0 ] ,
665
670
bg = d3 . select ( this ) . select ( '.legendtoggle' ) ;
666
671
667
- bg . call ( Drawing . setRect ,
672
+ Drawing . setRect ( bg ,
668
673
0 ,
669
674
- legendItem . height / 2 ,
670
675
( gd . _context . edits . legendText ? 0 : opts . _width ) + extraWidth ,
0 commit comments