Skip to content

Allow toggling legend to show just 1 series (or group) by double clicking #1432

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

Merged
merged 8 commits into from
Mar 13, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions src/components/dragelement/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var Plotly = require('../../plotly');
var Lib = require('../../lib');

var constants = require('../../plots/cartesian/constants');

var interactConstants = require('../../constants/interactions');

var dragElement = module.exports = {};

Expand Down Expand Up @@ -51,7 +51,7 @@ dragElement.unhoverRaw = unhover.raw;
dragElement.init = function init(options) {
var gd = Lib.getPlotDiv(options.element) || {},
numClicks = 1,
DBLCLICKDELAY = constants.DBLCLICKDELAY,
DBLCLICKDELAY = interactConstants.DBLCLICKDELAY,
startX,
startY,
newMouseDownTime,
Expand Down
3 changes: 1 addition & 2 deletions src/components/legend/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ module.exports = {
scrollBarWidth: 4,
scrollBarHeight: 20,
scrollBarColor: '#808BA4',
scrollBarMargin: 4,
DBLCLICKDELAY: 300
scrollBarMargin: 4
};
42 changes: 32 additions & 10 deletions src/components/legend/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ var Color = require('../color');
var svgTextUtils = require('../../lib/svg_text_utils');

var constants = require('./constants');
var interactConstants = require('../../constants/interactions');
var getLegendData = require('./get_legend_data');
var style = require('./style');
var helpers = require('./helpers');
var anchorUtils = require('./anchor_utils');

var SHOWISOLATETIP = true;

module.exports = function draw(gd) {
var fullLayout = gd._fullLayout;
Expand Down Expand Up @@ -399,7 +401,7 @@ function drawTexts(g, gd) {
function setupTraceToggle(g, gd) {
var newMouseDownTime,
numClicks = 1,
DBLCLICKDELAY = constants.DBLCLICKDELAY;
DBLCLICKDELAY = interactConstants.DBLCLICKDELAY;

var traceToggle = g.selectAll('rect')
.data([0]);
Expand Down Expand Up @@ -432,7 +434,7 @@ function setupTraceToggle(g, gd) {
}

if(numClicks === 1) {
legend._clickTimeout = setTimeout(function() { handleClick(g, gd, numClicks); }, 300);
legend._clickTimeout = setTimeout(function() { handleClick(g, gd, numClicks); }, DBLCLICKDELAY);
} else if(numClicks === 2) {
if(legend._clickTimeout) {
clearTimeout(legend._clickTimeout);
Expand All @@ -455,6 +457,13 @@ function handleClick(g, gd, numClicks) {
tracei,
newVisible;


if(numClicks === 1 && SHOWISOLATETIP && gd.data && gd._context.showTips) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need gd.data here - SHOWZOOMOUTTIP needed it because you can't autoscale a plot with nothing on it, but there wouldn't be a legend anyway without data!

Lib.notifier('Double click on legend to isolate individual trace', 'long');
SHOWISOLATETIP = false;
} else {
SHOWISOLATETIP = false;
}
if(Registry.traceIs(trace, 'pie')) {
var thisLabel = legendItem.label,
thisLabelIndex = hiddenSlices.indexOf(thisLabel);
Expand All @@ -469,37 +478,50 @@ function handleClick(g, gd, numClicks) {
hiddenSlices.push(d.label);
}
});
if(gd._fullLayout.hiddenlabels && gd._fullLayout.hiddenlabels.length === hiddenSlices.length && thisLabelIndex === -1) {
hiddenSlices = [];
}
}

Plotly.relayout(gd, 'hiddenlabels', hiddenSlices);
} else {
var otherTraces = [],
traceIndex,
var allTraces = [],
traceVisibility = [],
i;

for(i = 0; i < fullData.length; i++) {
otherTraces.push(i);
allTraces.push(i);
traceVisibility.push('legendonly');
}

if(legendgroup === '') {
traceIndicesInGroup = [trace.index];
otherTraces.splice(trace.index, 1);
traceVisibility[trace.index] = true;
} else {
for(i = 0; i < fullData.length; i++) {
tracei = fullData[i];
if(tracei.legendgroup === legendgroup) {
traceIndicesInGroup.push(tracei.index);
traceIndex = otherTraces.indexOf(i);
otherTraces.splice(traceIndex, 1);
traceVisibility[allTraces.indexOf(i)] = true;
}
}
}

if(numClicks === 1) {
newVisible = trace.visible === true ? 'legendonly' : true;
Plotly.restyle(gd, 'visible', newVisible, traceIndicesInGroup);
} else if(numClicks === 2) {
Plotly.restyle(gd, 'visible', true, traceIndicesInGroup);
Plotly.restyle(gd, 'visible', 'legendonly', otherTraces);
var sameAsLast = true;
for(i = 0; i < fullData.length; i++) {
if(fullData[i].visible !== traceVisibility[i]) {
sameAsLast = false;
break;
}
}
if(sameAsLast) {
traceVisibility = true;
}
Plotly.restyle(gd, 'visible', traceVisibility, allTraces);
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/constants/interactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@ module.exports = {
* Timing information for interactive elements
*/
SHOW_PLACEHOLDER: 100,
HIDE_PLACEHOLDER: 1000
HIDE_PLACEHOLDER: 1000,

// ms between first mousedown and 2nd mouseup to constitute dblclick...
// we don't seem to have access to the system setting
DBLCLICKDELAY: 300
};
4 changes: 0 additions & 4 deletions src/plots/cartesian/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ module.exports = {
AX_ID_PATTERN: /^[xyz][0-9]*$/,
AX_NAME_PATTERN: /^[xyz]axis[0-9]*$/,

// ms between first mousedown and 2nd mouseup to constitute dblclick...
// we don't seem to have access to the system setting
DBLCLICKDELAY: 300,

// pixels to move mouse before you stop clamping to starting point
MINDRAG: 8,

Expand Down