In the print composer (also in the layer selector) I can choose to automatically display the amount of objects in that category in square brackets. That is useful for point features/layers. But in the case of line layers I would like to show the total length of all lines in that category (sum) in just the same fashion.

It is possible to get that info using the GroupStats plugin. But I am looking for a way to automatically keep my legend updated with that info. Just as neat and uncomplicated as with the square brackets for the total count of objects.

1 Answer 1


Here is a solution based on virtual layer. Not entirely uncomplicated.

I tested on four lines in two categories, a and b, two lines in each category. The red and green category.

enter image description here

The trick is to use the category sum length as a categorized style. This will enable the length also in the print composer legend.

To sum the length in each category use a virtual layer. The SQL syntax is based on SQLite.

enter image description here

SELECT category, Geometry, SUM(GLength(Geometry)) as SumLength
FROM lines
GROUP BY category

Be aware that you only get the last geometry present for each category in the virtual layer, but this is fine since it's only for the print composer legend.

Style the virtual layer categorized with

Round( "SumLength" )

You will have to reclassify the style before printing since the sumlengths changes when you edit the line layer.

Resulting legend in print composer

enter image description here

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.