Skip to content

Horizontal legend items not visible when many legend items #769

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

Closed
psalmody opened this issue Jul 22, 2016 · 16 comments
Closed

Horizontal legend items not visible when many legend items #769

psalmody opened this issue Jul 22, 2016 · 16 comments
Labels
community community contribution feature something new
Milestone

Comments

@psalmody
Copy link
Contributor

Was pointed out in #766 that scrolling not intended for layout.legend.orientation = 'h'. Example workaround is to use legend groups.

This is not a great solution, however, as it means clicking the legend group hides/shows ALL traces in the group (see jsfiddle).

Proposing that plotly.js implement something similar to ggplot2 which automatically wraps additional legend items on horizontal legend orientation. Especially on mobile portrait orientation, this allows the graph data to take up the entire width of the screen and push the legend below the graph which is much preferable in my view.
kpi-2-raceretention

@psalmody psalmody changed the title Horizontal legend items not visible when many items Horizontal legend items not visible when many legend items Jul 22, 2016
@etpinard
Copy link
Contributor

Thanks.

@etpinard etpinard added the feature something new label Jul 22, 2016
psalmody added a commit to psalmody/plotly.js that referenced this issue Jul 22, 2016
@psalmody
Copy link
Contributor Author

@etpinard Can you review this PR on my fork? I think it should resolves this issue. psalmody#1

@etpinard etpinard added the community community contribution label Jul 22, 2016
@etpinard
Copy link
Contributor

@psalmody I think that should do it.

One possible imperfection I see here:

image

is that the items on the second line don't line up with the items above them - unlike in the ggplot example you posted. Was that on purpose?

@psalmody
Copy link
Contributor Author

psalmody commented Jul 22, 2016

@etpinard I did notice that but decided it's probably more complicated than it's worth. Personally, I don't have any problems with the items not appearing in columns and I think that the legend groups workaround would still be possible if a user really wanted the lined-up columns.

@etpinard
Copy link
Contributor

I did notice that but decided it's probably more complicated than it's worth

Yeah. I agree. Your solution sounds like a solid fallback for extra-wide legends. If ever some users want a more fine-grained legend layout, we could always add support for something like:

layout.legend = {
  type: 'grid',
  grid: [ [ 'trace 0', 'trace 1', 'trace 2'], ['trace 4', 'trace 3'] ]
};

down the road.

@n-riesco what do you think?

@n-riesco
Copy link
Contributor

I'm unable to test this until Monday.

Does toggling traces work in this PR?

@psalmody
Copy link
Contributor Author

If by toggle you mean hide/show the trace by clicking the legend item, yes.

@n-riesco
Copy link
Contributor

@etpinard , @mdtusz I think this PR is ready for review.


This PR is an improvement over the current behaviour. I think there is a low hanging fruit that could be implemented in another PR. With this PR, a multiline horizontal legend would look like this:

One easy way to align the traces in a grid would be to iterate over all the traces to compute the maximum width, and use this width for all the traces.

@psalmody
Copy link
Contributor Author

@etpinard I've made a change to the pull request per @n-riesco suggestions about aligning traces in a grid. I agree that it looks much better and it was simpler to do than I originally thought.

image of plotly with grid legend

@psalmody
Copy link
Contributor Author

Sorry but new to this process. @etpinard or @mdtusz should I send a PR back to plotly/plotly.js from my fork?

@mdtusz
Copy link
Contributor

mdtusz commented Jul 27, 2016

Yep! Open a PR with your fork and we'll take a look at it!

@psalmody
Copy link
Contributor Author

@etpinard @mdtusz See #786

@yniknafs
Copy link

yniknafs commented Apr 20, 2018

@psalmody @etpinard

I understand that customizing the grid is probably more complicated than its worth. But something I noticed is that when more than one row appear, the x-positioning of the legend is lost. So if you have one row, you can center the legend with xanchor: center, x:0.5. Once you have 2 rows, it just defaults to far left and any of the xanchor, x options don't do anything.

All I did in second screenshot was make the plot narrower.

legend: {
      orientation: 'h',
      y: -0.5,
      xanchor: 'center',
      x: 0.5,
    },

image

image

@psalmody
Copy link
Contributor Author

@yniknafs It's a great idea. Not sure I personally have the time to modify the code though... :(

@viggotw
Copy link

viggotw commented Sep 11, 2019

I did notice that but decided it's probably more complicated than it's worth

Yeah. I agree. Your solution sounds like a solid fallback for extra-wide legends. If ever some users want a more fine-grained legend layout, we could always add support for something like:

layout.legend = {
  type: 'grid',
  grid: [ [ 'trace 0', 'trace 1', 'trace 2'], ['trace 4', 'trace 3'] ]
};

down the road.

@n-riesco what do you think?

I realize I am pretty late for this conversation, but that suggestion sounds fantastic. The problem with using legend groups to align/group the legends is that this also makes them visible/invisible together when you toggle-click the trace name in the legend. More than ones, I have wanted to group similar legends together, though be able to make them invisible seperatly.

@adibiasio
Copy link

Any updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community community contribution feature something new
Projects
None yet
Development

No branches or pull requests

7 participants