# \`legend\_groupclick = "toggleitem"\` doesn't seem to work in PlotlyJS

**URL:** https://discourse.julialang.org/t/legend-groupclick-toggleitem-doesnt-seem-to-work-in-plotlyjs/76851
**Category:** Visualization
**Tags:** plotlyjs
**Created:** [February 21, 2022, 3:37pm UTC](https://discourse.julialang.org/t/legend-groupclick-toggleitem-doesnt-seem-to-work-in-plotlyjs/76851 "2022-02-21T15:37:30Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Mo-Gul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mo-gul/32/33308_2.png) [@Mo-Gul](https://discourse.julialang.org/u/Mo-Gul)
#### Post date: [February 21, 2022, 3:37pm UTC](https://discourse.julialang.org/t/legend-groupclick-toggleitem-doesnt-seem-to-work-in-plotlyjs/76851/1 "2022-02-21T15:37:31Z")

</div>

Currently `legend_groupclick="toggleitem"` doesn’t seem to work as can be tested in the result of

```julia
using PlotlyJS

function getData()
    x = range(start=0, stop=10, length=101)
    y1 = sin.(x)
    y2 = sin.(x) .- 0.1
    y3 = cos.(x)
    y4 = cos.(x) .- 0.1

    trace1 = scatter(x=x, y=y1, legendgroup="sin", legendgrouptitle_text="sin")
    trace2 = scatter(x=x, y=y2, legendgroup="sin", legendgrouptitle_text="sin")
    trace3 = scatter(x=x, y=y3, legendgroup="cos", legendgrouptitle_text="cos")
    trace4 = scatter(x=x, y=y4, legendgroup="cos", legendgrouptitle_text="cos")

    return [trace1, trace2, trace3, trace4]
end
data = getData()

layout = Layout(legend_groupclick="toggleitem")

fig = Plot(data, layout)

@assert fig.layout.fields[:legend][:groupclick] == "toggleitem"

open("plotly_julia.html", "w") do io
    PlotlyBase.to_html(io, fig)
end

```

Am I doing something wrong here? If not, is this a Julia issue and I can file an issue at that repository or is this because the same bug exists in the JavaScript library as can be seen in the filed issue?

[https://github.com/plotly/plotly.js/issues/5910](https://github.com/plotly/plotly.js/issues/5910)

* * *

_In Python there also was this bug, but it was fixed as can be seen at_

[https://github.com/plotly/plotly.py/issues/3488](https://github.com/plotly/plotly.py/issues/3488)

_And here the same code as above in python to prove that it works_

```python
import numpy as np
import plotly.graph_objects as go

x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

fig = go.Figure()
fig.add_trace(go.Scatter(x=x, y=y1 - 0.0, legendgroup='sin', legendgrouptitle_text='sin'))
fig.add_trace(go.Scatter(x=x, y=y1 - 0.1, legendgroup='sin', legendgrouptitle_text='sin'))
fig.add_trace(go.Scatter(x=x, y=y2 - 0.0, legendgroup='cos', legendgrouptitle_text='cos'))
fig.add_trace(go.Scatter(x=x, y=y2 - 0.1, legendgroup='cos', legendgrouptitle_text='cos'))

fig.update_layout(legend_groupclick='toggleitem')

fig.write_html('plotly_python.html')

```

---

<div class="post-metadata">

### Author: ![Mo-Gul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mo-gul/32/33308_2.png) [@Mo-Gul](https://discourse.julialang.org/u/Mo-Gul)
#### Post date: [March 4, 2022, 4:59am UTC](https://discourse.julialang.org/t/legend-groupclick-toggleitem-doesnt-seem-to-work-in-plotlyjs/76851/2 "2022-03-04T04:59:19Z")

</div>

Maybe also related

[https://github.com/JuliaPlots/PlotlyJS.jl/issues/118](https://github.com/JuliaPlots/PlotlyJS.jl/issues/118)

---

<div class="post-metadata">

### Author: ![Mo-Gul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mo-gul/32/33308_2.png) [@Mo-Gul](https://discourse.julialang.org/u/Mo-Gul)
#### Post date: [March 8, 2022, 7:34pm UTC](https://discourse.julialang.org/t/legend-groupclick-toggleitem-doesnt-seem-to-work-in-plotlyjs/76851/3 "2022-03-08T19:34:13Z")

</div>

For the record. The issue seems to be that the requested feature was introduced in Plotly.js [v2.4.1](https://github.com/plotly/plotly.js/releases/tag/v2.4.1), but PlotlyBase.jl currently refers to [v2.3.0](https://github.com/sglyon/PlotlyBase.jl/blob/2a5c8b87679ced5af7ac3894950ef0ea845eaa61/src/output.jl#L15).

---

<div class="post-metadata">

### Author: ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)
#### Post date: [June 6, 2023, 2:05pm UTC](https://discourse.julialang.org/t/legend-groupclick-toggleitem-doesnt-seem-to-work-in-plotlyjs/76851/4 "2023-06-06T14:05:43Z")

</div>

> [@Mo-Gul](#):
>
> PlotlyBase.jl

Is there a chance to use this feature by means of low level commands?

---

<div class="post-metadata">

### Author: ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)
#### Post date: [June 6, 2023, 9:13pm UTC](https://discourse.julialang.org/t/legend-groupclick-toggleitem-doesnt-seem-to-work-in-plotlyjs/76851/5 "2023-06-06T21:13:21Z")

</div>

The package [PlotlyLight.jl](https://github.com/JuliaComputing/PlotlyLight.jl) knows about the feature: `groupclick`.  
Here is my sample code:

```julia
using PlotlyLight
# --- Docu: https://plotly.com/javascript/

# --- Change template
# PlotlyLight.Preset.Template.plotly_dark!()
# --- Make plot, first two traces at once in two legendgroups:
fig_obj = PlotlyLight.Plot()(
        x = 1:20, y = cumsum(randn(20)), type="scatter", mode="lines+markers",
        legendgroup = "A", legendgrouptitle = Dict("text" => "group_name A"),)(
        x = 1:20, y = cumsum(randn(20)), type="scatter", mode="lines+markers",
        legendgroup = "B", legendgrouptitle = Dict("text" => "group_name B"),);
# --- add new trace:
fig_obj()(
    x = 1:20, y = cumsum(randn(20)), type="scatter", mode="lines+markers", legendgroup = "B", name = "new trace");

# ---
fig_obj.layout.legend.groupclick = "toggleitem"
display(fig_obj)

```

---

<div class="post-metadata">

### Author: ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)
#### Post date: [April 16, 2026, 3:19pm UTC](https://discourse.julialang.org/t/legend-groupclick-toggleitem-doesnt-seem-to-work-in-plotlyjs/76851/7 "2026-04-16T15:19:48Z")

</div>

Good news, since August 2025, it works 🙂  
Here my sample code:

````julia
using PlotlyJS
xToggleGroup = true
#: ---
function toggle_item(xTogglegroup::Bool=true)
    x = range(start=0, stop=10, length=101)
    y1 = sin.(x)
    y2 = sin.(x) .- 0.1
    y3 = cos.(x)
    y4 = cos.(x) .- 0.1
    #: ---
    trace1 = PlotlyJS.scatter(x=x, y=y1, legendgroup="sin", legendgrouptitle_text="sin")
    trace2 = PlotlyJS.scatter(x=x, y=y2, legendgroup="sin", legendgrouptitle_text="sin")
    trace3 = PlotlyJS.scatter(x=x, y=y3, legendgroup="cos", legendgrouptitle_text="cos")
    trace4 = PlotlyJS.scatter(x=x, y=y4, legendgroup="cos", legendgrouptitle_text="cos")
    #: ---
    traces = [trace1, trace2, trace3, trace4]
    if xTogglegroup
        sGroupClickItem = "togglegroup"
    else
        sGroupClickItem = "toggleitem"
    end
    layout = PlotlyJS.Layout(
        legend = PlotlyJS.attr( # ← nested mit attr()
            groupclick = sGroupClickItem # "toggleitem" oder "togglegroup" je nach Wunsch
        )
    )
    return PlotlyJS.Plot(traces, layout)
end

fig = toggle_item(xToggleGroup); PlotlyJS.display(fig)    

# Save as html
if xToggleGroup
    fn_html = raw"c:\tmp\plt\legend_groupclick_togglegroup.html"
else
    fn_html = raw"c:\tmp\plt\legend_groupclick_toggleitem.html"
end
open(fn_html, "w") do io
    PlotlyBase.to_html(io, fig)
end
@info "\"$fn_html\" plotted!"```
````
