Skip to content
import LegendViewModel from "@arcgis/core/widgets/Legend/LegendViewModel.js";
Inheritance:
LegendViewModelAccessor
Since
ArcGIS Maps SDK for JavaScript 4.11

Provides the logic for the Legend widget and component, which displays a label and symbol for interpreting the Renderer of each layer in a map. This class may be used to create custom, interactive Legends.

See also
Example
const legend = new Legend({
view: view,
viewModel: new LegendViewModel({
view: view
})
});
view.ui.add(legend, "bottom-left");

Constructors

Constructor

Constructor
Parameters
ParameterTypeDescriptionRequired
properties
See the properties table for a list of all the properties that may be passed into the constructor.

Properties

Any properties can be set, retrieved or listened to. See the Watch for changes topic.

activeLayerInfos

autocast Property
Type
Collection<ActiveLayerInfo>

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

basemapLegendVisible

Property
Type
boolean

Indicates whether to show the Basemap layers in the Legend.

Default value
false
Example
legend.viewModel.basemapLegendVisible = true;

declaredClass

readonlyinherited Property
Type
string
Inherited from: Accessor

The name of the class. The declared class name is formatted as esri.folder.className.

hideLayersNotInCurrentView

Property
Type
boolean
Since
ArcGIS Maps SDK for JavaScript 4.21

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.

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

See also
Default value
false
Example
// layers not displayed in the view
// will not be shown in the legend
legend.viewModel.hideLayersNotInCurrentView = true;

layerInfos

Property
Type
LayerInfo[]

Defines which layers and sublayers are shown in the legend, including any basemap layers you want visible. If not set, all layers in the map are displayed in the legend by default, including basemap layers when basemapLegendVisible is true.

loading

readonly Property
Type
boolean
Since
ArcGIS Maps SDK for JavaScript 4.34

Indicates whether the viewModel is currently loading.

Default value
false

respectLayerDefinitionExpression

Property
Type
boolean
Since
ArcGIS Maps SDK for JavaScript 4.34

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

See also
Default value
false

respectLayerVisibility

Property
Type
boolean
Since
ArcGIS Maps SDK for JavaScript 4.13

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:

When the respectLayerVisibility 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
Default value
true
Example
// Always displays legend elements for the map's layers
// regardless of their minScale, maxScale, and legendEnabled properties
legend.respectLayerVisibility = false;

state

readonly Property
Type
LegendViewModelState

The view model's state.

Default value
"disabled"

view

Property
Type
MapViewOrSceneView | LinkChartView | null | undefined

The view from which the widget will operate.

Type definitions

LegendViewModelState

Type definition
Since
ArcGIS Maps SDK for JavaScript 5.0

Represents the allowed values for the state property.

See also
Type
"ready" | "disabled"