@@ -33,6 +33,8 @@ module.exports = function draw(gd) {
33
33
34
34
if ( ! fullLayout . _infolayer || ! gd . calcdata ) return ;
35
35
36
+ if ( ! gd . _legendMouseDownTime ) gd . _legendMouseDownTime = 0 ;
37
+
36
38
var opts = fullLayout . legend ,
37
39
legendData = fullLayout . showlegend && getLegendData ( gd . calcdata , opts ) ,
38
40
hiddenSlices = fullLayout . hiddenlabels || [ ] ;
@@ -395,9 +397,9 @@ function drawTexts(g, gd) {
395
397
}
396
398
397
399
function setupTraceToggle ( g , gd ) {
398
- var hiddenSlices = gd . _fullLayout . hiddenlabels ?
399
- gd . _fullLayout . hiddenlabels . slice ( ) :
400
- [ ] ;
400
+ var newMouseDownTime ,
401
+ numClicks = 1 ,
402
+ DBLCLICKDELAY = constants . DBLCLICKDELAY ;
401
403
402
404
var traceToggle = g . selectAll ( 'rect' )
403
405
. data ( [ 0 ] ) ;
@@ -408,41 +410,98 @@ function setupTraceToggle(g, gd) {
408
410
. attr ( 'pointer-events' , 'all' )
409
411
. call ( Color . fill , 'rgba(0,0,0,0)' ) ;
410
412
411
- traceToggle . on ( 'click' , function ( ) {
413
+
414
+ traceToggle . on ( 'mousedown' , function ( ) {
415
+ newMouseDownTime = ( new Date ( ) ) . getTime ( ) ;
416
+ if ( newMouseDownTime - gd . _legendMouseDownTime < DBLCLICKDELAY ) {
417
+ // in a click train
418
+ numClicks += 1 ;
419
+ }
420
+ else {
421
+ // new click train
422
+ numClicks = 1 ;
423
+ gd . _legendMouseDownTime = newMouseDownTime ;
424
+ }
425
+ } ) ;
426
+ traceToggle . on ( 'mouseup' , function ( ) {
412
427
if ( gd . _dragged ) return ;
428
+ var legend = gd . _fullLayout . legend ;
413
429
414
- var legendItem = g . data ( ) [ 0 ] [ 0 ] ,
415
- fullData = gd . _fullData ,
416
- trace = legendItem . trace ,
417
- legendgroup = trace . legendgroup ,
418
- traceIndicesInGroup = [ ] ,
419
- tracei ,
420
- newVisible ;
430
+ if ( ( new Date ( ) ) . getTime ( ) - gd . _legendMouseDownTime > DBLCLICKDELAY ) {
431
+ numClicks = Math . max ( numClicks - 1 , 1 ) ;
432
+ }
421
433
422
- if ( Registry . traceIs ( trace , 'pie' ) ) {
423
- var thisLabel = legendItem . label ,
424
- thisLabelIndex = hiddenSlices . indexOf ( thisLabel ) ;
434
+ if ( numClicks === 1 ) {
435
+ legend . _clickTimeout = setTimeout ( function ( ) { handleClick ( g , gd , numClicks ) ; } , 300 ) ;
436
+ } else if ( numClicks === 2 ) {
437
+ if ( legend . _clickTimeout ) {
438
+ clearTimeout ( legend . _clickTimeout ) ;
439
+ }
440
+ handleClick ( g , gd , numClicks ) ;
441
+ }
442
+ } ) ;
443
+ }
444
+
445
+ function handleClick ( g , gd , numClicks ) {
446
+ var hiddenSlices = gd . _fullLayout . hiddenlabels ?
447
+ gd . _fullLayout . hiddenlabels . slice ( ) :
448
+ [ ] ;
425
449
450
+ var legendItem = g . data ( ) [ 0 ] [ 0 ] ,
451
+ fullData = gd . _fullData ,
452
+ trace = legendItem . trace ,
453
+ legendgroup = trace . legendgroup ,
454
+ traceIndicesInGroup = [ ] ,
455
+ tracei ,
456
+ newVisible ;
457
+
458
+ if ( Registry . traceIs ( trace , 'pie' ) ) {
459
+ var thisLabel = legendItem . label ,
460
+ thisLabelIndex = hiddenSlices . indexOf ( thisLabel ) ;
461
+
462
+ if ( numClicks === 1 ) {
426
463
if ( thisLabelIndex === - 1 ) hiddenSlices . push ( thisLabel ) ;
427
464
else hiddenSlices . splice ( thisLabelIndex , 1 ) ;
465
+ } else if ( numClicks === 2 ) {
466
+ hiddenSlices = [ ] ;
467
+ gd . calcdata [ 0 ] . forEach ( function ( d ) {
468
+ if ( thisLabel !== d . label ) {
469
+ hiddenSlices . push ( d . label ) ;
470
+ }
471
+ } ) ;
472
+ }
473
+
474
+ Plotly . relayout ( gd , 'hiddenlabels' , hiddenSlices ) ;
475
+ } else {
476
+ var otherTraces = [ ] ,
477
+ traceIndex ,
478
+ i ;
479
+
480
+ for ( i = 0 ; i < fullData . length ; i ++ ) {
481
+ otherTraces . push ( i ) ;
482
+ }
428
483
429
- Plotly . relayout ( gd , 'hiddenlabels' , hiddenSlices ) ;
484
+ if ( legendgroup === '' ) {
485
+ traceIndicesInGroup = [ trace . index ] ;
486
+ otherTraces . splice ( trace . index , 1 ) ;
430
487
} else {
431
- if ( legendgroup === '' ) {
432
- traceIndicesInGroup = [ trace . index ] ;
433
- } else {
434
- for ( var i = 0 ; i < fullData . length ; i ++ ) {
435
- tracei = fullData [ i ] ;
436
- if ( tracei . legendgroup === legendgroup ) {
437
- traceIndicesInGroup . push ( tracei . index ) ;
438
- }
488
+ for ( i = 0 ; i < fullData . length ; i ++ ) {
489
+ tracei = fullData [ i ] ;
490
+ if ( tracei . legendgroup === legendgroup ) {
491
+ traceIndicesInGroup . push ( tracei . index ) ;
492
+ traceIndex = otherTraces . indexOf ( i ) ;
493
+ otherTraces . splice ( traceIndex , 1 ) ;
439
494
}
440
495
}
441
-
496
+ }
497
+ if ( numClicks === 1 ) {
442
498
newVisible = trace . visible === true ? 'legendonly' : true ;
443
499
Plotly . restyle ( gd , 'visible' , newVisible , traceIndicesInGroup ) ;
500
+ } else if ( numClicks === 2 ) {
501
+ Plotly . restyle ( gd , 'visible' , true , traceIndicesInGroup ) ;
502
+ Plotly . restyle ( gd , 'visible' , 'legendonly' , otherTraces ) ;
444
503
}
445
- } ) ;
504
+ }
446
505
}
447
506
448
507
function computeTextDimensions ( g , gd ) {
0 commit comments