Click or drag to resize

LabelDefinition Class

Describes an object that defines the text, appearance, and position of labels for features within a given scale range via ArcGIS JSON.
Inheritance Hierarchy
SystemObject
  Esri.ArcGISRuntime.MappingLabelDefinition

Namespace:  Esri.ArcGISRuntime.Mapping
Assembly:  Esri.ArcGISRuntime (in Esri.ArcGISRuntime.dll) Version: 100.11.0
Syntax
public sealed class LabelDefinition

The LabelDefinition type exposes the following members.

Constructors
  NameDescription
Public methodLabelDefinition
Initializes a new instance of the LabelDefinition class with the specified LabelExpression and TextSymbol.
Top
Properties
  NameDescription
Public propertyAngle
Gets or sets the LabelAngle that specifies angular positions and layout directions for labels on or around point feature symbols.
Public propertyDeconflictionStrategy
Gets or sets the strategy for moving labels to avoid overlapping point symbols or higher priority labels.
Public propertyExpression
Gets or sets the expression script used to calculate the label text.
Public propertyFeatureBoundaryOverlapStrategy
Gets or sets the strategy for whether other labels are allowed to overlap the boundary of polygon features being labeled by this LabelDefinition.
Public propertyFeatureInteriorOverlapStrategy
Gets or sets the strategy for whether other labels are allowed to overlap the polygon features being labeled by this LabelDefinition.
Public propertyLabelOverlapStrategy
Gets or sets the strategy for whether other labels are allowed to overlap labels created by this LabelDefinition.
Public propertyLineConnection
Gets or sets the strategy for whether line features with the same label, and matching end vertices, should be joined before sharing a label.
Public propertyMaxScale
Gets or sets the maximum scale at which labels will be visible.
Public propertyMinScale
Gets or sets the minimum scale at which labels will be visible.
Public propertyMultipartStrategy
Gets or sets the strategy for how many labels should be placed on each polyline or polygon (multi-ring) feature.
Public propertyName
Gets or sets the text name for the class of labels.
Public propertyOffsetDistance
Gets or sets the distance (in points i.e. 1/72 inches) that text should be moved away from its features.
Public propertyOverrunStrategy
Gets or sets the strategy for whether a label will be visible at scales where the feature is too small for the label to fit.
Public propertyPlacement
Gets or sets the preferred position of the text label, with respect to its feature geometry.
Public propertyPriority
Gets or sets a number that defines which labels are placed first, and can also supplant existing lower-priority labels (e.g. during panning).
Public propertyRemoveDuplicatesDistance
Gets or sets a value that controls the duplicate thinning radius (in points i.e. 1/72 inches).
Public propertyRemoveDuplicatesStrategy
Gets or sets the strategy for whether labels are removed if other features have the same text label. This can reduce clutter and free up space.
Public propertyRepeatDistance
Gets or sets the distance apart (in points i.e. 1/72 inches) that the repetitions should be.
Public propertyRepeatStrategy
Gets or sets the strategy for whether a label should have multiple copies created and placed along or across the same line feature.
Public propertyStackAlignment
Gets or sets the strategy for how multi-row (stacked) text should be aligned.
Public propertyStackBreakPosition
The strategy for whether a row of text should be broken before or after it exceeds the ideal length.
Public propertyStackRowLength
Gets or sets a value that limits the number of characters in a row of stacked text.
Public propertyStackSeparators
Gets a collection of stack separators that should be used for automatic line breaking of label text.
Public propertyStackStrategy
Gets or sets the strategy for whether the text should be stacked or wrapped, rather than placed as long trailing labels across the map.
Public propertyTextLayout
Gets or sets the layout of the text, which can be either horizontal, straight, perpendicular, or follow the line feature.
Public propertyTextOrientation
Gets or sets the orientation of the text, which can be either angle direction or screen-oriented.
Public propertyTextSymbol
Gets or sets text symbol which describes the overall appearance of label text (e.g. font and size).
Public propertyUnknownJson
Gets a collection of unknown data from the source JSON.
Public propertyUnsupportedJson
Gets a collection of unsupported data from the source JSON.
Public propertyUseCodedValues
Gets or sets a value specifying whether the data source should translate domain identifiers into meanings using a CodedValueDomain.
Public propertyWhereClause
Gets or sets the SQL WHERE clause expression that limits the set of features for which labels will be generated and placed.
Top
Methods
  NameDescription
Public methodStatic memberCode exampleFromJson
Creates a label definition from an ArcGIS JSON label representation (labelingInfo).
Public methodToJson
Converts this label definition to an ArcGIS JSON label representation (labelingInfo).
Top
Remarks

A LabelDefinition describes a class of labels.

A collection of label definitions can be attached to a FeatureLayer, a SubtypeSublayer, a GraphicsOverlay, or an ArcGISMapImageSublayer of a dynamic ArcGISMapImageLayer.

Each LabelDefinition specifies:

  • which features are covered by the definition, by applying the WhereClause SQL constraint
  • what text should be displayed, based on the individual feature's attributes and the chosen Expression formula
  • how the text should appear, using the label definition's TextSymbol
  • where the text should appear, with respect to its feature, using the Placement placement
  • how to move or avoid other labels or features that would overlap each label.
Examples

WPF

Example Name: ShowLabelsOnLayer

Display custom labels on a feature layer.

Code example screen shot.

C#
// Copyright 2018 Esri.
// 
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.

using System;
using System.Windows;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;

namespace ArcGISRuntime.WPF.Samples.ShowLabelsOnLayer
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        name: "Show labels on layers",
        category: "Layers",
        description: "Display custom labels on a feature layer.",
        instructions: "Pan and zoom around the United States. Labels for congressional districts will be shown in red for Republican districts and blue for Democrat districts. Notice how labels pop into view as you zoom in.",
        tags: new[] { "attribute", "deconfliction", "label", "labeling", "string", "symbol", "text", "visualization" })]
    public partial class ShowLabelsOnLayer
    {
        // Help regarding the Json syntax for defining the LabelDefinition.FromJson syntax can be found here:
        // https://developers.arcgis.com/web-map-specification/objects/labelingInfo/
        private const string RedLabelJson =
            @"{
                    ""labelExpressionInfo"":{""expression"":""$feature.NAME + ' (' + left($feature.PARTY,1) + ')\\nDistrict' + $feature.CDFIPS""},
                    ""labelPlacement"":""esriServerPolygonPlacementAlwaysHorizontal"",
                    ""where"":""PARTY = 'Republican'"",
                    ""symbol"":
                        { 
                            ""angle"":0,
                            ""backgroundColor"":[0,0,0,0],
                            ""borderLineColor"":[0,0,0,0],
                            ""borderLineSize"":0,
                            ""color"":[255,0,0,255],
                            ""font"":
                                {
                                    ""decoration"":""none"",
                                    ""size"":10,
                                    ""style"":""normal"",
                                    ""weight"":""normal""
                                },
                            ""haloColor"":[255,255,255,255],
                            ""haloSize"":2,
                            ""horizontalAlignment"":""center"",
                            ""kerning"":false,
                            ""type"":""esriTS"",
                            ""verticalAlignment"":""middle"",
                            ""xoffset"":0,
                            ""yoffset"":0
                        }
               }";

        private const string BlueLabelJson =
            @"{
                    ""labelExpressionInfo"":{""expression"":""$feature.NAME + ' (' + left($feature.PARTY,1) + ')\\nDistrict' + $feature.CDFIPS""},
                    ""labelPlacement"":""esriServerPolygonPlacementAlwaysHorizontal"",
                    ""where"":""PARTY = 'Democrat'"",
                    ""symbol"":
                        { 
                            ""angle"":0,
                            ""backgroundColor"":[0,0,0,0],
                            ""borderLineColor"":[0,0,0,0],
                            ""borderLineSize"":0,
                            ""color"":[0,0,255,255],
                            ""font"":
                                {
                                    ""decoration"":""none"",
                                    ""size"":10,
                                    ""style"":""normal"",
                                    ""weight"":""normal""
                                },
                            ""haloColor"":[255,255,255,255],
                            ""haloSize"":2,
                            ""horizontalAlignment"":""center"",
                            ""kerning"":false,
                            ""type"":""esriTS"",
                            ""verticalAlignment"":""middle"",
                            ""xoffset"":0,
                            ""yoffset"":0
                        }
               }";

        public ShowLabelsOnLayer()
        {
            InitializeComponent();
            Initialize();
        }

        private async void Initialize()
        {
            // Create a map with a light gray canvas basemap.
            Map sampleMap = new Map(BasemapStyle.ArcGISLightGray);

            // Assign the map to the MapView.
            MyMapView.Map = sampleMap;

            // Define the URL string for the feature layer.
            string layerUrl = "https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_115th_Congressional_Districts/FeatureServer/0";

            // Create a service feature table from the URL.
            ServiceFeatureTable featureTable = new ServiceFeatureTable(new System.Uri(layerUrl));

            // Create a feature layer from the service feature table.
            FeatureLayer districtFeatureLabel = new FeatureLayer(featureTable);

            // Add the feature layer to the operations layers collection of the map.
            sampleMap.OperationalLayers.Add(districtFeatureLabel);

            try
            {
                // Load the feature layer - this way we can obtain it's extent.
                await districtFeatureLabel.LoadAsync();

                // Zoom the map view to the extent of the feature layer.
                MyMapView.SetViewpoint(new Viewpoint(new MapPoint(-10846309.950860, 4683272.219411, SpatialReferences.WebMercator), 20000000));

                // Create a label definition from the JSON string. 
                LabelDefinition redLabelDefinition = LabelDefinition.FromJson(RedLabelJson);
                LabelDefinition blueLabelDefinition = LabelDefinition.FromJson(BlueLabelJson);

                // Add the label definition to the feature layer's label definition collection.
                districtFeatureLabel.LabelDefinitions.Add(redLabelDefinition);
                districtFeatureLabel.LabelDefinitions.Add(blueLabelDefinition);

                // Enable the visibility of labels to be seen.
                districtFeatureLabel.LabelsEnabled = true;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString(), "Error");
            }
        }
    }
}
XAML
<UserControl x:Class="ArcGISRuntime.WPF.Samples.ShowLabelsOnLayer.ShowLabelsOnLayer"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013">
    <Grid>
        <esri:MapView x:Name="MyMapView" />
    </Grid>
</UserControl>
See Also
Additional Examples
Hyperlink to ExampleDescription
DisplaySubtypeFeatureLayerDisplays a composite layer of all the subtype values in a feature class.
ShowLabelsOnLayerDisplay custom labels on a feature layer.