LegendElement

摘要

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.

讨论

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.

属性

属性说明数据类型
autoAdd
(可读写)

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
(可读写)

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

Double
elementPositionX
(可读写)

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

Double
elementPositionY
(可读写)

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

Double
elementWidth
(可读写)

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

Double
items
(只读)

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

String
name
(可读写)

The name of the element.

String
parentDataFrameName
(只读)

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

String
title
(可读写)

The text string that represents the legend's title.

String
type
(只读)

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

方法概述

方法说明
adjustColumnCount (column_count)

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

方法

adjustColumnCount (column_count)
参数说明数据类型
column_count

An integer that represents the desired number of columns.

(默认值为 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.

代码示例

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/10/2012