You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to be able to know when the user hovers or clicks on my label or legend items. Specifically, I would like to be able to highlight a given pie slice or bar trace when a particular label or legend item is hovered over. d3pie does this, and an example can be seen here: http://d3pie.org/#generator-start
As a starting point, it would be useful to have this click handler fire an event after it calls Plotly.restyle or Plotly.relayout:
It doesn't say which trace item was clicked. However, if you are truly dedicated, you can climb back up to the legend group tag, then back down to the text element and grab the data-unformatted value.
We would like to give a user the ability to select series colors after viewing a plot. Ideally they would interact with the legend to do so. Another vote to resolve this issue :)
Another use for this would be if you have a plot with a collection of line graphs corresponding to different groups. The user could then hover over the legend to highlight the particular plot similar to the d3pie example above.
It seems that there isn't a native event handler for the axis labels but we can workaround this limitation using vanilla Javascript and D3js. Here is what I did under Angular for a bar chart (probably the selector needs to be adapted for other chart types):
import*asd3from'd3';exportclassAngularCustomComponent{// contructor and other methods here..plotlyClickEvents(){/** * Distinguish between single and double click * @see http://bl.ocks.org/ropeladder/83915942ac42f17c087a82001418f2ee */functionclickcancel(){letdispatcher=d3.dispatch('click','dblclick');functioncc(selection){letdown,tolerance=5,last,wait=null,args;// euclidean distancefunctiondist(a,b){returnMath.sqrt(Math.pow(a[0]-b[0],2)+Math.pow(a[1]-b[1],2));}selection.on('mousedown',function(){down=d3.mouse(document.body);last=+newDate();args=arguments;});selection.on('mouseup',function(){if(dist(down,d3.mouse(document.body))>tolerance){return;}else{if(wait){window.clearTimeout(wait);wait=null;dispatcher.apply("dblclick",this,args);}else{wait=window.setTimeout((function(){returnfunction(){dispatcher.apply("click",this,args);wait=null;};})(),300);}}});};// Copies a variable number of methods from source to target.letd3rebind=function(target,source,method){leti=1,n=arguments.length;while(++i<n)target[method]=d3_rebind(target,source,source[method]);returntarget;};// Method is assumed to be a standard D3 getter-setter:// If passed with no arguments, gets the value.// If passed with arguments, sets the value and returns the target.functiond3_rebind(target,source,method){returnfunction(){letvalue=method.apply(source,arguments);returnvalue===source ? target : value;};}returnd3rebind(cc,dispatcher,'on');}returnclickcancel();}onBarChartXAxisClick(){letself=this;letitem=null;d3.select("#your-parent-element-identifier").select('.cartesianlayer').select('.xaxislayer-above').selectAll('.xtick > *').each(function(e){// @see https://hstefanski.wordpress.com/2015/10/25/responding-to-d3-events-in-typescript/// but `d3.select(d3.event.currentTarget)` does not work in this case.// To workaround see https://stackoverflow.com/a/40314241item=d3.select(this);// @NOTE: the element of the x-axis is a <text> and does not have the// attribute "pointer-events". Without this attribute is not possibile// to listen for mouse events, and for that we have to add it.item.attr('pointer-events','all');constcc=self.plotlyClickEvents();item.call(cc);cc.on('click',(d,index)=>{// Do something});});}}
For anybody looking to add hover events to the the Plotly legend, here's are some simple events I added to the traceToggle item that highlights the hovered legend item using opacity:
A community PR for this feature would certainly be welcome, but our experience is deeper features like this are difficult to complete without the Plotly maintainers leading the effort.
Sponsorship range: $25k-$30k
What Sponsorship includes:
Completion of this feature to the Sponsor's satisfaction, in a manner coherent with the rest of the Plotly.js library and API
Tests for this feature
Long-term support (continued support of this feature in the latest version of Plotly.js)
Hi - this issue has been sitting for a while, so as part of our effort to tidy up our public repositories I'm going to close it. If it's still a concern, we'd be grateful if you could open a new issue (with a short reproducible example if appropriate) so that we can add it to our stack. Cheers - @gvwilson
Activity
etpinard commentedon Dec 3, 2015
I really like this idea.
Custom legend item click handlers could be set in the plot config e.g.:
The current handler would simply be the default.
alexcjohnson commentedon Dec 4, 2015
or, as in some other cases like plotly_beforeplot, it will still execute the default handler unless you return
falsefrom the custom handlerAbildtoft commentedon Feb 27, 2016
Thanks for an awesome library. Can I ask what the status is with regards to this?
abalter commentedon Oct 9, 2016
Here is a hacky way to do it. I did "inspect element" on a legend item and found that it has a class
legendtoggle. Using this,It doesn't say which trace item was clicked. However, if you are truly dedicated, you can climb back up to the legend group tag, then back down to the
textelement and grab thedata-unformattedvalue.joeymink commentedon Jan 4, 2017
We would like to give a user the ability to select series colors after viewing a plot. Ideally they would interact with the legend to do so. Another vote to resolve this issue :)
mauritzvdworm commentedon Feb 6, 2017
Another vote here.
Another use for this would be if you have a plot with a collection of line graphs corresponding to different groups. The user could then hover over the legend to highlight the particular plot similar to the d3pie example above.
aliavni commentedon Mar 13, 2017
+1
cutalion commentedon May 16, 2017
+1
acailly commentedon May 16, 2017
+1
sardbaba commentedon Dec 11, 2017
It seems that there isn't a native event handler for the axis labels but we can workaround this limitation using vanilla Javascript and D3js. Here is what I did under Angular for a bar chart (probably the selector needs to be adapted for other chart types):
Hope this helps
bruggsy commentedon Dec 12, 2017
For anybody looking to add hover events to the the Plotly legend, here's are some simple events I added to the traceToggle item that highlights the hovered legend item using opacity:
`
`
etpinard commentedon May 1, 2018
The click part of this feature request has been merged in #2581
markzolotoy commentedon Oct 31, 2018
Lots of information here. What is a status on making legend items clickable?
[-]click/hover events for labels/legends[/-][+]hover events for labels/legends[/+]jackparmer commentedon Sep 10, 2020
This issue has been tagged with
NEEDS SPON$ORA community PR for this feature would certainly be welcome, but our experience is deeper features like this are difficult to complete without the Plotly maintainers leading the effort.
Sponsorship range: $25k-$30k
What Sponsorship includes:
Please include the link to this issue when contacting us to discuss.
gvwilson commentedon Jun 5, 2024
Hi - this issue has been sitting for a while, so as part of our effort to tidy up our public repositories I'm going to close it. If it's still a concern, we'd be grateful if you could open a new issue (with a short reproducible example if appropriate) so that we can add it to our stack. Cheers - @gvwilson