3

Having a building table, grouped by an id and its respective amount of apartments. I want to label every symbol id on map composer legend with the amount of apartments.

enter image description here

For a simple expression 'apartments', nothing appears regarding amount of apartments neither any field inserted on expression.

I think issue comes because table has building level and all values regarding symbol id are repeated for the buildings are grouped on each id.

Even for repeated amounts I have written the expression: concat( @symbol_label, ': ',apartments) which avoid any repeated value and is not working. Legend appears as in the image from above. Is weird because on expression window the expression returns value (178),

enter image description here

8
  • Do you mean you want to sum all the apartments for each group? for example you have 7 rows with 161 apartments and id 1, you want the legend label to be "1127 apts"?
    – Dror Bogin
    Jan 25 at 10:40
  • No, sum is already done on apartments field. As it can be seen on map image, every building has its own information, like a correlative sum of apartments regarding a specific geometric order. I'm saying this because I can't work with a non repeated values for id and apartments
    – Coldon
    Jan 25 at 10:43
  • So you are looking for count("id","id") (second "id" is the group by clause) which counts the amount of same id's?
    – MrXsquared
    Jan 25 at 11:48
  • mmm... no. I just want to label apartments field in the legend. Seems very simple but it doesn't work with a simple expression 'apartments'
    – Coldon
    Jan 25 at 11:52
  • 1
    Probably is why I'm asking. I don't get it either... Even for repeated amounts I have written the expression: concat( @symbol_label, ': ',apartments) which avoid any repeated value and is not working. Legend appears as in the image from above. Is weard because on expression window the expression returns value (178)
    – Coldon
    Jan 25 at 12:11
6

Obviously, you can not use a field in print composer legend. Only @symbol_id, @symbol_count and @symbol_label will work. And of course all normal expressions, we will come to them at the end of this answer.

So the easiest solution seems to be to leave your print composer and go back to your main QGIS canvas. Open the layer properties, go to symbology and categorize your layer again with this expression: "id" || ': ' || "apartments".

enter image description here

(Indeed, it would be way nicer if one could choose an expression for the legend, but seems like that is not the case. There is most likely some neat Python solution possible though..)

If you now go back to your composer, you can simply add the legend:

enter image description here

If you do not wish to display the "id"s you can use some other expressions like regex_substr() or right(), e.g. right(@symbol_label,3) or more robust regexp_substr(@symbol_label,'[^\\s]*$'), which matches all characters after a whitespace:

enter image description hereenter image description here

Which results in:

enter image description here

1
  • Great! Definetely works! Even regex did its job ;)
    – Coldon
    Jan 25 at 13:16

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.