Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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>Histogram</title>

  <link rel="stylesheet" href="https://js.arcgis.com/4.12/esri/themes/light/main.css"/>
  <script src="https://js.arcgis.com/4.12/"></script>
</head>

<body>
  <div id="viewDiv"></div>
  <div id="containerDiv" class="esri-widget">
    <div style="padding-bottom:10px;">Current Weather Conditions</div>
    <div style="padding-bottom:10px;"><span id="title"></span></div>
      <div id="selected-range">
        <span id="bar-min"></span> - <span id="bar-max"></span>
      </div>
      <div id="histogramDiv"></div>
      <div class="horizontalLabels esri-widget">
        <span style="float: left" id="minLabel"></span>
        <span style="float: right" id="maxLabel"></span>
      </div>
      <div>
        <button class="esri-widget" id="clearEffect">Clear Selection</button>
      </div>
  </div>
</body>
</html>

              
            
!

CSS

              
                html,
body,
#viewDiv {
  padding: 0;
  margin: 0;
  height: 100%;
  width: 100%;
}

#containerDiv {
  background-color:#cfcfcf;
  padding: 10px;
  padding-right: 15px;
  text-align: center;
  width: 300px;
}

#histogramDiv {
  width: 100%;
  height: 200px;
  float:right;
  background: rgba(0,0,0,0);
}

#title{
  font-size: 16.38px;
  font-weight: 700;
}

button {
  cursor: pointer;
}

.esri-histogram__bars{
  cursor: pointer;
}

#selected-range{
  visibility: hidden;
  float: left;
}
              
            
!

JS

              
                require([
  "esri/Map",
  "esri/views/MapView",
  "esri/layers/FeatureLayer",
  "esri/renderers/smartMapping/statistics/histogram",
  "esri/renderers/smartMapping/statistics/summaryStatistics",
  "esri/widgets/Histogram",
  "esri/Color",
  "esri/intl",
  "esri/widgets/Legend"
], function(
  Map,
  MapView,
  FeatureLayer,
  histogram,
  summaryStatistics,
  Histogram,
  Color,
  intl,
  Legend
) {

  // WIND_CHILL, TEMP, DEW_POINT, R_HUMIDITY, PRESSURE
  // HEAT_INDEX, LATITUDE, LONGITUDE
  const field = "PRESSURE";

  const layer = new FeatureLayer({
    url: "https://services9.arcgis.com/RHVPKKiFTONKtxq3/ArcGIS/rest/services/NOAA_METAR_current_wind_speed_direction_v1/FeatureServer/0",
    opacity: 1,
    renderer: {
      type: "simple",
      symbol: {
        type: "simple-marker",
        outline: {
          color: [153,153,153,0.25],
          width: 0.375
        },
        color: "gray",
        size: 4
      },
      visualVariables: [{
        type: "color",
        field: field,
        legendOptions: {
          title: "Altimeter Pressure (Millibars)"
        },
        stops: [
          { value: 1007, color: "#e2ebd3", label: "< 1007" },
          { value: 1011, color: "#c8e09f", label: "" },
          { value: 1015, color: "#add46a", label: "1015" },
          { value: 1020, color: "#62afa7", label: "" },
          { value: 1024, color: "#2d8078", label: "> 1024" }
        ]
      }]
    },
    popupEnabled: false
  });

  const map = new Map({
    basemap: {
      portalItem: {
        id: "3582b744bba84668b52a16b0b6942544"
      }
    },
    layers: [layer]
  });

  view = new MapView({
    container: "viewDiv",
    map: map,
    scale: 73957190.948944,
    center: [-44.91149910246385, 36.475324760262716],
    highlightOptions: {
      color: "black"
    }
  });

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

  let layerView = null;
  view.whenLayerView(layer)
    .then(function(lv){
      layerView = lv;
      return getAverage(field)
    })
    .then(function(average){
      return generateHistogram(field, average);
    })
    .catch(function(error){
      console.error(error);
    });


  let histogramChart;
  let histogramMinLabel = document.getElementById("minLabel");
  let histogramMaxLabel = document.getElementById("maxLabel");
  let histogramTitle = document.getElementById("title");
  let barMin = document.getElementById("bar-min");
  let barMax = document.getElementById("bar-max");
  let selectedRange = document.getElementById("selected-range");

  function generateHistogram(fieldName, average){
    return histogram({
      layer: layer,
      field: fieldName,
      numBins: 100
    }).then(function(histogramResult){
      if(!histogramChart){
        histogramChart = Histogram.fromHistogramResult(histogramResult);
        histogramChart.container = "histogramDiv";
      } else {
        histogramChart.min = histogramResult.minValue;
        histogramChart.max = histogramResult.maxValue;
        histogramChart.bins = histogramResult.bins;
      }
      histogramChart.average = average;
      histogramChart.labelFormatFunction = formatValue;
      const vv = layer.renderer.visualVariables[0];
      histogramChart.barCreatedFunction = function(index, element) {
        const bin = histogramChart.bins[index];
        const midValue = (bin.maxValue - bin.minValue) / 2 + bin.minValue;
        const color = getColorFromValue(vv.stops, midValue);
        element.setAttribute("fill", color.toHex());

        element.addEventListener("focus", function() {
          const { minValue, maxValue, count } = bin;

          barMin.innerText = minValue.toFixed(2);
          barMax.innerText = maxValue.toFixed(2);
          selectedRange.style.visibility = "visible";
          
          const query = layerView.layer.createQuery();
          query.where = `${field} >= ${minValue} AND ${field} <= ${maxValue}`;
          layerView.queryObjectIds(query).then(function(ids) {
            layerView.effect = {
              filter: {
                objectIds: ids
              },
              excludedEffect: "grayscale(100%) opacity(5%)"
            };
          });
        });
      };
      histogramMaxLabel.innerText = formatValue(histogramResult.maxValue);
      histogramMinLabel.innerText = formatValue(histogramResult.minValue);
      histogramTitle.innerText = vv.legendOptions.title;
    });
  }

  const clearBtn = document.getElementById("clearEffect");
  clearBtn.addEventListener("click", function(){
    selectedRange.style.visibility = "hidden";
    layerView.effect = null;
    if(highlightHandle){
      highlightHandle.remove();
      highlightHandle = null;
    }

    if(histogramChart.dataLines){
      histogramChart.dataLines = null;
    }
  });

  function getAverage(fieldName) {
    return summaryStatistics({
      layer: layer,
      field: fieldName
    }).then(function(statistics) {
      return statistics.avg;
    });
  }

  function formatValue (value) {
    return intl.formatNumber(value, {
      minimumFractionDigits: 0,
      maximumFractionDigits: 0
    });
  }

  let highlightHandle;

  view.on("click", function(event){
    view.hitTest(event).then(function(hitResponse){
      if(highlightHandle){
        highlightHandle.remove();
        highlightHandle = null;
      }
      const graphicHit = hitResponse.results.length;
      if(!graphicHit){
        console.log("no hit");
        return;
      }
      const matchingHit = hitResponse.results.find(function(hit){
        return hit.graphic.layer.title === layer.title;
      });
      if(matchingHit){
        const graphic = matchingHit.graphic;
        const avgIncome = graphic.attributes[field];
        histogramChart.dataLines = [{
          value: avgIncome,
          label: formatValue(avgIncome)
        }];
        highlightHandle = layerView.highlight(graphic);
      } else {
        histogramChart.dataLines = null;
      }
    })
  });

  // infers the color for a given value
  // based on the stops from a ColorVariable
  function getColorFromValue(stops, value) {
    let minStop = stops[0];
    let maxStop = stops[stops.length - 1];

    const minStopValue = minStop.value;
    const maxStopValue = maxStop.value;

    if (value < minStopValue) {
      return minStop.color;
    }

    if (value > maxStopValue) {
      return maxStop.color;
    }

    const exactMatches = stops.filter(function(stop) {
      return stop.value === value;
    });

    if (exactMatches.length > 0) {
      return exactMatches[0].color;
    }

    minStop = null;
    maxStop = null;
    stops.forEach(function(stop, i) {
      if (!minStop && !maxStop && stop.value >= value) {
        minStop = stops[i - 1];
        maxStop = stop;
      }
    });

    const weightedPosition =
      (value - minStop.value) / (maxStop.value - minStop.value);

    return Color.blendColors(
      minStop.color,
      maxStop.color,
      weightedPosition
    );
  }

});
              
            
!
999px

Console