Skip to content

Legend click event #1002

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 3 commits into from
Aug 12, 2018
Merged
Changes from 2 commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
name: Legend Click Events
plot_url: https://codepen.io/plotly/embed/vazxKv/?height=500&theme-id=15263&default-tab=result
language: plotly_js
suite: events
order: 4.1
sitemap: false
arrangement: horizontal
markdown_content: |
`plotly_legendclick` and `plotly_legenddoubleclick` allow customization of the plotly legend. The default behaviour of `plotly_legendclick` is to hide a trace and the default behavior of `plotly_legenddoubleclick` is to select one trace and hide all the others.
We can add to the default behaviour by creating a new `plotly_legendclick` event with a function of our choice. We can also disable the default behaviour by creating a function that returns `false`. In the example below, we do both in order to create a `plotly_legendclick` event which changes the marker color back to black instead of erasing the trace.
---

var myPlot = document.getElementById('myDiv'),
x = [1, 2, 3, 4, 5, 6],
y = [1, 2, 3, 2, 3, 4],
y2 = [1, 4, 7, 6, 1, 5],
colors = [['#00000','#00000','#00000','#00000','#00000','#00000'],
Copy link
Member

Choose a reason for hiding this comment

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

this is a cool example I just have a few styling suggestions 💅
I think the example would look a bit more clear if the 2 traces had different coloring, perhaps colors[0] is black and colors[1] is gray.

['#00000','#00000','#00000','#00000','#00000','#00000']],
data = [{x:x, y:y, type:'scatter',
mode:'line', line:{ color:'red'},marker:{size:16, color:colors[0]}},
Copy link
Member

Choose a reason for hiding this comment

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

the css default colors (like 'red') can be a little harsh, for the docs it's nice to use color pallets, I like https://colorhunt.co/

{x:x, y:y2, type:'scatter',
mode:'line',line:{ color:'black'}, marker:{size:16, color:colors[1]}}],
Copy link
Member

Choose a reason for hiding this comment

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

I think it makes sense in this example to make the base line color match the base marker color.

layout = {
showlegend: true,
hovermode:'closest',
title:'Click on a Point to Change Color<br>Click on a Trace in the Legend to Change Back One Trace Only'
};

Plotly.newPlot('myDiv', data, layout);

myPlot.on('plotly_click', function(data){
var pn='',
tn='',
colors=[];
for(var i=0; i < data.points.length; i++){
pn = data.points[i].pointNumber;
tn = data.points[i].curveNumber;
colors = data.points[i].data.marker.color;
};
colors[pn] = '#C54C82';
var update = {'marker':{color: colors, size:16}};
Plotly.restyle('myDiv', update,[tn]);
});

myPlot.on('plotly_legendclick', function(data){
var trColors = ['#00000','#00000','#00000',
'#00000','#00000','#00000'];
var update = {'marker':{color: trColors, size:16}};
Plotly.restyle('myDiv', update,[data.curveNumber]);
return false;
});