LegendElement

Zusammenfassung

The LegendElement object provides access to properties that enable its positioning and resizing on the page layout as well as modifying its title.

Auswertung

A LegendElement is very similar to MapSurroundElement. It has an association with a MapFrame using a property called mapFrame but it also has a property called title.

The listElements method on the Layout object returns a Python list of page layout element objects. To return a list of MapsurroundElements only, use the MAPSURROUND_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 important that each page layout element be given a unique name so that it can be easily isolated using ArcPy scripting.

By default, layers added to a Map automatically get added to a legend. This makes it possible to author a Layout with an empty map and legend but have those items dynamically update when new layers are added to the map.

The elementPositionX and elementPositionY values are based on the element's anchor position, which is set on the Format tab. If you prefer that a LegendElement grows downward when layers are added, it is best to set the anchor position to be upper left.

Eigenschaften

EigenschaftErklärungDatentyp
elementHeight
(Lesen und schreiben)

The height of the element in page units.

Double
elementPositionX
(Lesen und schreiben)

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

Double
elementPositionY
(Lesen und schreiben)

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

Double
elementRotation
(Lesen und schreiben)

Rotates the element relative to the anchor position. Positive values are counter-clockwise and negative values are clockwise.

Double
elementWidth
(Lesen und schreiben)

The width of the element in page units.

Double
mapFrame
(Lesen und schreiben)

A reference to the associated MapFrame.

MapFrame
name
(Lesen und schreiben)

The name of the element. It is important that all elements have a unique name so they can be easily referenced.

String
showTitle
(Lesen und schreiben)

A boolean that controls if the title is displayed in the legend.

Boolean
syncLayerOrder
(Lesen und schreiben)

A boolean that controls if the items in the legend are in the same order as the layers in the map.

Boolean
syncLayerVisibility
(Lesen und schreiben)

A boolean that controls if an item should automatically appear in the legend if it is visible in the map.

Boolean
syncNewLayer
(Lesen und schreiben)

A boolean that controls if an item should automatically be added to the legend when added to the map.

Boolean
syncReferenceScale
(Lesen und schreiben)

A boolean that controls if the symbols in the legend should match the scaling of the symbols in a map if a reference scale is set.

Boolean
title
(Lesen und schreiben)

The text string that represents the legend's title.

String
type
(Nur lesen)

Returns a value of LEGEND_ELEMENT.

String
visible
(Lesen und schreiben)

Returns True if the element is visible on the layout. Rather than having to move unwanted objects off the page before printing or exporting, you can toggle the element's visibility.

Boolean

Codebeispiel

LegendElement example

The following script will add layers to a map that includes an inserted legend element named Legend. The layers will automatically get added to the legend and its title gets updated.

import arcpy
aprx = arcpy.mp.ArcGISProject(r"C:\Projects\YosemiteNP\Yosemite.aprx")
lyr1 = arcpy.mp.LayerFile(r"C:\Projects\Yosemite\LYRs\Trails.lyrx")
lyr2 = arcpy.mp.LayerFile(r"C:\Projects\Yosemite\LYRs\PointsOfInterest.lyrx")
m = aprx.listMaps("Yose*")[0]
m.addLayer(lyr1, "TOP")
m.addLayer(lyr2, "TOP")
lyt = aprx.listLayouts("Main Attrations")[0]
legend = lyt.listElements("LEGEND_ELEMENT", "Legend")[0]
legend.title = "Yosemite National Park"
aprx.save()
del aprx