LegendElement

Resumen

The LegendElement object provides access to properties that enables its repositioning on the page layout as well as modifying its title and getting its legend items and parent data frame.

Debate

Like a MapsurroundElement, the LegendElement object has an association with a single parent data frame. In addition, the LegendElement also has a method and properties for managing the contents within the legend. These are useful for controlling how new items get added to the legend, sizing the legend, and also for specifiying the number of columns in a legend.

Legend elements in a layout have a property that allows the legend to automatically update when layers are added or removed from a map. The autoAdd method toggles this behavior so you can control whether or not a newly added layer should appear in the legend.

The ListLayoutElements function returns a Python list of page layout element objects. It is necessary to then iterate through each item in the list or specify an index number to reference a specific page element object. To return a list of only LegendElements, use the LEGEND_ELEMENT constant for the element_type parameter. A wildcard can also be used to further refine the search based on the element name.

It is recommended that each page layout element be given a unique name so that it can be easily isolated using arcpy scripting. This is set via the Size and Position Tab on the Properties dialog box in ArcMap.

X and Y element positions are based on the element's anchor position, which is also set via the Size and Position tab on the Properties dialog box in ArcMap.

Page units can only be changed in ArcMap via Customize > ArcMap Options > Layout View Tab.

Propiedades

PropiedadExplicaciónTipo de datos
autoAdd
(Lectura y escritura)

Controls whether a layer should be automatically added to the legend when using the AddLayer or AddLayerToGroup functions. This property mimics the Map Connection check box option labeled Add a new item to legend when a new layer is added to the map, found via the Legend Properties dialog box's Item tab.

Boolean
elementHeight
(Lectura y escritura)

The height of the element in page units. The units assigned or reported are in page units.

Double
elementPositionX
(Lectura y escritura)

The x location of the data frame element's anchor position. The units assigned or reported are in page units.

Double
elementPositionY
(Lectura y escritura)

The y location of the data frame element's anchor position. The units assigned or reported are in page units.

Double
elementWidth
(Lectura y escritura)

The width of the element in page units. The units assigned or reported are in page units.

Double
items
(Sólo lectura)

Returns a list of strings that represents the individual legend item names.

String
name
(Lectura y escritura)

The name of the element.

String
parentDataFrameName
(Sólo lectura)

A string that represents the name of the data frame for the associated element.

String
title
(Lectura y escritura)

The text string that represents the legend's title.

String
type
(Sólo lectura)

Returns the element type for any given page layout element.

  • DATAFRAME_ELEMENTData frame element
  • GRAPHIC_ELEMENTGraphic element
  • LEGEND_ELEMENTLegend element
  • MAPSURROUND_ELEMENTMap surround element
  • PICTURE_ELEMENTPicture element
  • TEXT_ELEMENTText element
String

Vista general del método

MétodoExplicación
adjustColumnCount (column_count)

Provides a mechanism to set the number of columns in a legend.

Métodos

adjustColumnCount (column_count)
ParámetroExplicaciónTipo de datos
column_count

An integer that represents the desired number of columns.

(El valor predeterminado es 1)

Integer

There are plenty of cases where there is not enough space on a layout to fit all legend items in a single column. A legend can be interrogated using its elementHeight or elementWidth properties to determine the needed space on the page. Another method would be to count the number of items in a legend. Whichever method is used, adjustColumnCount can then be used to establish the number of desired columns in a legend.

Ejemplo de código

LegendElement example

The following script will add layers to a new data frame within a map document that includes an inserted legend element named Legend. The orthophoto will not get added to the legend. This is controlled using the autoAdd property. Finally, after the layers are added, the number of columns are adjusted to two.

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "New Data Frame")[0]
lyr1 = arcpy.mapping.Layer(r"C:\Project\Data\Parcels.lyr")
lyr2 = arcpy.mapping.Layer(r"C:\Project\Data\MapIndex.lyr")
lyr3 = arcpy.mapping.Layer(r"C:\Project\Data\Orthophoto.lyr")
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT", "Legend")[0]
legend.autoAdd = True
arcpy.mapping.AddLayer(df, lyr1, "BOTTOM")
arcpy.mapping.AddLayer(df, lyr2, "BOTTOM")
legend.autoAdd = False
arcpy.mapping.AddLayer(df, lyr3, "BOTTOM")
legend.adjustColumnCount(2)
mxd.save()
del mxd

7/11/2012