Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

add options to include shapes and newshape in legends #6653

Merged
merged 30 commits into from Jul 25, 2023
Merged

Conversation

archmoj
Copy link
Contributor

@archmoj archmoj commented Jun 27, 2023

Resolves #98 ! 馃槑
@plotly/plotly_js
@chriddyp

src/core.js Outdated Show resolved Hide resolved
'with *reversed* `legend.traceorder` they are on bottom/right side.',
'The default legendrank is 1000,',
'so that you can use ranks less than 1000 to place certain items before all unranked items,',
'and ranks greater than 1000 to go after all unranked items.'
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we say something about unranked or equal rank items? I presume traces according to their order in data, then shapes according to their order in layout.shapes?

Also I don't see shapes[].legendrank used in a test image to intersperse shapes and traces, or shapes[].legend to put shapes on multiple legends, can we add those?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call.
Addressed in 2e77fc7, 58b1cb1 and 3168078.

@LiamConnors
Copy link
Contributor

@archmoj, trying this out with Plotly.py, if i set itemclick to toggleothers on the legend, with a graph with three traces and two shapes, when I click on a trace, it only toggles the other two traces.

image

And if I click on a shape, it only toggles the three traces, not the other shape

image
import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[1, 2, 3, 4, 5],
    mode='markers',
    marker={'size':50}
))

fig.add_trace(go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[5, 4, 3, 2, 1],
    mode='markers',
    marker={'size':25}
))

fig.add_shape(
    showlegend=True,
    type="line",
    x0=3,
    y0=0.5,
    x1=5,
    y1=0.8,
    line_width=3,
    label=dict(texttemplate="Slope: %{slope:.3f}", font=dict(size=20)),
)


fig.add_shape(
    showlegend=True,
    type="rect",
    fillcolor="yellow",
    x0=1.5,
    y0=2.5,
    x1=5,
    y1=5.5,
    line_width=3,
    label=dict(
        texttemplate="Width: %{width:.3f}",
        font=dict(family="Courier New, monospace", size=20),
    ),
)

fig.add_trace(go.Scatter(
    x=[1, 2, 3, 4, 5],
    y=[5, 4, 3, 2, 1],
    mode='markers',
    marker={'size':25}
))

fig.update_layout(legend= {'itemclick': 'toggleothers'})

fig.show()


Also, the shapes always appear after the traces in the legend, even if I define them first. How is the legendrank supposed to work by default if I don't set it for a particular trace/shape?
I would have thought they'd appear in the order defined.

@archmoj
Copy link
Contributor Author

archmoj commented Jul 7, 2023

@LiamConnors Thanks for your feedback.
I made various improvements in this PR as well as what included in v2.24.3.
I appreciate it if you could possibly QA this PR again. 馃檹

Regarding your question about shapes being presented at the end of traces, if one wants shapes to be displayed first, they could e.g. set legendrank: 1 in each trace. I also improved the legendrank documentation in this regards. Please let me know if you have any suggestions.

Thank you!

@LiamConnors
Copy link
Contributor

Thanks @archmoj! That update to the description makes sense and I'll add an example to the docs that shows this. I still encounter the issue when itemclick is set to toggleothers.

@alexcjohnson
Copy link
Contributor

I notice a bug that seems to be new in this PR: if you have multiple legends, with or without shapes, you can double-click a trace or shape to isolate it, but if you double-click the same trace or shape again it doesn't bring the others back.

Copy link
Contributor

@alexcjohnson alexcjohnson left a comment

Choose a reason for hiding this comment

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

You fixed my last comment in #6684 so this is good to go after the conflict is resolved! Nice work! 馃拑

@archmoj archmoj merged commit 16d5d22 into master Jul 25, 2023
1 check passed
@archmoj archmoj deleted the shape-legends branch July 25, 2023 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Include shapes in legends
3 participants