Wichtig

Übersetzen ist eine Gemeinschaftsleistung Sie können mitmachen. Diese Seite ist aktuell zu 17.14% übersetzt.

22.2.4. Das Beschriftungselement

The Label item is a tool that helps decorate your map with texts that would help to understand it; it can be the title, author, data sources or any other information… You can add a label with the label Add Label tool following items creation instructions and manipulate it the same way as exposed in Interacting with layout items.

By default, the label item provides a default text that you can customize using its Item Properties panel. Other than the items common properties, this feature has the following functionalities (see Abb. 22.24):

../../../../_images/label_mainproperties.png

Abb. 22.24 Label Item Properties Panel

22.2.4.1. Haupteigenschaften

The Main properties group is the place to provide the text of the label. The text can be static, dynamic with expression functions and variables, and/or formatted with HTML. Dynamic parts of a label need to be surrounded by [% and %] in order to be interpreted and evaluated as such.

  • To use expressions in labels, you can click on Insert/Edit Expression… button, write your formula as usual and when the dialog is applied, QGIS automatically adds the surrounding characters.

    Hinweis

    Clicking the Insert/Edit Expression… button when no selection is made in the textbox will append the new expression to the existing text. If you want to modify an existing expression, you need to first select the part of interest.

    Because maps are usually filled with some common textual information (date, author, title, page number, …), QGIS provides a direct access to the corresponding expressions or variables: press the Dynamic text button to select and insert them into your label.

    Tipp

    The top menu Add Item ► Add Dynamic Text ► can be used to create a new label item filled with the selected predefined expression.

    It’s possible to turn a dynamic label into static: press the drop-down arrow next to the Insert/Edit Expression… button and select Convert to Static. Any dynamic parts of the label’s contents will be evaluated and replaced with their current values. You can then manually tweak the resulting text when needed.

  • Labels can be interpreted as HTML code: check checkbox Render as HTML. You can now insert HTML tags or styles, URL, a clickable image that links to a web page, or something more complex…

The following code combines HTML rendering with expressions, for an advanced labeling and will output Abb. 22.25:

<html>
 <head>
   <style>
      /* Define some custom styles, with attribute-based size */
      name {color:red; font-size: [% ID %]px; font-family: Verdana; text-shadow: grey 1px 0 10px;}
      use {color:blue;}
   </style>
 </head>

 <body>
   <!-- Information to display -->
   <u>Feature Information</u>
   <ul style="list-style-type:disc">
     <li>Feature Id: [% ID %]</li>
     <li>Airport: <name>[% NAME %]</name></li>
     <li>Main use: <use>[% USE %]</use></li>
   </ul>
   Last check: [% concat( format_date( "control_date", 'yyyy-MM-dd'), ' by <b><i>', @user_full_name, '</i></b>' ) %]

   <!-- Insert an image -->
   <p align=center><img src="path/to/logos/qgis-logo-made-with-color.svg" alt="QGIS icon" style="width:80px;height:50px;"</p>
 </body>
</html>
../../../../_images/label_htmlexpression.png

Abb. 22.25 Leveraging a label with HTML styling

22.2.4.2. Darstellung

  • Define font and style of the text by clicking on the Font button. In the Label Font menu you can use some of the options for Formatting the label text.

  • You can specify different horizontal and vertical margins in mm. This is the margin from the edge of the layout item. The label can be positioned outside the bounds of the label e.g. to align label items with other items. In this case you have to use negative values for the margin.

  • Using the text alignment is another way to position your label. It can be:

    • Left, Center, Right or Justify for Horizontal alignment

    • and Top, Middle, Bottom for Vertical alignment.

22.2.4.3. Exploring expressions in a label item

Below some examples of expressions you can use to populate the label item with interesting information - remember that the code, or at least the calculated part, should be surrounded by [% and %] in the Main properties frame:

  • Display a title with the current atlas feature value in „field1“:

    'This is the map for ' || "field1"
    

    or, written in the Main properties section:

    This is the map for [% "field1" %]
    
  • Add a pagination for processed atlas features (eg, Page 1/10):

    concat( 'Page ', @atlas_featurenumber, '/', @atlas_totalfeatures )
    
  • Return the name of the airports of the current atlas region feature, based on their common attributes:

    aggregate( layer := 'airports',
               aggregate := 'concatenate',
               expression := "NAME",
               filter := fk_regionId = attribute( @atlas_feature, 'ID' ),
               concatenator := ', '
             )
    

    Or, if an attributes relation is set:

    relation_aggregate( relation := 'airports_in_region_relation',
                        aggregate := 'concatenate',
                        expression := "NAME",
                        concatenator := ', '
                      )
    
  • Return the name of the airports contained in the current atlas region feature, based on their spatial relationship:

    aggregate( layer := 'airports',
               aggregate := 'concatenate',
               expression := "NAME",
               filter := contains( geometry( @parent ), $geometry ),
               concatenator := ', '
             )
    

    OR:

    array_to_string( array:= overlay_contains( layer := 'airports',
                                               expression := "NAME" ),
                     delimiter:= ', '
                   )
    
  • Return the lower X coordinate of the Map 1 item’s extent:

    x_min( map_get( item_variables( 'Map 1' ), 'map_extent' ) )
    
  • Retrieve the name of the layers in the current layout Map 1 item, and formats in one name by line:

    array_to_string(
     array_foreach(
      map_get( item_variables( 'Map 1' ), 'map_layers' ), -- retrieve the layers list
      layer_property( @element, 'name' ) -- retrieve each layer name
     ),
     '\n' -- converts the list to string separated by breaklines
    )
    
  • Display the list of layers with their license strings (usage rights) in a layout Map 1 item. You need to fill the layers‘ Access metadata properties first.

    array_to_string( map_credits( 'Map 1', true ) )