Legend

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

The Legend widget displays labels and symbols for layers in a map. Labels and their corresponding symbols depend on the values set in the Renderer of the layer. The legend will only display layers and sublayers that are visible in the view.

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)

widgets-legend-basic

You can use the view's DefaultUI to add widgets to the view's user interface via the ui property on the view.

Known Limitations

For information about gaining full control of widget styles, see the Styling topic.
See also:
Example:
let legend = new Legend({
  view: view,
  layerInfos: [{
    layer: featureLayer,
    title: "Legend"
  }]
});

view.ui.add(legend, "bottom-right");

Constructors

new Legend(properties)
Parameter:
properties Object
optional

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

Example:
// typical usage
let legend = new Legend({
  view: view
});

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>more details

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

more detailsLegend
Booleanmore details

Indicates whether to show the Basemap layers in the Legend.

more detailsLegend
String|HTMLElementmore details

The ID or node representing the DOM element containing the widget.

more detailsWidget
Stringmore details

The name of the class.

more detailsAccessor
Numbermore details

Indicates the heading level to use for the legend title.

more detailsLegend
Stringmore details

The widget's default CSS icon class.

more detailsLegend
Stringmore details

The unique ID assigned to the widget when the widget is created.

more detailsWidget
Stringmore details

The widget's default label.

more detailsLegend
Object[]more details

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

more detailsLegend
Booleanmore details

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

more detailsLegend
Object|Stringmore details

Indicates the style of the legend.

more detailsLegend
MapView|SceneViewmore details

A reference to the MapView or SceneView.

more detailsLegend
LegendViewModelmore details

The view model for this widget.

more detailsLegend
Booleanmore details

Indicates whether the widget is visible.

more detailsWidget

Property Details

Autocasts from Object[]

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

basemapLegendVisible Boolean

Indicates whether to show the Basemap layers in the Legend. If you set this property to true and specify layerInfos, the basemap layers you want included in the legend must also be specified in layerInfos.

Default Value:false
Example:
legend.basemapLegendVisible = true;

The ID or node representing the DOM element containing the widget. This property can only be set once. The following examples are all valid use cases when working with widgets.

Examples:
// Create the HTML div element programmatically at runtime and set to the widget's container
const basemapGallery = new BasemapGallery({
  view: view,
  container: document.createElement("div")
});

// Add the widget to the top-right corner of the view
view.ui.add(basemapGallery, {
  position: "top-right"
});
// Specify an already-defined HTML div element in the widget's container

const basemapGallery = new BasemapGallery({
  view: view,
  container: basemapGalleryDiv
});

// Add the widget to the top-right corner of the view
view.ui.add(basemapGallery, {
  position: "top-right"
});

// HTML markup
<body>
  <div id="viewDiv"></div>
  <div id="basemapGalleryDiv"></div>
</body>
// Specify the widget while adding to the view's UI
const basemapGallery = new BasemapGallery({
  view: view
});

// Add the widget to the top-right corner of the view
view.ui.add(basemapGallery, {
  position: "top-right"
});
declaredClass Stringreadonly inherited
Since: ArcGIS API for JavaScript 4.7

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

headingLevel Number
Since: ArcGIS API for JavaScript 4.20

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.

Default Value:3
See also:
Example:
// legend title will render as an <h2>
legend.headingLevel = 2;
iconClass String
Since: ArcGIS API for JavaScript 4.7

The widget's default CSS icon class.

The unique ID assigned to the widget when the widget is created. If not set by the developer, it will default to the container ID, or if that is not present then it will be automatically generated.

label String
Since: ArcGIS API for JavaScript 4.7

The widget's default label.

layerInfos Object[]

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 listed below.

Properties:
title String
optional

Specifies a title for the layer to display above its symbols and descriptions. If no title is specified the service name is used.

layer Layer

A layer to display in 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
Example:
// Always displays legend elements for the map's layers
// regardless of their minScale, maxScale, and legendEnabled properties
legend.respectLayerVisibility = false;
style Object|String
Since: ArcGIS API for JavaScript 4.7

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.

Properties:
type String

Specifies the style of the legend. There are two possible values listed in the table below:

Value Description
classic The legend has a portrait orientation. The user can scroll vertically when many elements are included in the legend's content.
card In wide views, the legend has a landscape orientation that allows users to scroll horizontally to view all legend elements. This style can be responsive, making it ideal for mobile web apps. In smaller views, the legend collapses to occupy less space. One element is shown at a time in a card-style layout, which the user can navigate horizontally.

Possible Values:"classic"|"card"

layout String
optional
Default Value:stack

When a card type is specified, you can specify one of the following layout options.

Value Description
auto This layout is responsive so that in wide views the legend has a side-by-side layout, and a stack layout in smaller (mobile) views.
side-by-side The legend has a landscape orientation that allows users to scroll horizontally to view multiple legend cards at a time.
stack The legend cards are stacked, which conserves space, but restricts the user to seeing only one card at a time.

Possible Values:"auto"|"side-by-side"|"stack"

Default Value:classic
Examples:
// renders the legend in the card style with a "stack" layout
legend.style = "card";
// renders the legend in the card style with a responsive
// layout that toggles between "stack" and "side-by-side"
legend.style = {
  type: "card",
  layout: "auto"
};
// renders the legend in the classic layout
legend.style = "classic";

A reference to the MapView or SceneView. Set this to link the widget to a specific view.

The view model for this widget. This is a class that contains all the logic (properties and methods) that controls this widget's behavior. See the LegendViewModel class to access all properties and methods on the widget.

visible Boolean inherited

Indicates whether the widget is visible.

If false, the widget will no longer be rendered in the web document. This may affect the layout of other elements or widgets in the document. For example, if this widget is the first of three widgets associated to the upper right hand corner of the view UI, then the other widgets will reposition when this widget is made invisible. For more information, refer to the CSS display value of "none".

Default Value:true
Example:
// Hides the widget in the view
widget.visible = false;

Method Overview

Show inherited methods Hide inherited methods
Name Return Type Summary Class
Stringmore details

A utility method used for building the value for a widget's class property.

more detailsWidget
more details

Destroys the widget instance.

more detailsWidget
Booleanmore details

Emits an event on the instance.

more detailsWidget
Booleanmore details

Indicates whether there is an event listener on the instance that matches the provided event name.

more detailsWidget
Booleanmore details

isFulfilled() may be used to verify if creating an instance of the class is fulfilled (either resolved or rejected).

more detailsWidget
Booleanmore details

isRejected() may be used to verify if creating an instance of the class is rejected.

more detailsWidget
Booleanmore details

isResolved() may be used to verify if creating an instance of the class is resolved.

more detailsWidget
Objectmore details

Registers an event handler on the instance.

more detailsWidget
more details

Widget teardown helper.

more detailsWidget
more details

This method is primarily used by developers when implementing custom widgets.

more detailsWidget
Objectmore details

This method is primarily used by developers when implementing custom widgets.

more detailsWidget
more details

Renders widget to the DOM immediately.

more detailsWidget
more details

This method is primarily used by developers when implementing custom widgets.

more detailsWidget
Promisemore details

when() may be leveraged once an instance of the class is created.

more detailsWidget

Method Details

classes(classNames){String}inherited
Since: ArcGIS API for JavaScript 4.7

A utility method used for building the value for a widget's class property. This aids in simplifying CSS class setup.

Parameter:
classNames String|String[]|Object
repeatable

The class names.

Returns:
Type Description
String The computed class name.
See also:
Example:
// .tsx syntax showing how to set CSS classes while rendering the widget

render() {
  const dynamicIconClasses = {
    [CSS.myIcon]: this.showIcon,
    [CSS.greyIcon]: !this.showIcon
  };

  return (
    <div class={classes(CSS.root, CSS.mixin, dynamicIconClasses)} />
  );
}
destroy()inherited

Destroys the widget instance.

emit(type, event){Boolean}inherited
Since: ArcGIS API for JavaScript 4.5

Emits an event on the instance. This method should only be used when creating subclasses of this class.

Parameters:
type String

The name of the event.

event Object
optional

The event payload.

Returns:
Type Description
Boolean true if a listener was notified
hasEventListener(type){Boolean}inherited

Indicates whether there is an event listener on the instance that matches the provided event name.

Parameter:
type String

The name of the event.

Returns:
Type Description
Boolean Returns true if the class supports the input event.
isFulfilled(){Boolean}inherited
Since: ArcGIS API for JavaScript 4.19

isFulfilled() may be used to verify if creating an instance of the class is fulfilled (either resolved or rejected). If it is fulfilled, true will be returned.

Returns:
Type Description
Boolean Indicates whether creating an instance of the class has been fulfilled (either resolved or rejected).
isRejected(){Boolean}inherited
Since: ArcGIS API for JavaScript 4.19

isRejected() may be used to verify if creating an instance of the class is rejected. If it is rejected, true will be returned.

Returns:
Type Description
Boolean Indicates whether creating an instance of the class has been rejected.
isResolved(){Boolean}inherited
Since: ArcGIS API for JavaScript 4.19

isResolved() may be used to verify if creating an instance of the class is resolved. If it is resolved, true will be returned.

Returns:
Type Description
Boolean Indicates whether creating an instance of the class has been resolved.
on(type, listener){Object}inherited

Registers an event handler on the instance. Call this method to hook an event with a listener. See the Events summary table for a list of supported events.

Parameters:

An event or an array of events to listen for.

listener Function

The function to call when the event fires.

Returns:
Type Description
Object Returns an event handler with a remove() method that should be called to stop listening for the event(s).
Property Type Description
remove Function When called, removes the listener from the event.
Example:
view.on("click", function(event){
  // event is the event handle returned after the event fires.
  console.log(event.mapPoint);
});
own(handles)inherited

Widget teardown helper. Any handles added to it will be automatically removed when the widget is destroyed.

Parameter:

Handles marked for removal once the widget is destroyed.

postInitialize()inherited

This method is primarily used by developers when implementing custom widgets. Executes after widget is ready for rendering.

render(){Object}inherited

This method is primarily used by developers when implementing custom widgets. It must be implemented by subclasses for rendering.

Returns:
Type Description
Object The rendered virtual node.
renderNow()inherited

Renders widget to the DOM immediately.

scheduleRender()inherited

This method is primarily used by developers when implementing custom widgets. Schedules widget rendering. This method is useful for changes affecting the UI.

when(callback, errback){Promise}inherited
Since: ArcGIS API for JavaScript 4.19

when() may be leveraged once an instance of the class is created. This method takes two input parameters: a callback function and an errback function. The callback executes when the instance of the class loads. The errback executes if the instance of the class fails to load.

Parameters:
callback Function
optional

The function to call when the promise resolves.

errback Function
optional

The function to execute when the promise fails.

Returns:
Type Description
Promise Returns a new promise for the result of callback that may be used to chain additional functions.
Example:
// Although this example uses the BasemapGallery widget, any class instance that is a promise may use when() in the same way
let bmGallery = new BasemapGallery();
bmGallery.when(function(){
  // This function will execute once the promise is resolved
}, function(error){
  // This function will execute if the promise is rejected due to an error
});

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