LegendViewModel

AMD: require(["esri/widgets/Legend/LegendViewModel"], (LegendVM) => { /* code goes here */ });
ESM: import LegendVM from "@arcgis/core/widgets/Legend/LegendViewModel";
Class: esri/widgets/Legend/LegendViewModel
Inheritance: LegendViewModel Accessor
Since: ArcGIS API for JavaScript 4.11

Provides the logic for the Legend widget, 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 Legend widgets.

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

Constructors

new LegendViewModel(properties)
Parameter
properties Object
optional

See the properties for a list of all the properties that may be passed into the constructor.

Property Overview

Any properties can be set, retrieved or listened to. See the Working with Properties topic.
Show inherited properties Hide inherited properties
Name Type Summary Class
Collection<ActiveLayerInfo>

Collection of ActiveLayerInfo objects used by the legend view to display data in the legend.

more details
LegendViewModel
Boolean

Indicates whether to show the Basemap layers in the Legend.

more details
LegendViewModel
String

The name of the class.

more details
Accessor
Boolean

When true, layers will only be shown in the legend if they are visible in the view's extent.

more details
LegendViewModel
Object[]

Specifies a subset of the layers in the map to display in the legend.

more details
LegendViewModel
Boolean

Determines whether to respect the properties of the layers in the map that control the legend's visibility (minScale, maxScale, legendEnabled).

more details
LegendViewModel
String

The view model's state.

more details
LegendViewModel
MapView|SceneView

The view from which the widget will operate.

more details
LegendViewModel

Property Details

Autocasts from Object[]

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 Boolean

Indicates whether to show the Basemap layers in the Legend.

Default Value:filterBasemaps
Example
legend.viewModel.basemapLegendVisible = true;
declaredClass Stringreadonly inherited

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

hideLayersNotInCurrentView Boolean
Since: ArcGIS API 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.

Default Value:false
See also
Example
// layers not displayed in the view
// will not be shown in the legend
legend.viewModel.hideLayersNotInCurrentView = true;
layerInfos Object[]

Specifies a subset of the layers in the map to display in the legend. If this property is not set, all operational layers in the map will display in the legend. This property can be used to control layer display order in the legend. Objects in this array are defined with the properties listed below.

Properties
title String
optional

Specify a title for the layer. If no title is specified the service name is used.

layer Layer
optional

A layer to add to the legend.

respectLayerVisibility Boolean
Since: ArcGIS API 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:

  • 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 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.

Default Value:true
See also
Example
// Always displays legend elements for the map's layers
// regardless of their minScale, maxScale, and legendEnabled properties
legend.respectLayerVisibility = false;
state Stringreadonly

The view model's state.

Possible Values:"ready"|"disabled"

Default Value:disabled

The view from which the widget will operate.

Method Overview

Show inherited methods Hide inherited methods
Name Return Type Summary Class

Adds one or more handles which are to be tied to the lifecycle of the object.

more details
Accessor
Boolean

Returns true if a named group of handles exist.

more details
Accessor

Removes a group of handles owned by the object.

more details
Accessor

Method Details

addHandles(handleOrHandles, groupKey)inherited
Since: ArcGIS API for JavaScript 4.25

Adds one or more handles which are to be tied to the lifecycle of the object. The handles will be removed when the object is destroyed.

// Manually manage handles
const handle = reactiveUtils.when(
  () => !view.updating,
  () => {
    wkidSelect.disabled = false;
  },
  { once: true }
);

// Handle gets removed when the object is destroyed.
this.addHandles(handle);
Parameters
handleOrHandles WatchHandle|WatchHandle[]

Handles marked for removal once the object is destroyed.

groupKey *
optional

Key identifying the group to which the handles should be added. All the handles in the group can later be removed with Accessor.removeHandles(). If no key is provided the handles are added to a default group.

hasHandles(groupKey){Boolean}inherited
Since: ArcGIS API for JavaScript 4.25

Returns true if a named group of handles exist.

Parameter
groupKey *
optional

A group key.

Returns
Type Description
Boolean Returns true if a named group of handles exist.
Example
// Remove a named group of handles if they exist.
if (obj.hasHandles("watch-view-updates")) {
  obj.removeHandles("watch-view-updates");
}
removeHandles(groupKey)inherited
Since: ArcGIS API for JavaScript 4.25

Removes a group of handles owned by the object.

Parameter
groupKey *
optional

A group key or an array or collection of group keys to remove.

Example
obj.removeHandles(); // removes handles from default group

obj.removeHandles("handle-group");
obj.removeHandles("other-handle-group");

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