21.2.4. 标签项

这个 Label Item是一种工具,可以用有助于理解地图的文本来装饰地图;它可以是标题、作者、数据源或任何其他信息……您可以使用 标签 Add Label 刀具随动 items creation instructions 并以与 与布局项目交互

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 图 21.24):

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

图 21.24 标签项属性面板

21.2.4.1. 主要属性

这个 Main properties 组是提供标签文本的位置。文本可以是静态的、动态的 expression 函数和变量,和/或用HTML格式化。标签的动态部分需要用 [%%] 以便被这样解释和评价。

  • 要在标签中使用表达式,您可以单击 Insert/Edit Expression... 按钮,像往常一样编写公式,当应用对话框时,QGIS会自动添加周围的字符。

    提示

    单击 Insert/Edit Expression... 按钮在文本框中未进行任何选择时,将新表达式追加到现有文本。如果要修改现有的表达式,则需要首先选择感兴趣的部分。

    由于地图通常填充了一些常见的文本信息(日期、作者、标题、页码等),因此QGIS提供了对相应表达式或变量的直接访问:按 Dynamic text 按钮选择它们并将其插入您的标签中。

    小技巧

    最上面的菜单 Add Item ► Add Dynamic Text ► 可用于创建用选定的预定义表达式填充的新标签项。

    可以将动态标签转换为静态标签:按下 Insert/Edit Expression... 按钮并选择 Convert to Static 。标签内容的任何动态部分都将被计算并替换为其当前值。然后,您可以根据需要手动调整生成的文本。

  • 标签可以解释为HTML码:请检查 复选框 Render as HTML 。您现在可以插入HTML标记或样式、URL、链接到网页的可点击图像或其他更复杂的内容...

The following code combines HTML rendering with expressions, for an advanced labeling and will output 图 21.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

图 21.25 利用具有HTML样式的标签

21.2.4.2. 外观

  • 单击定义文本的字体和样式 Font 纽扣。在 Label Font 菜单,您可以使用其中的一些选项 Formatting the label text

  • 可以在中指定不同的水平和垂直边距 mm 。这是从布局项目边缘开始的边距。标签可以定位在标签的边界之外,例如以使标签物品与其他物品对齐。在这种情况下,您必须使用负值作为边距。

  • 使用文本对齐是放置标签的另一种方式。它可以是:

    • Left, Center, Right or Justify for Horizontal alignment

    • TopMiddleBottomVertical alignment

21.2.4.3. 浏览标签项中的表达式

下面是一些可用于用有趣的信息填充标签项的表达式示例-请记住,代码或至少计算出的部分应该用 [%%]Main properties 框架:

  • 在“field1”中显示具有当前地图集要素值的标题:

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

    或者,写在 Main properties 部分:

    This is the map for [% "field1" %]
    
  • 为已处理的地图集特征添加分页(例如, Page 1/10 ):

    concat( 'Page ', @atlas_featurenumber, '/', @atlas_totalfeatures )
    
  • 根据其共同属性,返回当前atlas区域要素的机场名称:

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

    或者,如果 attributes relation 已设置:

    relation_aggregate( relation := 'airports_in_region_relation',
                        aggregate := 'concatenate',
                        expression := "NAME",
                        concatenator := ', '
                      )
    
  • 根据机场的空间关系,返回当前地图集区域要素中包含的机场名称:

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

    或:

    array_to_string( array:= overlay_contains( layer := 'airports',
                                               expression := "NAME" ),
                     delimiter:= ', '
                   )
    
  • 属性的下X坐标。 Map 1 项目范围:

    x_min( map_get( item_variables( 'Map 1' ), 'map_extent' ) )
    
  • 检索当前布局中的层的名称 Map 1 项,并逐行在一个名称中设置格式:

    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
    )
    
  • 在布局中显示带有许可字符串(使用权限)的图层列表 Map 1 项目。你需要填充层‘ Access metadata 首先是属性。

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