Skip to content
ESM
import "@arcgis/map-components/components/arcgis-legend";
Inheritance:
ArcgisLegendPublicLitElement
Since
ArcGIS Maps SDK for JavaScript 4.28

The Legend component displays the symbols and labels used to represent layers in an arcgis-map or arcgis-scene. Only layers and sublayers visible in the view are shown, unless ignoreLayerVisibility is set to true.

The legend automatically updates when:

  • the visibility of a layer or sublayer changes
  • a layer is added or removed from the map
  • a layer's renderer, opacity, or title is changed
  • the legendEnabled property is changed (set to true or false on the layer)

Known Limitations

Various examples of legends based on renderer type.

Unique values

Unique values

Continuous color

Continuous color

Classed color

Classed color

Continuous size

Continuous size

Size - above and below

Size - above and below

Dot density

Dot density

Predominance

Predominance

Relationship

Relationship

Legend labels

Legend text is directly controlled through the layer and renderer properties. For example, layer.title is directly used by the legend to present a layer's symbology to the end user. To override this text, you can directly update layer.title.

Furthermore, you can control date and number formatting in Legend by using the FieldConfiguration formatting options on the layer. As an alternative to this, you can override Legend text via the renderer's properties. For example, the renderer may have a legendOptions property (or a label property in the case of visual variable stops or UniqueValueInfos) that can be used to override default legend text describing the renderer element in question. The Update legend text sample provides an example of overriding legend text via the renderer.

Note that when you override legend text, all formatting and localization that would otherwise be dynamically applied to labels will no longer apply.

See also

Demo

Properties

PropertyAttributeType
auto-destroy-disabled
basemap-legend-visible
card-style-layout
"auto" | "side-by-side" | "stack" | undefined
heading-level
hide-layers-not-in-current-view
icon
ignore-layer-visibility
label
legend-style
"card" | "classic" | { type: "card" | "classic"; layout?: "auto" | "side-by-side" | "stack"; }
loading
readonly reflected
loading
reference-element
respect-layer-definition-expression
state
readonly

activeLayerInfos

Property
Type
Collection<ActiveLayerInfo>

Collection of ActiveLayerInfo objects used by the legend view to display data in the legend. The legend component watches this property to hide or display the layer's legend when an ActiveLayerInfo is removed from or added to this collection.

autoDestroyDisabled

Property
Type
boolean

If true, the component will not be destroyed automatically when it is disconnected from the document. This is useful when you want to move the component to a different place on the page, or temporarily hide it. If this is set, make sure to call the destroy() method when you are done to prevent memory leaks.

Attribute
auto-destroy-disabled
Default value
false

basemapLegendVisible

Property
Type
boolean

Indicates whether to show the Basemap layers in the Legend. If you set this property to true and specify layerInfos that include the basemap layers, the basemap will be displayed in the legend.

Known limitation:

VectorTileLayers in the basemap are not currently supported in the legend. This includes most Esri basemaps such as "Streets", "Topographic", "Navigation", and others that use vector tiles.

Attribute
basemap-legend-visible
Default value
false
Example

Show basemap layers in the legend

const mapElement = document.querySelector("arcgis-map");
const basemapLayer = mapElement.map.basemap.baseLayers.getItemAt(0);
const legend = document.querySelector("arcgis-legend");
legend.layerInfos = [{ layer: basemapLayer }];
legend.basemapLegendVisible = true;

cardStyleLayout

Property
Type
"auto" | "side-by-side" | "stack" | undefined
Since
ArcGIS Maps SDK for JavaScript 4.34

Indicates the layout of the legend when the legendStyle is set to the string value of "card". The layout determines how the legend's content is arranged.

card-style-layout

Attribute
card-style-layout
Example

Renders the legend in the card style with a "stack" layout

<arcgis-legend legend-style="card" card-style-layout="stack"></arcgis-legend>
const legend = document.querySelector("arcgis-legend");
legend.legendStyle = "card";
legend.cardStyleLayout = "stack";

headingLevel

Property
Type
HeadingLevel

Indicates the heading level to use for the legend title. By default, legend titles are rendered as level 3 headings (e.g. <h3>Legend title</h3>). Depending on the legend placement in your app, you may need to adjust this heading for proper semantics. This is important for meeting accessibility standards.

See also
Attribute
heading-level
Default value
3
Example

Legend title will render as an <h2>

<arcgis-legend heading-level="2"></arcgis-legend>
document.querySelector("arcgis-legend").headingLevel = 2;

hideLayersNotInCurrentView

Property
Type
boolean

When true, layers will only be shown in the legend if they are visible in the view's extent. When data from a layer is not visible in the view, the layer's legend information will be hidden. Only layers that implement the createQuery() and queryFeatureCount() methods are supported by hideLayersNotInCurrentView.

To hide layers completely from the legend, you should set the legendEnabled property of the layer to false.

See also
Attribute
hide-layers-not-in-current-view
Default value
false
Example

Layers not displayed in the current map extent will not be shown in the legend

<arcgis-legend hide-layers-not-in-current-view></arcgis-legend>
document.querySelector("arcgis-legend").hideLayersNotInCurrentView = true;

icon

Property
Type
string | undefined

Icon which represents the component. Typically used when the component is controlled by another component (e.g. by the Expand component).

See also
Attribute
icon
Default value
"legend"

ignoreLayerVisibility

Property
Type
boolean
Since
ArcGIS Maps SDK for JavaScript 4.34

Determines whether to respect the properties of the layers in the map that control the legend's visibility (minScale, maxScale, legendEnabled). By default, a layer's legend elements will not render in the legend given the following conditions:

  • The layer's legendEnabled property is set to false.
  • If the view's scale is outside the visibility range defined by the layer's minScale and maxScale properties.

When the ignoreLayerVisibility property of the legend is set to true, the legend elements for each layer in the map will always display, thus disregarding the minScale, maxScale, and legendEnabled properties for each layer in the map.

See also
Attribute
ignore-layer-visibility
Default value
false
Example

Always displays legend elements for the map's layers regardless of their minScale, maxScale, and legendEnabled properties.

<arcgis-legend ignore-layer-visibility></arcgis-legend>
document.querySelector("arcgis-legend").ignoreLayerVisibility = true;

label

Property
Type
string | undefined

The component's default label.

Attribute
label
Default value
"Legend"

layerInfos

Property
Type
Array<LayerInfo>

Specifies a subset of the layers to display in the legend. This includes any basemap layers you want to be visible in the legend. If this property is not set, all layers in the map will display in the legend, including basemap layers if basemapLegendVisible is true. Objects in this array are defined with the properties in the example below.

Example

Only show a specific sublayer in the legend

const subtypeGroupLayer = webmap.layers.getItemAt(0);
const subtypeSubLayer = subtypeGroupLayer.sublayers.getItemAt(1);
const subtypeSublayerId = parseInt(subtypeSubLayer.subtypeCode);
document.querySelector("arcgis-legend").layerInfos = [
{
layer: subtypeGroupLayer,
sublayerIds: [subtypeSublayerId]
}
];
});

legendStyle

Property
Type
"card" | "classic" | { type: "card" | "classic"; layout?: "auto" | "side-by-side" | "stack"; }

Indicates the style of the legend. The style determines the legend's layout and behavior. You can either specify a string or an object to indicate the style. The known string values are the same values listed in the table within the type property.

legend-style

See also
Attribute
legend-style
Default value
"classic"
Example

Renders the legend in the card style with a stack layout

document.querySelector("arcgis-legend").legendStyle = {
type: "card",
layout: "stack"
};

loading

readonlyreflected Property
Type
boolean
Since
ArcGIS Maps SDK for JavaScript 4.34

Indicates whether the legend is currently loading.

Attribute
loading
Default value
false

messageOverrides

Property
Type
Record<string, unknown> | undefined
Since
ArcGIS Maps SDK for JavaScript 4.34

Replace localized message strings with your own strings.

Note: Individual message keys may change between releases.

referenceElement

Property
Type
ArcgisReferenceElement | string | undefined

By assigning the id attribute of the Map or Scene component to this property, you can position a child component anywhere in the DOM while still maintaining a connection to the Map or Scene.

See also
Attribute
reference-element

respectLayerDefinitionExpression

Property
Type
boolean
Since
ArcGIS Maps SDK for JavaScript 4.30

If a layer uses a unique value render, only features that satisfy the layer's definition will be displayed in the legend when set to true.

See also
Attribute
respect-layer-definition-expression
Default value
false
Example

Only display features that satisfy the layer's definitionExpression.

<arcgis-legend respect-layer-definition-expression></arcgis-legend>
document.querySelector("arcgis-legend").respectLayerDefinitionExpression = true;

state

readonly Property
Type
LegendViewModelState

The current state of the component.

Default value
"disabled"

view

Property
Type
LinkChartView | MapViewOrSceneView | null | undefined

The view associated with the component.

Note: The recommended approach is to fully migrate applications to use map and scene components and avoid using MapView and SceneView directly. However, if you are migrating a large application from widgets to components, you might prefer a more gradual transition. To support this use case, the SDK includes this view property which connects a component to a MapView or SceneView. Ultimately, once migration is complete, the arcgis-legend component will be associated with a map or scene component rather than using the view property.

Methods

MethodSignature
componentOnReady
inherited
componentOnReady(): Promise<this>
destroy(): Promise<void>

componentOnReady

inherited Method
Signature
componentOnReady (): Promise<this>
Inherited from: PublicLitElement

Creates a promise that resolves once the component is fully loaded.

Returns
Promise<this>
Example
const arcgisLegend = document.querySelector("arcgis-legend");
document.body.append(arcgisLegend);
await arcgisLegend.componentOnReady();
console.log("arcgis-legend is ready to go!");

destroy

Method
Signature
destroy (): Promise<void>

Permanently destroy the component.

Returns
Promise<void>

Events

arcgisPropertyChange

Event
arcgisPropertyChange: CustomEvent<{ name: "state"; }>

Emitted when the value of a property is changed. Use this to listen to changes to properties.

bubbles composed cancelable

arcgisReady

Event
arcgisReady: CustomEvent<void>

Emitted when the component associated with a map or scene view is ready to be interacted with.

bubbles composed cancelable