Add Legend Widget to Sidebar

2233
3
Jump to solution
12-12-2016 10:32 AM
chuckfrank
Occasional Contributor

I'm using the sidebar example ArcGIS API for JavaScript Sandbox  and I would like to place the legend widget inside of it.  Is this possible? So far I have only been able to place it at a default location.  It would be great if I could place it inside of a div of my choosing.

Thanks

Chuck

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Sure here is a sample with that:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
  <title>View padding - 4.1</title>
  <style>
    html,
    body,
    #viewDiv {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }

    #sidebar {
      z-index: 99;
      position: absolute;
      top: 0;
      right: 0;
      height: 100%;
      background: rgba(0, 0, 0, 0.5);
      width: 320px;
    }

    #text {
      color: white;
      padding: 3%;
    }

    .esri-legend.esri-widget {
      background-color: rgba(255,255,255,0);
    }
  </style>
  <link rel="stylesheet" href="https://js.arcgis.com/4.1/esri/css/main.css">
  <script src="https://js.arcgis.com/4.1/"></script>
  <script>
    require([
      "esri/WebMap",
      "esri/views/MapView",
      "esri/widgets/Legend",
      "dojo/dom",
      "dojo/domReady!"
    ], function(
      WebMap,
      MapView,
      Legend,
      dom
    ) {

      var webmap = new WebMap({
        portalItem: { // autocasts as new PortalItem()
          id: "4abe6a830b8f466dacf8abfde567a781"
        }
      });

      // Create the view set the view padding to be 320 px from the right
      var view = new MapView({
        container: "viewDiv",
        map: webmap,
        center: [-74.045459, 40.690083], // Liberty Island, NY, USA
        zoom: 16,
        padding: {
          right: 320 // Same value as the #sidebar width in CSS
        }
      });

      view.then(function() {
          // get the first layer in the collection of operational layers in the WebMap
          // when the resources in the MapView have loaded.
          var featureLayer = webmap.layers.getItemAt(0);

          var legend = new Legend({
            view: view,
            layerInfos: [{
              layer: featureLayer,
              title: "NY Educational Attainment"
            }]
          }, "text");
        });

    });
  </script>
</head>

<body>
  <div id="viewDiv">
    <div id="sidebar">
      <div id="text">
      </div>
    </div>
  </div>
</body>
</html>

View solution in original post

3 Replies
RobertScheitlin__GISP
MVP Emeritus

Sure here is a sample with that:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
  <title>View padding - 4.1</title>
  <style>
    html,
    body,
    #viewDiv {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }

    #sidebar {
      z-index: 99;
      position: absolute;
      top: 0;
      right: 0;
      height: 100%;
      background: rgba(0, 0, 0, 0.5);
      width: 320px;
    }

    #text {
      color: white;
      padding: 3%;
    }

    .esri-legend.esri-widget {
      background-color: rgba(255,255,255,0);
    }
  </style>
  <link rel="stylesheet" href="https://js.arcgis.com/4.1/esri/css/main.css">
  <script src="https://js.arcgis.com/4.1/"></script>
  <script>
    require([
      "esri/WebMap",
      "esri/views/MapView",
      "esri/widgets/Legend",
      "dojo/dom",
      "dojo/domReady!"
    ], function(
      WebMap,
      MapView,
      Legend,
      dom
    ) {

      var webmap = new WebMap({
        portalItem: { // autocasts as new PortalItem()
          id: "4abe6a830b8f466dacf8abfde567a781"
        }
      });

      // Create the view set the view padding to be 320 px from the right
      var view = new MapView({
        container: "viewDiv",
        map: webmap,
        center: [-74.045459, 40.690083], // Liberty Island, NY, USA
        zoom: 16,
        padding: {
          right: 320 // Same value as the #sidebar width in CSS
        }
      });

      view.then(function() {
          // get the first layer in the collection of operational layers in the WebMap
          // when the resources in the MapView have loaded.
          var featureLayer = webmap.layers.getItemAt(0);

          var legend = new Legend({
            view: view,
            layerInfos: [{
              layer: featureLayer,
              title: "NY Educational Attainment"
            }]
          }, "text");
        });

    });
  </script>
</head>

<body>
  <div id="viewDiv">
    <div id="sidebar">
      <div id="text">
      </div>
    </div>
  </div>
</body>
</html>
KenBuja
MVP Esteemed Contributor

To expand on Robert Scheitlin, GISP‌ reply, in the constructor, you can specify the HTML element where the legend will be placed.

0 Kudos
chuckfrank
Occasional Contributor

Thanks Robert.  That's excellent.  I didn't know the name of the html element, "text" in this case, could be used.  Thanks!

0 Kudos