<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Is it possible to configure a legend to only include features which are visible? in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/is-it-possible-to-configure-a-legend-to-only/m-p/1376537#M83504</link>
    <description>&lt;P&gt;Hello, many thanks for this.&lt;/P&gt;&lt;P&gt;I had a go with configuring it so that the featurelayer is queried on refresh and the geometry the user draws is used to query it, but i'm definitely sure i've gone wrong somewhere.&lt;/P&gt;&lt;P&gt;&amp;nbsp;Could you provide some critiques if possible.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;        //Loop through webmap layers to retrieve features
        map.layers.forEach((FeatureLayer) =&amp;gt; { //For each layer in the webmap 
          FeatureLayer.on("refresh", function(event){ //On refresh
              if (event.dataChanged){ //If a change has happened
               const queryParams = FeatureLayer.createQuery(); //Create a feature query
               queryParams.geometry = sketchGeometry; //Set the feature query geometry as the one drawn by the user
               queryParams.returnGeometry = true;
                FeatureLayer.queryFeatures(queryParams).then(function(results){ //returns all the graphics from the layer view
                  map.layers.add(results.features);    }) }})});
                               
        view.when(() =&amp;gt; {//At the view
         var container = document.createElement("DIV"); //Create a container for the layer's legend
          container.style.backgroundColor = "#FFFFFF"; //Set container's background colour
          view.ui.add(container, "bottom-left"); // Add widget to the bottom right corner of the view
         addLegendForLayer(Legend, view, container, map.layers.getItemAt(0));//Run the addLegendForLayer() custom function to add the legend into the container             
        });&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 31 Jan 2024 16:42:35 GMT</pubDate>
    <dc:creator>EmilyGoldsmith</dc:creator>
    <dc:date>2024-01-31T16:42:35Z</dc:date>
    <item>
      <title>Is it possible to configure a legend to only include features which are visible?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/is-it-possible-to-configure-a-legend-to-only/m-p/1375881#M83487</link>
      <description>&lt;P&gt;Hello.&lt;/P&gt;&lt;P&gt;In my spatial query app, i've configured the legend to dynamically show only those layers which are present within a mapview. However, I was wondering if it was possible to go one step further and show only those features which have been filtered to the view?&lt;/P&gt;&lt;P&gt;For example, even though this point has no features intersecting with it (and thus filtered), a layer shows up in the legend due to being present in the mapview:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="EmilyGoldsmith_0-1706612202493.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/93169iF2816E3E58D777E8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="EmilyGoldsmith_0-1706612202493.png" alt="EmilyGoldsmith_0-1706612202493.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="EmilyGoldsmith_1-1706612262715.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/93170i30382668C27BD53A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="EmilyGoldsmith_1-1706612262715.png" alt="EmilyGoldsmith_1-1706612262715.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;My legend is configured like this:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;        const legend = new Legend({
            view: view
          });  
        legend.hideLayersNotInCurrentView = true; 

        //Add expandable functionality to the legend
        legendExpand = new Expand({
          expandIcon: "layers",  
          view: view,
          content: legend,
          expanded: true
});&lt;/LI-CODE&gt;&lt;P&gt;Thank you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jan 2024 12:31:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/is-it-possible-to-configure-a-legend-to-only/m-p/1375881#M83487</guid>
      <dc:creator>EmilyGoldsmith</dc:creator>
      <dc:date>2024-01-30T12:31:22Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to configure a legend to only include features which are visible?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/is-it-possible-to-configure-a-legend-to-only/m-p/1376011#M83490</link>
      <description>&lt;P&gt;Although this is not possible with the out of the box &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Legend.html" target="_self"&gt;Legend&lt;/A&gt; widget, it is still possible for you to accomplish this behavior in your application, assuming the layers you're working with are client-side layers (e.g. &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html" target="_self"&gt;FeatureLayers&lt;/A&gt;).&amp;nbsp; &lt;A href="https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/order-layers-in-map-and-independently-in-legend/m-p/1287108/highlight/true#M81096" target="_self"&gt;This post&lt;/A&gt;&amp;nbsp;shows how to use multiple Legend widgets, although having the appearance of being a single legend to the end user.&amp;nbsp; If you're using FeatureLayers, you could add one Legend per layer.&amp;nbsp; For each layer, you would get a reference to its associated &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-views-layers-FeatureLayerView.html" target="_self"&gt;FeatureLayerView&lt;/A&gt;, and then whenever it &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html#event-refresh" target="_self"&gt;refreshes&lt;/A&gt; it, you would &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-views-layers-FeatureLayerView.html#queryFeatureCount" target="_self"&gt;query&lt;/A&gt; the FeatureLayerView for its features.&amp;nbsp; This query runs client-side, so is instantaneous.&amp;nbsp; If it returns no features, you would hide the associated Legend instance, or if there are features returned, you would show it instead.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jan 2024 17:39:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/is-it-possible-to-configure-a-legend-to-only/m-p/1376011#M83490</guid>
      <dc:creator>JoelBennett</dc:creator>
      <dc:date>2024-01-30T17:39:23Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to configure a legend to only include features which are visible?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/is-it-possible-to-configure-a-legend-to-only/m-p/1376537#M83504</link>
      <description>&lt;P&gt;Hello, many thanks for this.&lt;/P&gt;&lt;P&gt;I had a go with configuring it so that the featurelayer is queried on refresh and the geometry the user draws is used to query it, but i'm definitely sure i've gone wrong somewhere.&lt;/P&gt;&lt;P&gt;&amp;nbsp;Could you provide some critiques if possible.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;        //Loop through webmap layers to retrieve features
        map.layers.forEach((FeatureLayer) =&amp;gt; { //For each layer in the webmap 
          FeatureLayer.on("refresh", function(event){ //On refresh
              if (event.dataChanged){ //If a change has happened
               const queryParams = FeatureLayer.createQuery(); //Create a feature query
               queryParams.geometry = sketchGeometry; //Set the feature query geometry as the one drawn by the user
               queryParams.returnGeometry = true;
                FeatureLayer.queryFeatures(queryParams).then(function(results){ //returns all the graphics from the layer view
                  map.layers.add(results.features);    }) }})});
                               
        view.when(() =&amp;gt; {//At the view
         var container = document.createElement("DIV"); //Create a container for the layer's legend
          container.style.backgroundColor = "#FFFFFF"; //Set container's background colour
          view.ui.add(container, "bottom-left"); // Add widget to the bottom right corner of the view
         addLegendForLayer(Legend, view, container, map.layers.getItemAt(0));//Run the addLegendForLayer() custom function to add the legend into the container             
        });&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2024 16:42:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/is-it-possible-to-configure-a-legend-to-only/m-p/1376537#M83504</guid>
      <dc:creator>EmilyGoldsmith</dc:creator>
      <dc:date>2024-01-31T16:42:35Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to configure a legend to only include features which are visible?</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/is-it-possible-to-configure-a-legend-to-only/m-p/1376628#M83506</link>
      <description>&lt;P&gt;My apologies, I sent you in the wrong direction due to a misunderstanding of the &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html#event-refresh" target="_self"&gt;refresh&lt;/A&gt; event.&amp;nbsp; I thought it would fire every time the layer refreshed itself, but that's not the case.&amp;nbsp; Instead, the trigger we're looking for is the associated FeatureLayerView's &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-views-layers-FeatureLayerView.html#updating" target="_self"&gt;updating&lt;/A&gt; property; when it changes from true to false, the layer has just finished refreshing.&amp;nbsp; It is then that we should &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-views-layers-FeatureLayerView.html#queryFeatures" target="_self"&gt;query&lt;/A&gt; the FeatureLayerView to see if it has any features, in which case I've also found it's best to use the view's extent (simply &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-views-layers-FeatureLayerView.html#queryFeatureCount" target="_self"&gt;getting the count&lt;/A&gt; of the FeatureLayerView's available features can include features that aren't visible on screen).&lt;/P&gt;&lt;P&gt;I've modified the code for the &lt;A href="https://developers.arcgis.com/javascript/latest/sample-code/sandbox/?sample=widgets-legend" target="_self"&gt;sample&lt;/A&gt; mentioned in the &lt;A href="https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/order-layers-in-map-and-independently-in-legend/m-p/1287108/highlight/true#M81096" target="_self"&gt;other thread&lt;/A&gt;, and is reproduced in its fullness below:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;html lang="en"&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="utf-8" /&amp;gt;
    &amp;lt;meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no" /&amp;gt;
    &amp;lt;title&amp;gt;Legend widget | Sample | ArcGIS Maps SDK for JavaScript 4.28&amp;lt;/title&amp;gt;
    &amp;lt;link rel="stylesheet" href="https://js.arcgis.com/4.28/esri/themes/light/main.css" /&amp;gt;

    &amp;lt;style&amp;gt;
      html,
      body,
      #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
        overflow: hidden;
      }
    &amp;lt;/style&amp;gt;

    &amp;lt;script src="https://js.arcgis.com/4.28/"&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script&amp;gt;
      function addLegendForLayer(Legend, view, container, layer) {
        var childElement = document.createElement("DIV");
        container.appendChild(childElement);

        const legend = new Legend({
          view: view,
          layerInfos: [
            {
              layer: layer,
              title: "NY Educational Attainment"
            }
          ],
          container: childElement
        });

        return {legend:legend,layer:layer};
      }

      require(["esri/views/MapView", "esri/widgets/Legend", "esri/WebMap"], (
        MapView,
        Legend,
        WebMap
      ) =&amp;gt; {
        const webmap = new WebMap({
          portalItem: {
            // autocasts as new PortalItem()
            id: "05e015c5f0314db9a487a9b46cb37eca"
          }
        });

        const view = new MapView({
          container: "viewDiv",
          map: webmap
        });

        view.when(() =&amp;gt; {
          var container = document.createElement("DIV");
          container.style.backgroundColor = "#FFFFFF";

          // Add widget to the bottom right corner of the view
          view.ui.add(container, "bottom-right");
          
          addLegendForLayer(Legend, view, container, webmap.layers.getItemAt(0));
          var item = addLegendForLayer(Legend, view, container, webmap.layers.getItemAt(0));

          var legend2 = item.legend;
          var layer2 = item.layer;

          view.whenLayerView(layer2).then(function(layerView2) {
            layerView2.watch("updating", function(newValue, oldValue, propertyName, target) {
              if ((oldValue) &amp;amp;&amp;amp; (!newValue)) {
                var query = layerView2.createQuery();
                query.geometry = view.extent;
                
                layerView2.queryFeatures(query).then(function(featureSet) {
                  legend2.visible = (featureSet.features.length &amp;gt; 0);
                });
              }
            });
          });
        });
      });
    &amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;

  &amp;lt;body class="calcite"&amp;gt;
    &amp;lt;div id="viewDiv"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To view this in action, go to the sample's &lt;A href="https://developers.arcgis.com/javascript/latest/sample-code/sandbox/?sample=widgets-legend" target="_self"&gt;sandbox&lt;/A&gt;, replace the code in the pane on the left with what's above (you can copy and paste), and click the "Refresh" button near the top-right of the page.&amp;nbsp; After it's loaded, zoom to an area where there are no features, and the 2nd legend item disappears.&amp;nbsp; Zoom back out to where there are features, and the legend reappears.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2024 17:53:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/is-it-possible-to-configure-a-legend-to-only/m-p/1376628#M83506</guid>
      <dc:creator>JoelBennett</dc:creator>
      <dc:date>2024-01-31T17:53:05Z</dc:date>
    </item>
  </channel>
</rss>

