plotly.js icon indicating copy to clipboard operation
plotly.js copied to clipboard

Add ability to disable legend trace toggling

Open etpinard opened this issue 10 years ago • 16 comments

Some folks don't want it apparently; we should make this optional.

Maybe a boolean layout.legend.tracetoggle attribute?

etpinard avatar Jun 20 '16 14:06 etpinard

I have implemented it could you check it, please?

https://github.com/hectorip/plotly.js/pull/1

I have created a pull-request in my own fork as stated in the guidelines. Thanks in advance.

hectorip avatar Jul 19 '16 09:07 hectorip

@hectorip you're off to a great start. I think that's all you need.

A couple recommendations before making your PR to the main repo:

  • no need for that true in coerce('tracetoggle', true); In this case, the default value is static (i.e. it doesn't depend on other attributes).
  • make sure npm run lint is clear of errors. I think you'll only need to remove that console.log
  • Instead of adding an image baseline mock json, you should add a jasmine test that make sure that clicking on the legend items with tracetoggle: false doesn't do anything. See suite legend_test.js and legend_scroll_test.js for inspiration.

Thanks for your efforts :beers:

etpinard avatar Jul 19 '16 14:07 etpinard

@etpinard @hectorip What do you think about returning false from a callback to disable the default behavior?

I have a branch of plotly where I've added a plotly_legend_toggle event so I can know when to apply some custom scaling to the chart when a user toggles a trace.

It seems like there could be other uses for being able to influence the behavior of a toggle or react to it happening.

john-soklaski avatar Jul 19 '16 17:07 john-soklaski

What do you think about returning false from a callback to disable the default behavior?

We could, but the plotly.js data / layout API needs to be JSON serializable to ensure that all our API libraries have access to this feature.

@john-soklaski To treat the custom handler case, we would need a config option as proposed in https://github.com/plotly/plotly.js/issues/65 - which isn't at all incompatible with the legend.tracetoggle boolean attribute.

etpinard avatar Jul 19 '16 17:07 etpinard

@etpinard That makes sense, thanks. Glad to see it would still be a possibility :)

john-soklaski avatar Jul 19 '16 17:07 john-soklaski

I also had this need to disable trace toggling, but only for a particular trace, not for all. I wanted to have like a "background trace" that would be displayed in the legend but be untogglable. After digging around for hours in the plugin's js , I made an accidental discovery while inspecting the chart's svg dom elements: There is a special transparent element overlaying each trace in the legend (.legendtoggle). This element has the attached js click handler responsible for toggling the corresponding trace. By making this element unclickable, we circumvent the need for any new js hacks or changing the plugin api. Here is the workaround, using only a few lines of css: http://codepen.io/tudor-sv/pen/vXEZYG Of course this method could also be used to make all legend items untogglable...

tudor-sv avatar Sep 05 '16 14:09 tudor-sv

I have a related issue. I don't want to disable legend trace toggling in general, but I'd like to include a hyperlink in my trace name so that a page with more detail on that trace can load. I am okay with the trace toggling on click anywhere not within my <a> element, but I want click events within <a> links to pass through and work as expected.

dwinston avatar Sep 26 '17 19:09 dwinston

Any ETA on when this feature would be merged into plotly?

daattali avatar Jan 20 '18 06:01 daattali

Now doable using:

Plotly.newPlot('graph', {}).then(gd => {
  gd.on('plotly_legendclick', () => false}
})

I'll leave this ticket open, as something like https://github.com/hectorip/plotly.js/pull/1 could offer another viable of doing this.

etpinard avatar May 01 '18 17:05 etpinard

Hi @etpinard, thanks for fixing that.

I'm using reactjs wrapper for Plotly (import createPlotlyComponent from 'react-plotly.js/factory').

Unfortunately I cannot figure out what proper syntax should be in that case.

This phrase doesn't work:

const PlotlyReactComponent = createPlotlyComponent(Plotly). then(gd => { gd.on('plotly_legendclick', () => false); });

Will appreciate a lot your help!

AllaSorokina avatar May 21 '18 15:05 AllaSorokina

@AllaSorokina I'm no React expert, but something like

import React from 'react';
import createPlotlyComponent from 'react-plotly.js/factory';
const Plot = createPlotlyComponent(Plotly);

class App extends React.Component {
  render() {
    return (
      <Plot
        data={[
          {
            x: [1, 2, 3],
            y: [2, 6, 3],
            type: 'scatter',
            mode: 'lines+points',
            marker: {color: 'red'},
          },
          {type: 'bar', x: [1, 2, 3], y: [2, 5, 3]},
        ]}
        layout={{width: 320, height: 240, title: 'A Fancy Plot'}}
        onLegendDoubleClick={() => false}}
      />
    );
  }
}

should work.

If it doesn't, please ask your question again on https://community.plot.ly/

etpinard avatar May 23 '18 13:05 etpinard

This seems to be possible now with the plotly_legendclick & plotly_legenddoubleclick event hooks by returning false from the custom event handler. See https://stackoverflow.com/questions/51029619/what-is-the-right-way-to-override-default-event-handler-behaviors-e-g-toggle-t

jacobq avatar Jun 25 '18 18:06 jacobq

Seems like this issue can be closed now, per this comment, right?

jacobq avatar Jun 29 '18 19:06 jacobq

I chose not to in https://github.com/plotly/plotly.js/pull/2581

Add ability to disable legend trace toggling #665 (this PR adds a way, but it might be nice to add attributes for this so that the behavior can be exported)

etpinard avatar Jun 29 '18 20:06 etpinard

@etpinard OK, so this issue is really about providing a better way rather than simply making it possible. Fair enough

jacobq avatar Jun 29 '18 20:06 jacobq

What's the current status of this functionality?

RylanSchaeffer avatar Jan 14 '19 23:01 RylanSchaeffer