Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Double-Click Feature that Isolates Legend-Items #871

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 53 additions & 7 deletions src/components/legend/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,16 +389,47 @@ function setupTraceToggle(g, gd) {
.attr('pointer-events', 'all')
.call(Color.fill, 'rgba(0,0,0,0)');

traceToggle.on('click', function() {
// This means it'll take a minimum of 200ms to take the single
// click action. If it's too short the single and double actions
// will be called.
// The default time between clicks on windows is 200ms (http://en.wikipedia.org/wiki/Double-click)
// Adjust accordingly.
var timeOut = 200;
var timeoutID = 0;
var ignoreSingleClicks = false;

traceToggle.on('click', function() {
if (!ignoreSingleClicks) {
// The double click generates two single click events
// and then a double click event so we always clear
// the last timeoutID
clearTimeout(timeoutID);

timeoutID = setTimeout(function() {
traceToggleByClickType(restyleBySingleClick);
}, timeOut);
}
});

traceToggle.on('dblclick', function() {
clearTimeout(timeoutID);
ignoreSingleClicks = true;

setTimeout(function() {
ignoreSingleClicks = false;
}, timeOut);
traceToggleByClickType(restyleByDoubleClick);
});

var traceToggleByClickType = function (restyleCondition) {
if(gd._dragged) return;

var legendItem = g.data()[0][0],
fullData = gd._fullData,
trace = legendItem.trace,
legendgroup = trace.legendgroup,
traceIndicesInGroup = [],
tracei,
newVisible;
tracei;

if(Plots.traceIs(trace, 'pie')) {
var thisLabel = legendItem.label,
Expand All @@ -419,11 +450,26 @@ function setupTraceToggle(g, gd) {
}
}
}

newVisible = trace.visible === true ? 'legendonly' : true;
Plotly.restyle(gd, 'visible', newVisible, traceIndicesInGroup);
restyleCondition(trace, traceIndicesInGroup);
}
});
};

var restyleBySingleClick = function (trace, traceIndicesInGroup) {
var newVisible = trace.visible === true ? 'legendonly' : true;
Plotly.restyle(gd, 'visible', newVisible, traceIndicesInGroup);
};

var restyleByDoubleClick = function (trace, traceIndicesInGroup) {
var calcdata = gd.calcdata;
var items = [];
Plotly.restyle(gd, 'visible', true, traceIndicesInGroup);

for(var i = 0; i < calcdata.length; i++){
if(i !== traceIndicesInGroup[0]){
items.push(i);
}
} Plotly.restyle(gd, 'visible', 'legendonly', items);
};
}

function computeTextDimensions(g, gd) {
Expand Down