2

I want to wrap legend text after a certain amount of characters depending on the width (variable) of the legend item.

I already tried the answer given at Wrapping legend text in QGIS MapComposer at character count. This solution is not working well for me. Is there a way to apply a expression to all layers ?

I want to have a automatic legend text for my atlas composition without having to manually wrap text legend.

Exemple 1 of atlas page

enter image description here

Exemple 2 of atlas page

enter image description here

Legend text of all layers

enter image description here

When using the expression wordwrap it seems to work at first but then QGIS goes suddenly very slow. The text legend seems to be constantly loading beetween wrapped and unwrapped mode.

Picture of wrapped text with the expression

enter image description here

Picture of unwrapped text with the same expression, one second later

enter image description here

Data and Project links

2
  • 1
    Why didn't the linked answer work? Sep 15, 2021 at 11:52
  • 1
    To be honest i don't really know : it works for some layers and it does not for others. I don't really understand how the expression works either. It seems also that applying this expression to all my layers make the map composer very slow. Sep 15, 2021 at 13:16

2 Answers 2

8

There is a much simpler expression you can try.

Using the same "Expression button".

You can use:

wordwrap(@symbol_label, 5)

With 5 here representing the number of characters it should wrap after.

enter image description here

3
  • very elegant solution!
    – christoph
    Sep 15, 2021 at 14:49
  • Thanks for the answer ! Sep 15, 2021 at 16:33
  • Hi @FlorianMarlière if the answer has solved your issue please mark it as correct using the green tick near the voting buttons. Sep 16, 2021 at 8:58
4

To add to @HeikkiVesanto's solution: Go to Project / Properties / Variables, create a new variable called project_wrap and insert the expression there.

The advantage is the possibility to automatically update the length of the lines of all layers at once in the legend. Use the variable in the print composer (instead of the whole expression): paste eval(@project_wrap). If you then change the variable definition (change no. of characters), the line-length automatically changes at once for all lines defined with this variable. Not need to manually tweek each line separately.

Updating of the legend takes a while. Simply open the expression button and close immediately, then the legends updates.

enter image description here

enter image description here

13
  • The concept works fine but when i do this, QGIS suddenly goes very slow and the legend seems to constantly be loading, switching beetween wrapped text and unwrapped text. I'll edit the post to show what i mean. Sep 15, 2021 at 15:23
  • I guess best would be if you could share the project (with data) for testing. Save project, close QGIS an reopen project does not help?
    – Babel
    Sep 15, 2021 at 15:29
  • Reopening QGIS does not help. I tried to use this expression in a simple map composer (no atlas) and it worked great. So the issue seems to come frome the Atlas Composer ? Sep 15, 2021 at 16:02
  • Might be. From far, it's difficult to say. What QGIS version?
    – Babel
    Sep 15, 2021 at 16:03
  • 3.20.2-Odense Uptated the data and project if you want to try it. Eitherway thanks for the answer :) Sep 15, 2021 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.