import LegendVM from "@arcgis/core/widgets/Legend/LegendViewModel.js";
            const LegendVM = await $arcgis.import("@arcgis/core/widgets/Legend/LegendViewModel.js");
            @arcgis/core/widgets/Legend/LegendViewModel
        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
 - 
              
Legend widget - Deprecated since 4.34. Use the Legend component instead.
 
const legend = new Legend({
  view: view,
  viewModel: new LegendViewModel({
    view: view
  })
});
view.ui.add(legend, "bottom-left");
  
Constructors
- 
  
  
  
  
  
  
  
  
    Parameterproperties Objectoptional
See the properties for a list of all the properties that may be passed into the constructor.
 
Property Overview
| Name | Type | Summary | Class | 
|---|---|---|---|
Collection of ActiveLayerInfo objects used by the legend view to display data in the legend.  | LegendViewModel | ||
Indicates whether to show the Basemap layers in the Legend.  | LegendViewModel | ||
The name of the class.  | Accessor | ||
When   | LegendViewModel | ||
Defines which layers and sublayers are shown in the legend, including any basemap layers you want visible.  | LegendViewModel | ||
Indicates whether the viewModel is currently loading.  | LegendViewModel | ||
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.  | LegendViewModel | ||
Determines whether to respect the properties of the layers in the map that control the legend's visibility (  | LegendViewModel | ||
The view model's state.  | LegendViewModel | ||
The view from which the widget will operate.  | LegendViewModel | 
Property Details
- 
  
  
activeLayerInfos
PropertyactiveLayerInfos Collection<ActiveLayerInfo>autocast - 
  
    
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.
 
- 
  
  
hideLayersNotInCurrentView
PropertyhideLayersNotInCurrentView BooleanSince: ArcGIS Maps SDK for JavaScript 4.21LegendViewModel since 4.11, hideLayersNotInCurrentView added at 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
legendEnabledproperty of the layer tofalse.- Default Value:false
 - See also
 
Example// layers not displayed in the view // will not be shown in the legend legend.viewModel.hideLayersNotInCurrentView = true; 
- 
  
    
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
Propertyloading BooleanreadonlySince: ArcGIS Maps SDK for JavaScript 4.34LegendViewModel since 4.11, loading added at 4.34.  - 
  
    
Indicates whether the viewModel is currently loading.
- Default Value:false
 
 
- 
  
  
respectLayerDefinitionExpression
PropertyrespectLayerDefinitionExpression BooleanSince: ArcGIS Maps SDK for JavaScript 4.34LegendViewModel since 4.11, respectLayerDefinitionExpression added at 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.
- Default Value:false
 - See also
 
 
- 
  
  
respectLayerVisibility
PropertyrespectLayerVisibility BooleanSince: ArcGIS Maps SDK for JavaScript 4.13LegendViewModel since 4.11, respectLayerVisibility added at 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
respectLayerVisibilityproperty of the legend is set tofalse, the legend elements for each layer in the map will always display, thus disregarding theminScale,maxScale, andlegendEnabledproperties 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; - The layer's legendEnabled property
is set to 
 
- 
  
  
state
Propertystate Stringreadonly - 
  
    
The view model's state.
Possible Values:"ready" |"disabled"
- Default Value:"disabled"
 
 
Method Overview
| Name | Return Type | Summary | Class | 
|---|---|---|---|
Adds one or more handles which are to be tied to the lifecycle of the object.  | Accessor | ||
Returns true if a named group of handles exist.  | Accessor | ||
Removes a group of handles owned by the object.  | Accessor | 
Method Details
- 
  
  
  
  Inherited from Accessor
Since: ArcGIS Maps SDK for JavaScript 4.25Accessor since 4.0, addHandles added at 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 } ); this.addHandles(handle); // Destroy the object this.destroy();ParametershandleOrHandles WatchHandle|WatchHandle[]Handles marked for removal once the object is destroyed.
groupKey *optionalKey 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
InheritedMethodhasHandles(groupKey){Boolean}Inherited from AccessorSince: ArcGIS Maps SDK for JavaScript 4.25Accessor since 4.0, hasHandles added at 4.25.  - 
  
  
    
Returns true if a named group of handles exist.
ParametergroupKey *optionalA group key.
ReturnsType Description Boolean Returns trueif 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"); } 
- 
  
  
  
  Inherited from Accessor
Since: ArcGIS Maps SDK for JavaScript 4.25Accessor since 4.0, removeHandles added at 4.25.  - 
  
  
    
Removes a group of handles owned by the object.
ParametergroupKey *optionalA group key or an array or collection of group keys to remove.
Exampleobj.removeHandles(); // removes handles from default group obj.removeHandles("handle-group"); obj.removeHandles("other-handle-group"); 
Type Definitions
- 
  
    
An object that defines a layer, optional sublayer IDs, and a custom title for legend display. Use this to specify which layers and sublayers appear in the legend and how they are labeled with layerInfos.
- Properties
 - 
  
A layer to display in the legend.
optional The IDs of the sublayers for which to display legend information. Only applicable if the
layeris a MapImageLayer, SubtypeGroupLayer or WMSLayer.optionaltitle StringSpecifies a title for the layer to display above its symbols and descriptions. If no title is specified the service name is used.