Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d23c24d

Browse filesBrowse files
committed
Move legend click handler to separate file
1 parent f8a78a2 commit d23c24d
Copy full SHA for d23c24d

File tree

2 files changed

+226
-211
lines changed
Filter options

2 files changed

+226
-211
lines changed

‎src/components/legend/draw.js

Copy file name to clipboardExpand all lines: src/components/legend/draw.js
+3-211Lines changed: 3 additions & 211 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

1211
var d3 = require('d3');
@@ -19,6 +18,7 @@ var dragElement = require('../dragelement');
1918
var Drawing = require('../drawing');
2019
var Color = require('../color');
2120
var svgTextUtils = require('../../lib/svg_text_utils');
21+
var handleClick = require('./handle_click');
2222

2323
var constants = require('./constants');
2424
var interactConstants = require('../../constants/interactions');
@@ -29,7 +29,6 @@ var style = require('./style');
2929
var helpers = require('./helpers');
3030
var anchorUtils = require('./anchor_utils');
3131

32-
var SHOWISOLATETIP = true;
3332
var DBLCLICKDELAY = interactConstants.DBLCLICKDELAY;
3433

3534
module.exports = function draw(gd) {
@@ -428,8 +427,9 @@ function drawTexts(g, gd) {
428427

429428
return Plotly.restyle(gd, update, traceIndex);
430429
});
430+
} else {
431+
text.call(textLayout);
431432
}
432-
else text.call(textLayout);
433433
}
434434

435435
function setupTraceToggle(g, gd) {
@@ -478,214 +478,6 @@ function setupTraceToggle(g, gd) {
478478
});
479479
}
480480

481-
function handleClick(g, gd, numClicks) {
482-
if(gd._dragged || gd._editing) return;
483-
484-
var hiddenSlices = gd._fullLayout.hiddenlabels ?
485-
gd._fullLayout.hiddenlabels.slice() :
486-
[];
487-
488-
var legendItem = g.data()[0][0];
489-
var fullData = gd._fullData;
490-
var fullTrace = legendItem.trace;
491-
var legendgroup = fullTrace.legendgroup;
492-
493-
var i, j, kcont, key, keys, val;
494-
var attrUpdate = {};
495-
var attrIndices = [];
496-
var carrs = [];
497-
var carrIdx = [];
498-
499-
function insertUpdate(traceIndex, key, value) {
500-
var attrIndex = attrIndices.indexOf(traceIndex);
501-
var valueArray = attrUpdate[key];
502-
if(!valueArray) {
503-
valueArray = attrUpdate[key] = [];
504-
}
505-
506-
if(attrIndices.indexOf(traceIndex) === -1) {
507-
attrIndices.push(traceIndex);
508-
attrIndex = attrIndices.length - 1;
509-
}
510-
511-
valueArray[attrIndex] = value;
512-
513-
return attrIndex;
514-
}
515-
516-
function setVisibility(fullTrace, visibility) {
517-
var fullInput = fullTrace._fullInput;
518-
if(Registry.hasTransform(fullInput, 'groupby')) {
519-
var kcont = carrs[fullInput.index];
520-
if(!kcont) {
521-
var groupbyIndices = Registry.getTransformIndices(fullInput, 'groupby');
522-
var lastGroupbyIndex = groupbyIndices[groupbyIndices.length - 1];
523-
kcont = Lib.keyedContainer(fullInput, 'transforms[' + lastGroupbyIndex + '].styles', 'target', 'value.visible');
524-
carrs[fullInput.index] = kcont;
525-
}
526-
527-
var curState = kcont.get(fullTrace._group);
528-
529-
// If not specified, assume visible. This happens if there are other style
530-
// properties set for a group but not the visibility. There are many similar
531-
// ways to do this (e.g. why not just `curState = fullTrace.visible`??? The
532-
// answer is: because it breaks other things like groupby trace names in
533-
// subtle ways.)
534-
if(curState === undefined) {
535-
curState = true;
536-
}
537-
538-
if(curState !== false) {
539-
// true -> legendonly. All others toggle to true:
540-
kcont.set(fullTrace._group, visibility);
541-
}
542-
carrIdx[fullInput.index] = insertUpdate(fullInput.index, 'visible', fullInput.visible === false ? false : true);
543-
} else {
544-
// false -> false (not possible since will not be visible in legend)
545-
// true -> legendonly
546-
// legendonly -> true
547-
var nextVisibility = fullInput.visible === false ? false : visibility;
548-
549-
insertUpdate(fullInput.index, 'visible', nextVisibility);
550-
}
551-
}
552-
553-
if(numClicks === 1 && SHOWISOLATETIP && gd.data && gd._context.showTips) {
554-
Lib.notifier('Double click on legend to isolate individual trace', 'long');
555-
SHOWISOLATETIP = false;
556-
} else {
557-
SHOWISOLATETIP = false;
558-
}
559-
560-
if(Registry.traceIs(fullTrace, 'pie')) {
561-
var thisLabel = legendItem.label,
562-
thisLabelIndex = hiddenSlices.indexOf(thisLabel);
563-
564-
if(numClicks === 1) {
565-
if(thisLabelIndex === -1) hiddenSlices.push(thisLabel);
566-
else hiddenSlices.splice(thisLabelIndex, 1);
567-
} else if(numClicks === 2) {
568-
hiddenSlices = [];
569-
gd.calcdata[0].forEach(function(d) {
570-
if(thisLabel !== d.label) {
571-
hiddenSlices.push(d.label);
572-
}
573-
});
574-
if(gd._fullLayout.hiddenlabels && gd._fullLayout.hiddenlabels.length === hiddenSlices.length && thisLabelIndex === -1) {
575-
hiddenSlices = [];
576-
}
577-
}
578-
579-
Plotly.relayout(gd, 'hiddenlabels', hiddenSlices);
580-
} else {
581-
var hasLegendgroup = legendgroup && legendgroup.length;
582-
var traceIndicesInGroup = [];
583-
var tracei;
584-
if(hasLegendgroup) {
585-
for(i = 0; i < fullData.length; i++) {
586-
tracei = fullData[i];
587-
if(!tracei.visible) continue;
588-
if(tracei.legendgroup === legendgroup) {
589-
traceIndicesInGroup.push(i);
590-
}
591-
}
592-
}
593-
594-
if(numClicks === 1) {
595-
var nextVisibility;
596-
597-
switch(fullTrace.visible) {
598-
case true:
599-
nextVisibility = 'legendonly';
600-
break;
601-
case false:
602-
nextVisibility = false;
603-
break;
604-
case 'legendonly':
605-
nextVisibility = true;
606-
break;
607-
}
608-
609-
if(hasLegendgroup) {
610-
for(i = 0; i < fullData.length; i++) {
611-
if(fullData[i].visible !== false && fullData[i].legendgroup === legendgroup) {
612-
setVisibility(fullData[i], nextVisibility);
613-
}
614-
}
615-
} else {
616-
setVisibility(fullTrace, nextVisibility);
617-
}
618-
} else if(numClicks === 2) {
619-
// Compute the clicked index. expandedIndex does what we want for expanded traces
620-
// but also culls hidden traces. That means we have some work to do.
621-
var isClicked, isInGroup, otherState;
622-
var isIsolated = true;
623-
for(i = 0; i < fullData.length; i++) {
624-
isClicked = fullData[i] === fullTrace;
625-
if(isClicked) continue;
626-
627-
isInGroup = (hasLegendgroup && fullData[i].legendgroup === legendgroup);
628-
629-
if(!isInGroup && fullData[i].visible === true && !Registry.traceIs(fullData[i], 'notLegendIsolatable')) {
630-
isIsolated = false;
631-
break;
632-
}
633-
}
634-
635-
for(i = 0; i < fullData.length; i++) {
636-
// False is sticky; we don't change it.
637-
if(fullData[i].visible === false) continue;
638-
639-
if(Registry.traceIs(fullData[i], 'notLegendIsolatable')) {
640-
continue;
641-
}
642-
643-
switch(fullTrace.visible) {
644-
case 'legendonly':
645-
setVisibility(fullData[i], true);
646-
break;
647-
case true:
648-
otherState = isIsolated ? true : 'legendonly';
649-
isClicked = fullData[i] === fullTrace;
650-
isInGroup = isClicked || (hasLegendgroup && fullData[i].legendgroup === legendgroup);
651-
setVisibility(fullData[i], isInGroup ? true : otherState);
652-
break;
653-
}
654-
}
655-
}
656-
657-
for(i = 0; i < carrs.length; i++) {
658-
kcont = carrs[i];
659-
if(!kcont) continue;
660-
var update = kcont.constructUpdate();
661-
662-
var updateKeys = Object.keys(update);
663-
for(j = 0; j < updateKeys.length; j++) {
664-
key = updateKeys[j];
665-
val = attrUpdate[key] = attrUpdate[key] || [];
666-
val[carrIdx[i]] = update[key];
667-
}
668-
}
669-
670-
// The length of the value arrays should be equal and any unspecified
671-
// values should be explicitly undefined for them to get properly culled
672-
// as updates and not accidentally reset to the default value. This fills
673-
// out sparse arrays with the required number of undefined values:
674-
keys = Object.keys(attrUpdate);
675-
for(i = 0; i < keys.length; i++) {
676-
key = keys[i];
677-
for(j = 0; j < attrIndices.length; j++) {
678-
// Use hasOwnPropety to protect against falsey values:
679-
if(!attrUpdate[key].hasOwnProperty(j)) {
680-
attrUpdate[key][j] = undefined;
681-
}
682-
}
683-
}
684-
685-
Plotly.restyle(gd, attrUpdate, attrIndices);
686-
}
687-
}
688-
689481
function computeTextDimensions(g, gd) {
690482
var legendItem = g.data()[0][0];
691483

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.