<?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 Legend Alternative in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/legend-alternative/m-p/539397#M50232</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have found that the legend dijit does not suit my needs entirely.&amp;nbsp; Its easy to implement, but the legend does not seem to be able to show layers if they were outside the visible scale range and/or checked off... which is the case when my map loads...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So an alternative would be to build a legend... first I made sprites and hard coded everything in... what a time drain! &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So I thought about how I could dynamically build one on the fly... only when it is needed...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In my app I have dojo titlepanes that act as the containers for the legend information.&amp;nbsp; Each layer in my map service gets its own legend. Its similar to a table of contents... They are closed to start, but when clicked, the title pane checks to see if a legend has been built... if not, it builds one... &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Using an esri request on the map service legend rest endpoint allowed me create legend info for a bunch of layers regardless of whether it was visible&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is the code... dont get confused by the layerPrefix... like I said I have a bunch of layers... all aerial data collection info...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I guess if anyone has a better solution or has a tweak Id be happy to see it posted, otherwise just wanted to share if people found themselves with a similar scenario...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Jeff&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;var aerial2000LegendTitlePane = new TitlePane({
&amp;nbsp; title: "Legend", 
&amp;nbsp; legendBuilt: false,
&amp;nbsp; onClick: function(){
&lt;SPAN&gt;&amp;nbsp; makeLegend("aerial2000", "&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://mydomain/arcgis/rest/services/mymapservice/MapServer/legend" rel="nofollow noopener noreferrer" target="_blank"&gt;http://mydomain/arcgis/rest/services/mymapservice/MapServer/legend&lt;/A&gt;&lt;SPAN&gt;", 12);&lt;/SPAN&gt;
&amp;nbsp; }, 
&amp;nbsp; open: false}, 'aerial2000LegendTitlePane');&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The inputs include&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;a name prefix which corresponds to my html structure&lt;/LI&gt;&lt;LI&gt;the map service legend endpoint&lt;/LI&gt;&lt;LI&gt;the id of the layer in the map service&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;/PRE&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;/*********************************** 
build legend
************************************/
function makeLegend(layerPrefix, serviceURL, serviceLayerID) {

&amp;nbsp; if (registry.byId(layerPrefix + "LegendTitlePane").get("legendBuilt") == false) {
&amp;nbsp; var theLegend = esriRequest({
&amp;nbsp; url: serviceURL, 
&amp;nbsp; handleAs: 'json', 
&amp;nbsp; content: { f: 'json' }, 
&amp;nbsp; callbackParamName: 'callback'}
&amp;nbsp; );
&amp;nbsp; theLegend.then(function(response) {
&amp;nbsp; console.log("Success: ", response);
&amp;nbsp; var filteredArray = array.filter(response.layers, function(item){
&amp;nbsp; return item.layerId == serviceLayerID;
&amp;nbsp; });

&amp;nbsp; registry.byId(layerPrefix + "LegendTitlePane").set("content", domConstruct.create("div",{id: layerPrefix + "Legend"}));

&amp;nbsp; array.forEach(filteredArray[0].legend, function (item, idx){
&amp;nbsp; var legendRow = domConstruct.create("div",{class: "LegendRow"}, layerPrefix + "Legend", 'last');
&amp;nbsp; var iconDiv = domConstruct.create("div", {style: " vertical-align: middle;display:inline-block;width:" + item.width + "px;height:" + item.height + "px;background:url(data:image/png;base64," + item.imageData + ") no-repeat;"}, legendRow, "first");
&amp;nbsp; var labelDiv = domConstruct.create("div", {style: " vertical-align: middle;display:inline-block;", innerHTML: item.values[0]}, legendRow, "last");
&amp;nbsp; });

&amp;nbsp; registry.byId(layerPrefix + "LegendTitlePane").set("legendBuilt", true);
&amp;nbsp; }, function(error) {
&amp;nbsp; console.log("Error: ", error);
&amp;nbsp; domConstruct.create("span", {innerHTML: "Legend Not Available"}, layerPrefix + "Legend", "first");
&amp;nbsp; });
&amp;nbsp; }

}&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 23:23:46 GMT</pubDate>
    <dc:creator>JeffreySchmidt</dc:creator>
    <dc:date>2021-12-11T23:23:46Z</dc:date>
    <item>
      <title>Legend Alternative</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/legend-alternative/m-p/539397#M50232</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have found that the legend dijit does not suit my needs entirely.&amp;nbsp; Its easy to implement, but the legend does not seem to be able to show layers if they were outside the visible scale range and/or checked off... which is the case when my map loads...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So an alternative would be to build a legend... first I made sprites and hard coded everything in... what a time drain! &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So I thought about how I could dynamically build one on the fly... only when it is needed...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In my app I have dojo titlepanes that act as the containers for the legend information.&amp;nbsp; Each layer in my map service gets its own legend. Its similar to a table of contents... They are closed to start, but when clicked, the title pane checks to see if a legend has been built... if not, it builds one... &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Using an esri request on the map service legend rest endpoint allowed me create legend info for a bunch of layers regardless of whether it was visible&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is the code... dont get confused by the layerPrefix... like I said I have a bunch of layers... all aerial data collection info...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I guess if anyone has a better solution or has a tweak Id be happy to see it posted, otherwise just wanted to share if people found themselves with a similar scenario...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Jeff&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;var aerial2000LegendTitlePane = new TitlePane({
&amp;nbsp; title: "Legend", 
&amp;nbsp; legendBuilt: false,
&amp;nbsp; onClick: function(){
&lt;SPAN&gt;&amp;nbsp; makeLegend("aerial2000", "&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://mydomain/arcgis/rest/services/mymapservice/MapServer/legend" rel="nofollow noopener noreferrer" target="_blank"&gt;http://mydomain/arcgis/rest/services/mymapservice/MapServer/legend&lt;/A&gt;&lt;SPAN&gt;", 12);&lt;/SPAN&gt;
&amp;nbsp; }, 
&amp;nbsp; open: false}, 'aerial2000LegendTitlePane');&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The inputs include&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;a name prefix which corresponds to my html structure&lt;/LI&gt;&lt;LI&gt;the map service legend endpoint&lt;/LI&gt;&lt;LI&gt;the id of the layer in the map service&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;/PRE&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;/*********************************** 
build legend
************************************/
function makeLegend(layerPrefix, serviceURL, serviceLayerID) {

&amp;nbsp; if (registry.byId(layerPrefix + "LegendTitlePane").get("legendBuilt") == false) {
&amp;nbsp; var theLegend = esriRequest({
&amp;nbsp; url: serviceURL, 
&amp;nbsp; handleAs: 'json', 
&amp;nbsp; content: { f: 'json' }, 
&amp;nbsp; callbackParamName: 'callback'}
&amp;nbsp; );
&amp;nbsp; theLegend.then(function(response) {
&amp;nbsp; console.log("Success: ", response);
&amp;nbsp; var filteredArray = array.filter(response.layers, function(item){
&amp;nbsp; return item.layerId == serviceLayerID;
&amp;nbsp; });

&amp;nbsp; registry.byId(layerPrefix + "LegendTitlePane").set("content", domConstruct.create("div",{id: layerPrefix + "Legend"}));

&amp;nbsp; array.forEach(filteredArray[0].legend, function (item, idx){
&amp;nbsp; var legendRow = domConstruct.create("div",{class: "LegendRow"}, layerPrefix + "Legend", 'last');
&amp;nbsp; var iconDiv = domConstruct.create("div", {style: " vertical-align: middle;display:inline-block;width:" + item.width + "px;height:" + item.height + "px;background:url(data:image/png;base64," + item.imageData + ") no-repeat;"}, legendRow, "first");
&amp;nbsp; var labelDiv = domConstruct.create("div", {style: " vertical-align: middle;display:inline-block;", innerHTML: item.values[0]}, legendRow, "last");
&amp;nbsp; });

&amp;nbsp; registry.byId(layerPrefix + "LegendTitlePane").set("legendBuilt", true);
&amp;nbsp; }, function(error) {
&amp;nbsp; console.log("Error: ", error);
&amp;nbsp; domConstruct.create("span", {innerHTML: "Legend Not Available"}, layerPrefix + "Legend", "first");
&amp;nbsp; });
&amp;nbsp; }

}&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:23:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/legend-alternative/m-p/539397#M50232</guid>
      <dc:creator>JeffreySchmidt</dc:creator>
      <dc:date>2021-12-11T23:23:46Z</dc:date>
    </item>
  </channel>
</rss>

