0

My QGIS project includes three layers representing catchment areas for playgrounds. Depending on the age of the children, the catchment areas are either 200 m (catch_area_200m.shp), 400 m (catch_area_400m.shp), or 1000 m (catch_area_1000m.shp).

Within the print composer always just one catchment area is visible. Depending on which of the three layers is active, I want to have this information in the title of the legend.

What I basically need is some sort of conditional clause, checking which layer is activated and then printing a string with the appropriate distance, e.g. '400 Meters', to the title of the legend.

How can this be realised?

  • This would be an if-clause on a substring of the layer-name - the biggest issue (for me) is how to get the name of the active layer. – Erik Jun 16 at 12:23
0

You can solve it using the function is_layer_visible in an expression.

In your Legend > Item proprieties select the Data Defined Override on the right of the title and, then, Edit...

enter image description here

Here you can use this expression

case 
when is_layer_visible('poly') then concat ('100m')
when  is_layer_visible('poly 2') then concat ('200m')
else ''
end

Change the name on the layers (here poly and poly 2) with yours.

Now, the legend title will be depending on the activated layer.

| improve this answer | |
  • Thank you! Unfortunately it doesn't work for me. When I search for the function 'is_layer_visible' nothing appears (QGIS 3.4.4). It is supposed to be listed within the "General"-section of the field calculator? Or is a plugin required? – zehpunktbarron Jun 22 at 12:16
  • The function is part of the QGIS core. And it is available on 3.4 (see documentation here docs.qgis.org/3.4/en/docs/user_manual/working_with_vector/…). It is on General and available in all the expression and data defined override options. If you are using on the legend title it should be selectable. – Val P Jun 22 at 16:15

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.