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
23 changes: 10 additions & 13 deletions src/components/legend/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var helpers = require('./helpers');
var anchorUtils = require('./anchor_utils');

var SHOWISOLATETIP = true;
var DBLCLICKDELAY = interactConstants.DBLCLICKDELAY;

module.exports = function draw(gd) {
var fullLayout = gd._fullLayout;
Expand Down Expand Up @@ -335,18 +336,15 @@ module.exports = function draw(gd) {
} else {
var traces = [],
clickedTrace;
fullLayout._infolayer.selectAll('g.traces').each(function() { d3.select(this).call(function() { traces.push(this); }); });
for(var i = 0; i < traces.length; i++) {
var tracei = traces[i];
var p = tracei[0][0].getBoundingClientRect();
if(e.clientX >= p.left && e.clientX <= p.right && e.clientY >= p.top && e.clientY <= p.bottom) {
clickedTrace = tracei;
break;
}
}
if(clickedTrace) {
traces = fullLayout._infolayer.selectAll('g.traces').filter(function() {
var bbox = this.getBoundingClientRect();
return (e.clientX >= bbox.left && e.clientX <= bbox.right &&
e.clientY >= bbox.top && e.clientY <= bbox.bottom);
})[0];
if(traces.length > 0) {
clickedTrace = d3.select(traces[0]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

non-blocking, but I find it confusing to dig into d3's nested arrays directly. Preferable to leave traces as a selection, then use traces.size() instead of traces[0].length, and then you don't need to re-select it to make clickedTrace.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixing.

if(numClicks === 1) {
legend._clickTimeout = setTimeout(function() { handleClick(clickedTrace, gd, numClicks); }, 300);
legend._clickTimeout = setTimeout(function() { handleClick(clickedTrace, gd, numClicks); }, DBLCLICKDELAY);
} else if(numClicks === 2) {
if(legend._clickTimeout) {
clearTimeout(legend._clickTimeout);
Expand Down Expand Up @@ -422,8 +420,7 @@ function drawTexts(g, gd) {

function setupTraceToggle(g, gd) {
var newMouseDownTime,
numClicks = 1,
DBLCLICKDELAY = interactConstants.DBLCLICKDELAY;
numClicks = 1;

var traceToggle = g.selectAll('rect')
.data([0]);
Expand Down