Using ArcGIS Server JavaScript API v4.x and the Legend Widget, is it possible to limit the legend to only those features which are currently visible?

For example, in the Legend Widget Sample, only show the green High School classes when zoomed in to this extent:

enter image description here

1 Answer 1


No, the Legend widget doesn't work that way. It is based off the layer's renderer.

"Labels and their corresponding symbols depend on the values set in the Renderer of the layer."

-- From https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Legend.html

If you really wanted that behavior, I guess in theory you could update the renderer each time the map changes, but that doesn't seem like a great idea. A slightly better option might be to create your own legend-like display that looks at the currently visible features and updates dynamically as the map extent changes.

  • Hi Bjorn - does the new hideLayersNotInCurrentView option change this answer? When data from a layer is not visible in the view, the layer's legend information will be hidden. May 26, 2022 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.