Vous êtes sur la page 1sur 7

Building label expressions

ArcGIS 10.4

Locate topic

You can use label expressions to adjust the formatting of your labels. In addition to inserting
characters and scripting functions, you can use ArcGIS formatting tags in label expressions.
These are special characters for changing the appearance of all or part of your labels. For
example, you might use the bold formatting tag to make the first line bold in a stacked, multiline
label.

Learn more about formatting labels with text formatting tags

A label expression is limited to a single line of code unless you check the Advanced box on the
Label Expression dialog box. Checking the Advanced box allows you to enter a function
containing programming logic and spanning multiple lines of code.

Field values are automatically cast to text strings. Therefore, if you wish to use a numeric value
in an arithmetic operation, or when making a comparison, you will need to cast it back to a
numeric data type. The examples below add two integer fields:

Python

int([FIELD1]) + int([FIELD2])

VBScript

cint([FIELD1]) + cint([FIELD2])

JScript

parseInt([FIELD1]) + parseInt([FIELD2])

Steps:

1. Click the Label Manager button on the Labeling toolbar.

2. Click a label class in the Label Classes list.

3. Click the Expression button.

4. Choose a language on the Parser menu.

5. Type a Python, VBScript, or JScript expression. You can also create an expression
by double-clicking the field to add it to the expression or by selecting the field
and clicking the Append button to append the field to the end of the expression
separated by a space.

Fields are enclosed in square brackets [ ] irrespective of the data type of the
layer's data source.
Optionally, enter ArcGIS text formatting tags in the Expression box to apply
formatting to a portion of your label text.
If your expression will span multiple lines of code, check the Advanced check box
and enter your label expression.

6. Click Verify to make sure there are no syntax errors.

7. Click OK on each of the dialog boxes.

Tip:

Both regular and advanced label expressions can be saved as label expression files (.lxp), which
can be loaded into other layers or maps.

Expression examples
The following are examples of label expressions:

Concatenate a string to the value in a field. For example, this expression creates a label
where the value of the PARCELNO field is preceded by the text "Parcel no:":

Python

"Parcel no: " + [PARCELNO]

VBScript

"Parcel no: " & [PARCELNO]

JScript

"Parcel no: " + [PARCELNO]

Round a decimal number to a set number of decimals. For example, this expression
displays an Area field rounded to one decimal place:

Python
round(float([AREA]), 1)

VBScript

Round ([AREA], 1)

JScript

function FindLabel ( [AREA] )


{
var ss;
var num= parseFloat([AREA]);
ss = num.toFixed(1);
return (ss);
}

Convert your text labels to all uppercase or lowercase. For example, this expression
makes a Name field all lowercase:

Python

def FindLabel ( [NAME] ):


S = [NAME]
S = S.lower()
return S

VBScript

LCase ([NAME])

JScript

[NAME].toLowerCase()

Convert your text labels to proper case. For example, this expression takes a Name field
that is in all capitals and makes it proper case:
Python

def FindLabel ( [NAME] ):


S = [NAME]
S = S.title()
return S

VBScript

Function FindLabel ( [NAME] )


FindLabel = UCase(Left([NAME],1)) & LCase(Right([NAME], Len([NAME])
-1))
End Function

JScript

function FindLabel ( [NAME] )


{
var str = [NAME];
var iLen = String(str).length;
var upper = (str.substring(0,1)).toUpperCase();
var lower = (str.substring(1, iLen)).toLowerCase()
return upper + lower;
}

Create stacked text. For example, this expression creates a label with the Name field and
the two address fields all on separate lines:

Python

"Name: " + [NAME] + '\n' + [ADDRESS_1] + '\n' + [ADDRESS_2]

VBScript

"Name: " & [NAME] & vbCrLf& [ADDRESS_1] & vbCrLf& [ADDRESS_2]

JScript
"Name: " + [NAME] + "\r" + [ADDRESS_1] + "\r" + [ADDRESS_2]

Create stacked text based on text from one field. For example, this expression uses the
comma to specify where the stack happens:

Python

def FindLabel ( [LABELFIELD] ):


S = [LABELFIELD]
S = S.replace(', ', '\n')
return S

VBScript

Function FindLabel ( [LABELFIELD] )


FindLabel = replace([LABELFIELD], ", ", vbnewline)
End Function

JScript

function FindLabel ( [LABELFIELD] )


{
var r, re;
var str = [LABELFIELD];
re = /,/g;
r = str.replace(re, "\r");
return r;
}

Format your labels. For example, this expression displays the label as currency:

Python

def FindLabel ( [MAXIMUM_OC], [RATE] ):


import locale
locale.setlocale(locale.LC_ALL, '')
S = locale.currency(float([MAXIMUM_OC]) * float([RATE]))
return S
VBScript

"Occupancy Revenue: " & FormatCurrency ([MAXIMUM_OC] * [RATE])

JScript

function FindLabel ( [MAXIMUM_OC], [RATE] )


{
var ss;
var num1 = parseFloat([MAXIMUM_OC]);
var num2 = parseFloat([RATE]);
var num3 = num1 * num2
ss = num3.toFixed(2);
return ("$" + ss);
}

Specify a conditional if-else statement. These functions label cities with their name in a
large, red font if their population is equal to or exceeds 250,000 and in the default label
font if the population is less than 250,000:

Python

def FindLabel ( [NAME], [POPULATION] ):


if long([POPULATION]) >= 250000:
return "<CLR red='255'><FNT size = '14'>" + [NAME] +
"</FNT></CLR>"
else:
return [NAME]

VBScript

Function FindLabel ([NAME], [POPULATION])


if (cLng([POPULATION]) >= 250000) then
FindLabel = "<CLR red='255'><FNT size = '14'>" + [NAME] +
"</FNT></CLR>"
else
FindLabel = [NAME]
end if
End Function
JScript

function FindLabel ( [NAME], [POPULATION] )


{
if (parseFloat([POPULATION]) >= 250000){
return ("<CLR red='255'><FNT size = '14'>" + [NAME] +
"</FNT></CLR>");
}
else
return ([NAME]);
}

Vous aimerez peut-être aussi