Customizing the order of Legends

We are creating a custom visual in Power BI using plotly. Currently, by default, the legends are displayed in certain order and we need to allow the user to specify this legend display order in Power BI. Any inputs on how to write plotly code for this? Thanks in advance.

Legend items are listed in the same order as their corresponding items in gd.data from top to bottom (with the exception of stack bars which are shown in reversed order by default).

You can reverse that order using layout.traceorder: https://plot.ly/javascript/reference/#layout-legend-traceorder

You can also try using legend groups e.g. https://plot.ly/javascript/legend/

Unfortunately, we don’t offer a way to “fully” customize the legend ordering at the moment.

1 Like

The ability to reorder legend items would be really helpful. I see that there’s the categoryorder (and category_orders) for plotly express, however that reorders the actual traces, which breaks my plot. So it doesn’t seem like it’s possible for me to re-order the legend.

2 Likes

I agree with you, something like:

layout.traceorder: 'alphabetical'

would be for sure very helpful.

Looks like this hasn’t moved since 2020.

Is there an established way to sort legend entries by alphabetical order?