question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

hover events for labels/legends

See original GitHub issue

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: https://github.com/plotly/plotly.js/blob/47b13b0c2ecffae3ecb0b37d957978f3004a7ac7/src/components/legend/index.js#L484

A hover handler for the same traces would help a lot as well.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:6
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
bruggsycommented, 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:

`

traceToggle.on('mouseenter', function() {
var legendItem = g.data()[0][0],
    fullData = gd._fullData,
    trace = legendItem.trace,
    legendgroup = trace.legendgroup,
    tracei,
    allTraces = [],
    i,
    traceOpacity = [];

if(trace.visible === true){	    
        for(i = 0; i < fullData.length; i++) {
	allTraces.push(i);
	traceOpacity.push(
	    Registry.traceIs(fullData[i], 'notLegendIsolatable') ? 1.0: 0.1
	);
        }

    if(legendgroup === ''){
	traceOpacity[trace.index] = 1.0;
    } else{
	for(i = 0; i < fullData.length; i++) {
	    tracei = fullData[i];
	    if(tracei.legendgroup === legendgroup) {
		traceOpacity[allTraces.indexOf(i)] = 1.0;
	    }
	}
    }

    var update = {
	opacity: traceOpacity
    };
    Plotly.restyle(gd,update,allTraces);
}
});
traceToggle.on('mouseleave',function() {

var legendItem = g.data()[0][0],
    fullData = gd._fullData,
    trace = legendItem.trace,
    allTraces = [],
    i;
if(trace.visible === true){	    

    var update = {
	opacity: 1.0
    };
    for(i = 0; i < fullData.length; i++){
	allTraces.push(i);
    }

    Plotly.restyle(gd,update,allTraces);
}

});

`

1reaction
mauritzvdwormcommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hover style for labels in legend - highcharts
I've disabled clicking on legend labels by adding this property: plotOptions.series.events.legendItemClick = function() { return false; }.
Read more >
Show series bullets/labels on legend hover
This demo shows how we can add events to Legend items that would toggle on respective series' bullets and bullet labels on hover....
Read more >
Add an event for legend item hovering –
You might add mouseover/mouseout events to legend items. This event should provide the index of the legend item.
Read more >
show datalabels on legends hover
Hi anu_1555, In mouseOver and mouseOut events, you can loop over all available series and set their state programmatically. ... Let me know...
Read more >
itemmouseover - Sets Mouse Over Event
Sets the mouseover event handler for the legend, which is triggered when the user moves the mouse(input device) over a legend item.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found