Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>Tornado paths in the United States, 2017</title>
    <style>
      html,
      body {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
      }

      #header {
        height: 12%;
        text-align: center
      }

      #viewDiv {
        height: 86%;
      }

      #sidebar {
        z-index: 99;
        position: absolute;
        top: 12%;
        right: 0;
        height: 86%;
        background: rgba(176, 204, 231, 0.4);
        width: 320px;
        padding: 2.0rem;
        color: white;
      }

      #sidebar h5 {
        float: left;
        clear: left;
        ;
      }

      #state,
      #efNum {
        float: right;
      }

      .vals {
        font-weight: bolder;
      }

      footer {
        padding-top: 60px;
        float: right;
        clear: all;
      }

    </style>
    <link rel="stylesheet" href="https://js.arcgis.com/4.11/esri/themes/dark-blue/main.css">
    <link rel="stylesheet" href="https://s3-us-west-1.amazonaws.com/patterns.esri.com/files/calcite-web/1.2.5/css/calcite-web.min.css">
    <script src="https://s3-us-west-1.amazonaws.com/patterns.esri.com/files/calcite-web/1.2.5/js/calcite-web.min.js"></script>
    <script src="https://js.arcgis.com/4.11/"></script>
    <script>
      require([
        "esri/Map",
        "esri/views/MapView",
        "esri/widgets/BasemapToggle",
        "esri/layers/FeatureLayer",
        "esri/widgets/Legend",
        "esri/layers/GraphicsLayer",
        "esri/Graphic"
      ], function(Map, MapView, BasemapToggle, FeatureLayer, Legend, GraphicsLayer, Graphic) {

        var tornadoRenderer = {
          type: "unique-value",
          field: "MAG",
          defaultSymbol: other,
          defaultLabel: "Unclassified EF score (-9)",
          uniqueValueInfos: [{
              value: 0,
              symbol: ef0,
              label: "EF 0"
            },
            {
              value: 1,
              symbol: ef1,
              label: "EF 1"
            },
            {
              value: 2,
              symbol: ef2,
              label: "EF 2"
            },
            {
              value: 3,
              symbol: ef3,
              label: "EF 3"
            },
            {
              value: 4,
              symbol: ef4,
              label: "EF 4"
            }
          ]
        }

        var tornadoPopups = {
          "title": "2017 Tornado information",
          "content": "<b>Date: </b> {Date} <br> <b>EF (Fujita) Scale Number: </b> {mag} <br> <b> Injuries: </b> {inj} <br> <b> Fatalities: </b> {fat} <br> <b>Estimated Property Loss ($): </b> {loss}"
        };

        var tornadoLayer = new FeatureLayer({
          url: "https://services5.arcgis.com/njRHYVhl2CMXMsap/arcgis/rest/services/2017_Tornado_paths_in_the_US/FeatureServer",
          renderer: tornadoRenderer,
          popupTemplate: tornadoPopups
        });

        // GraphicsLayer for displaying results
        var resultsLayer = new GraphicsLayer();

        var map = new Map({
          basemap: "dark-gray-vector",
          layers: [tornadoLayer, resultsLayer]
        });

        var view = new MapView({
          container: "viewDiv",
          map: map,
          center: [-90.049, 35.1495],
          zoom: 4.75
        });

        var basemapToggle = new BasemapToggle({
          view: view,
          secondMap: "satellite"
        });

        

        // add querySelectors for input elements
        var state = document.querySelector("#state");
        var efNum = document.querySelector("#efNum");
        var fatal = document.querySelector("#fatal");
        var injury = document.querySelector("#injury");
        var loss = document.querySelector("#loss");

        fatal.addEventListener("input", function() {
          document.querySelector("#fatal-val").innerText = fatal.value;
        });

        injury.addEventListener("input", function() {
          document.querySelector("#injury-val").innerText = injury.value;
        });

        loss.addEventListener("input", function() {
          document.querySelector("#loss-val").innerText = loss.value;
        });

        fatal.addEventListener("input", function() {
          queryFatalities().then(displayResults);
        });

        state.addEventListener("change", function() {
          var type = event.target.value;
          if (type === "United States") {
            tornadoLayer.visible = true;
          }
          setTornadoInStateExpression(type);
          document.querySelector("#state-val").innerHTML = type;
        });



        var ef0 = {
          type: "simple-line",
          color: [239, 170, 110],
          width: 1,
          style: "solid"
        };

        var ef1 = {
          type: "simple-line",
          color: [234, 60, 225],
          width: 1,
          style: "solid"
        };

        var ef2 = {
          type: "simple-line",
          color: [237, 230, 104],
          width: 1,
          style: "solid"
        };

        var ef3 = {
          type: "simple-line",
          color: [165, 18, 18],
          width: 1,
          style: "solid"
        };

        var ef4 = {
          type: "simple-line",
          color: [219, 89, 8],
          width: 1,
          style: "solid"
        };

        var other = {
          type: "simple-line",
          color: [150, 150, 150],
          width: 1,
          style: "solid"
        };

        

        //map.add(tornadoLayer);

        view.ui.add(basemapToggle, "top-left");

        var legend = new Legend({
          view: view,
          layerInfos: [{
            layer: tornadoLayer
          }]
        });

        view.ui.add(legend, "bottom-left");

        // query all features from the tornado layer
        view.when(function() {
            return tornadoLayer.when(function() {
              var query = tornadoLayer.createQuery();
              return tornadoLayer.queryFeatures(query);
            });
          })
          .then(getValues)
          .then(getUniqueValues)
          .then(addToSelect);

        // return an array of all the values in the State field of the tornado layer
        function getValues(response) {
          var features = response.features;
          var values = features.map(function(feature) {
            return feature.attributes.st;
          });
          return values;
        }

        // return an array of all unique values in the State field of the tornado layer
        function getUniqueValues(values) {
          var uniqueValues = [];
          values.forEach(function(item, i) {
            if ((uniqueValues.length < 1 || uniqueValues.indexOf(item) === -1) &&
              item !== ""
            ) {
              uniqueValues.push(item);
            }
          });
          return uniqueValues;
        }

        // add the unique values to the states select element. Allows user to filter by state.
        function addToSelect(values) {
          values.sort();
          values.forEach(function(value) {
            var option = document.createElement("option");
            option.text = value;
            state.add(option);
          });
          return setTornadoInStateExpression(state.value);
        }

        // set the definition expression on the tornado layer to reflect user choice for state

        function setTornadoInStateExpression(newValue) {
          if (newValue !== "United States") {
            tornadoLayer.definitionExpression = "st = '" + newValue + "'";
          } else {
            tornadoLayer.definitionExpression = "st LIKE '%'";
          }
          tornadoLayer.visible = true;
          return queryforTornadoGeometries();
        }

        // get all the geometries of the tornado layer. The createQuery() method creates a query object that respects the definitionExpression of the layer
        function queryforTornadoGeometries() {
          var tornadoQuery = tornadoLayer.createQuery();

          return tornadoLayer.queryFeatures(tornadoQuery).then(function(response) {
            tornadoGeometries = response.features.map(function(feature) {
              return feature.geometry;
            });
            return tornadoGeometries;
          });
        }



        function queryFatalities() {
          var query = tornadoLayer.createQuery();
          query.where = "fat <=" + fatal.value;

          return tornadoLayer.queryFeatures(query);
        }


        function displayResults(results) {
          console.log("results: ", results);
          resultsLayer.removeAll();
          console.log("results.features.map: ", results.features.map);
          var features = results.features.map(function(graphic) {
            graphic.symbol = {
              type: "simple-line",
              width: 2,
              color: "darkorange"
            };
            return graphic;
          });
          console.log("features: ", features);
          resultsLayer.addMany(features);
        }

      });

    </script>
  </head>

  <body>
    <div id="header">
      <h1>Tornadoes in the United States, 2017</h1>
    </div>
    <div id="viewDiv">
      <div id="sidebar">
        <h4>Query the Tornado layer</h4>
        <h5>State: </h5> <select name="state" id="state">
            <option value="United States">United States</option>
          </select>

        <h5>Fatalities: </h5> <input type="range" id="fatal" name="Deaths" min="0" max="11" value="11">
        <h5>Injuries: </h5> <input type="range" id="injury" name="Injured" min="0" max="88" value="88">
        <h5>Loss ($): </h5> <input type="range" id="loss" name="loss" min="0" max="310300000" step="100000" value="310300000">
        <br>
        <div id="results">
          <h5>Showing tornadoes that occured in <br>
            <span class="vals" id="state-val">United States</span> which caused <br>
            <span class="vals" id="fatal-val">11</span> or fewer deaths, <br>
            <span class="vals" id="injury-val">88</span> or fewer injuries, and <br> $
            <span class="vals" id="loss-val">310,000,000</span> or less in financial losses.
          </h5>
        </div>
        <br>
        <footer>
        </footer>



      </div>
    </div>
  </body>

</html>

              
            
!

CSS

              
                
              
            
!

JS

              
                
              
            
!
999px

Console