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


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.

  • yeah, I was thinking along the lines of setting the target layer to legendEnabled: false and creating a copy of the layer based on a dynamic query using the current map extent, and putting that layer in the legend. Ideally I was hoping that someone had already written the code ;) – Stephen Lead Dec 19 '20 at 2:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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