|
10 | 10 | `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.
|
11 | 11 | 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.
|
12 | 12 | ---
|
13 |
| - |
14 | 13 | var myPlot = document.getElementById('myDiv'),
|
15 | 14 | x = [1, 2, 3, 4, 5, 6],
|
16 | 15 | y = [1, 2, 3, 2, 3, 4],
|
17 | 16 | y2 = [1, 4, 7, 6, 1, 5],
|
18 |
| - colors = [['#00000','#00000','#00000','#00000','#00000','#00000'], |
19 |
| - ['#00000','#00000','#00000','#00000','#00000','#00000']], |
| 17 | + colors = [['#5C636E','#5C636E','#5C636E','#5C636E','#5C636E','#5C636E'], |
| 18 | + ['#393e46','#393e46','#393e46','#393e46','#393e46','#393e46']], |
20 | 19 | data = [{x:x, y:y, type:'scatter',
|
21 |
| - mode:'line', line:{ color:'red'},marker:{size:16, color:colors[0]}}, |
| 20 | + mode:'line', line:{ color:'#5C636E'},marker:{size:16, color:colors[0]}}, |
22 | 21 | {x:x, y:y2, type:'scatter',
|
23 |
| - mode:'line',line:{ color:'black'}, marker:{size:16, color:colors[1]}}], |
| 22 | + mode:'line',line:{ color:'#393e46'}, marker:{size:16, color:colors[1]}}], |
24 | 23 | layout = {
|
25 | 24 | showlegend: true,
|
26 | 25 | hovermode:'closest',
|
|
44 | 43 | });
|
45 | 44 |
|
46 | 45 | myPlot.on('plotly_legendclick', function(data){
|
47 |
| - var trColors = ['#00000','#00000','#00000', |
48 |
| - '#00000','#00000','#00000']; |
49 |
| - var update = {'marker':{color: trColors, size:16}}; |
| 46 | + var trColors = [['#5C636E','#5C636E','#5C636E','#5C636E','#5C636E','#5C636E'], |
| 47 | + ['#393e46','#393e46','#393e46','#393e46','#393e46','#393e46']]; |
| 48 | + var update = {'marker':{color: trColors[data.curveNumber], size:16}}; |
50 | 49 | Plotly.restyle('myDiv', update,[data.curveNumber]);
|
51 | 50 | return false;
|
52 | 51 | });
|
0 commit comments