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

  • 1
    Why didn't the linked answer work? – HeikkiVesanto Sep 15 '21 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. – Florian Marlière Sep 15 '21 at 13:16

2 Answers2

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

HeikkiVesanto
  • 16,433
  • 2
  • 46
  • 68
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

Babel
  • 71,072
  • 14
  • 78
  • 208
  • 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. – Florian Marlière Sep 15 '21 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 '21 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 ? – Florian Marlière Sep 15 '21 at 16:02
  • Might be. From far, it's difficult to say. What QGIS version? – Babel Sep 15 '21 at 16:03
  • 3.20.2-Odense Uptated the data and project if you want to try it. Eitherway thanks for the answer :) – Florian Marlière Sep 15 '21 at 16:33
  • Which of the projects? Which layout? – Babel Sep 15 '21 at 17:08
  • The project Template SHP juillet 2021" and for the layouts, it will be automatically updated by the atlas composer. All layers are used in different page of the atlas composer. – Florian Marlière Sep 15 '21 at 17:29
  • For me, it seems to work with your project, see: https://i.stack.imgur.com/42SrL.png - But only in this layout; the other two have no legend and I get an error saying No matching atlas features found!. I'm not sure but I guess the others are raster layers. With rasters, this solution does not seem to work – Babel Sep 16 '21 at 12:44
  • Indeed it works with this layout. The other two are the ones where i did an atlas focused on only 1 polygon, and several layers per pages. Maybe for those ones, there is too much data to handle idk. Your error tho should comes from the path of the layer "Site". If you make a new shp and then change the path of the layer "Site" it should works. – Florian Marlière Sep 16 '21 at 13:15
  • Is it possible to wrap the legend in the principal map canvas to avoid this issue ? – Florian Marlière Sep 16 '21 at 13:47
  • You could try in layer properties / legend, but I'm not sure if it offers such an option – Babel Sep 16 '21 at 13:51
  • No option found, just so you know :) – Florian Marlière Sep 16 '21 at 14:34
  • Probably your prblem indeed has to do with the fact that you're project is quite large. Maybe create a dummy project with just a few simple layers to see if that works. Than at least it's clear where the problem is. – Babel Sep 16 '21 at 14:37