Skip to content

Legend

ESM:
Use dark colors for code blocksCopy
1
import "@arcgis/map-components/components/arcgis-legend";
CDN:
No specific import is needed for this component.
Since:ArcGIS Maps SDK for JavaScript 4.28

The Legend component displays the symbols and labels used to represent layers in a 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
activeLayerInfosCollection<ActiveLayerInfo>
autoDestroyDisabledauto-destroy-disabledboolean
basemapLegendVisiblebasemap-legend-visibleboolean
cardStyleLayoutcard-style-layout"auto" | "side-by-side" | "stack"
headingLevelheading-level1 | 2 | 3 | 4 | 5 | 6
hideLayersNotInCurrentViewhide-layers-not-in-current-viewboolean
iconiconstring
ignoreLayerVisibilityignore-layer-visibilityboolean
labellabelstring
layerInfosArray<LegendViewModelLayerInfo>
legendStylelegend-style"card" | "classic" | { type: "card" | "classic"; layout?: "auto" | "side-by-side" | "stack" | undefined; }
loading
readonlyreflected
loadingboolean
messageOverridesRecord<string, unknown>
position
deprecated
position"bottom-leading" | "bottom-left" | "bottom-right" | "bottom-trailing" | "manual" | "top-leading" | "top-left" | "top-right" | "top-trailing"
referenceElementreference-elementHTMLArcgisLinkChartElement | HTMLArcgisMapElement | HTMLArcgisSceneElement | string
respectLayerDefinitionExpressionrespect-layer-definition-expressionboolean
respect-layer-visibility-disabledboolean
state
readonlyreflected
state"disabled" | "ready"
viewMapView | SceneView

activeLayerInfos

Property
activeLayerInfos: 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
autoDestroyDisabled: 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
basemapLegendVisible: 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.

Example

Show basemap layers in the legend

Use dark colors for code blocksCopy
1
2
3
4
5
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;
Attribute
basemap-legend-visible
Default value
false

cardStyleLayout

Property
cardStyleLayout: "auto" | "side-by-side" | "stack"
Since:ArcGIS Maps SDK for JavaScript 4.34Legend since 4.28, cardStyleLayout added at 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

Example

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

Use dark colors for code blocksCopy
1
<arcgis-legend legend-style="card" card-style-layout="stack"></arcgis-legend>
Use dark colors for code blocksCopy
1
2
3
const legend = document.querySelector("arcgis-legend");
legend.legendStyle = "card";
legend.cardStyleLayout = "stack";
Attribute
card-style-layout

headingLevel

Property
headingLevel: 1 | 2 | 3 | 4 | 5 | 6

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
Example

Legend title will render as an <h2>

Use dark colors for code blocksCopy
1
<arcgis-legend heading-level="2"></arcgis-legend>
Use dark colors for code blocksCopy
1
document.querySelector("arcgis-legend").headingLevel = 2;
Attribute
heading-level
Default value
3

hideLayersNotInCurrentView

Property
hideLayersNotInCurrentView: 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
Example

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

Use dark colors for code blocksCopy
1
<arcgis-legend hide-layers-not-in-current-view></arcgis-legend>
Use dark colors for code blocksCopy
1
document.querySelector("arcgis-legend").hideLayersNotInCurrentView = true;
Attribute
hide-layers-not-in-current-view
Default value
false

icon

Property
icon: string

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
ignoreLayerVisibility: boolean
Since:ArcGIS Maps SDK for JavaScript 4.34Legend since 4.28, ignoreLayerVisibility added at 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
Example

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

Use dark colors for code blocksCopy
1
<arcgis-legend ignore-layer-visibility></arcgis-legend>
Use dark colors for code blocksCopy
1
document.querySelector("arcgis-legend").ignoreLayerVisibility = true;
Attribute
ignore-layer-visibility
Default value
false

label

Property
label: string

The component's default label.

Attribute
label
Default value
"Legend"

layerInfos

Property
layerInfos: Array<LegendViewModelLayerInfo>

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

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
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
legendStyle: "card" | "classic" | { type: "card" | "classic"; layout?: "auto" | "side-by-side" | "stack" | undefined; }

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
Example

Renders the legend in the card style with a stack layout

Use dark colors for code blocksCopy
1
2
3
4
document.querySelector("arcgis-legend").legendStyle = {
  type: "card",
  layout: "stack"
};
Attribute
legend-style
Default value
"classic"

loading

readonlyreflected
Property
loading: boolean
Since:ArcGIS Maps SDK for JavaScript 4.34Legend since 4.28, loading added at 4.34.

Indicates whether the legend is currently loading.

Attribute
loading
Default value
false

messageOverrides

Property
messageOverrides: Record<string, unknown>
Since:ArcGIS Maps SDK for JavaScript 4.34Legend since 4.28, messageOverrides added at 4.34.

Replace localized message strings with your own strings.

Note: Individual message keys may change between releases.

position

deprecatedProperty
position: "bottom-leading" | "bottom-left" | "bottom-right" | "bottom-trailing" | "manual" | "top-leading" | "top-left" | "top-right" | "top-trailing"
Deprecatedsince 4.34, use slot instead.
Attribute
position

referenceElement

Property

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
respectLayerDefinitionExpression: boolean
Since:ArcGIS Maps SDK for JavaScript 4.30Legend since 4.28, respectLayerDefinitionExpression added at 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
Example

Only display features that satisfy the layer's definitionExpression.

Use dark colors for code blocksCopy
1
<arcgis-legend respect-layer-definition-expression></arcgis-legend>
Use dark colors for code blocksCopy
1
document.querySelector("arcgis-legend").respectLayerDefinitionExpression = true;
Attribute
respect-layer-definition-expression
Default value
false

respectLayerVisibilityDisabled

deprecatedProperty
respectLayerVisibilityDisabled: boolean
Deprecatedsince 4.34 use ignoreLayerVisibility instead.

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 respectLayerVisibilityDisabled property of the legend is set to false, 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
Example

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

Use dark colors for code blocksCopy
1
<arcgis-legend respect-layer-visibility-disabled="false"></arcgis-legend>
Use dark colors for code blocksCopy
1
document.querySelector("arcgis-legend").respectLayerVisibilityDisabled = true;
Attribute
respect-layer-visibility-disabled
Default value
false

state

readonlyreflected
Property
state: "disabled" | "ready"

The current state of the component.

Attribute
state
Default value
"disabled"

view

Property

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 Legend component will be associated with a map or scene component rather than using the view property.

Methods

MethodSignature
componentOnReadycomponentOnReady(): Promise<void>
destroydestroy(): Promise<void>

componentOnReady

Method
componentOnReady(): Promise<void>

Create a promise that resolves once component is fully loaded.

Example
Use dark colors for code blocksCopy
1
2
3
4
const arcgisLegend = document.querySelector("arcgis-legend");
document.body.append(arcgisLegend);
await arcgisLegend.componentOnReady();
console.log("arcgis-legend is ready to go!");
Returns
Promise<void>

destroy

Method
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

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.