Skip to content

Horizontal legend items not visible when many legend items #769

Description

@psalmody

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

Activity

  1. changed the title [-]Horizontal legend items not visible when many items[/-] [+]Horizontal legend items not visible when many legend items[/+] on Jul 22, 2016
  2. etpinard commented on Jul 22, 2016

    @etpinard
    Contributor

    Thanks.

  3. added a commit that references this issue on Jul 22, 2016
    bb60510
  4. psalmody commented on Jul 22, 2016

    @psalmody
    ContributorAuthor

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

  5. etpinard commented on Jul 22, 2016

    @etpinard
    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?

  6. psalmody commented on Jul 22, 2016

    @psalmody
    ContributorAuthor

    @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.

  7. etpinard commented on Jul 22, 2016

    @etpinard
    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?

  8. n-riesco commented on Jul 22, 2016

    @n-riesco
    Contributor

    I'm unable to test this until Monday.

    Does toggling traces work in this PR?

  9. psalmody commented on Jul 22, 2016

    @psalmody
    ContributorAuthor

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

  10. n-riesco commented on Jul 26, 2016

    @n-riesco
    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.

  11. psalmody commented on Jul 26, 2016

    @psalmody
    ContributorAuthor

    @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

  12. psalmody commented on Jul 27, 2016

    @psalmody
    ContributorAuthor

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

  13. mdtusz commented on Jul 27, 2016

    @mdtusz
    Contributor

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

  14. psalmody commented on Jul 27, 2016

    @psalmody
    ContributorAuthor
  15. added this to the v1.16.0 milestone on Jul 28, 2016
  16. yniknafs commented on Apr 20, 2018

    @yniknafs

    @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

  17. psalmody commented on Apr 20, 2018

    @psalmody
    ContributorAuthor

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

  18. viggotw commented on Sep 11, 2019

    @viggotw

    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.

  19. adibiasio commented on Jun 15, 2020

    @adibiasio

    Any updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    communitycommunity contributionfeaturesomething new

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions