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/dark/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="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 {
  padding: 10px;
  padding-right: 15px;
  text-align: center;
  width: 250px;
}

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

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

.horizontalLabels {
  padding: 5px;
  background: rgba(0,0,0,0);
}

button {
  cursor: pointer;
}

.esri-histogram__bars{
  cursor: pointer;
}
              
            
!

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 = "LATITUDE";

  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: "Latitude"
        },
        stops: [
          { value: -1, color: "#00ffc8", label: "< -45" },
          // { value: -22.5, color: "#1db695", label: "" },
          { value: 0, color: "#4a443d", label: "0" },
          // { value: 22.5, color: "#a06022", label: "" },
          { value: 1, color: "#ff7d00", label: "> 45" }
        ]
      }]
    },
    popupEnabled: false
  });

  const gridLayer = new FeatureLayer({ 
    portalItem: { id: "68f844f9efd04df6a4636e0f410ac072" }
  });

  const map = new Map({
    basemap: "dark-gray-vector",
    layers: [layer, gridLayer]
  });

  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);
    })
    .catch(function(error){
      console.error(error);
    });


  let histogramChart;
  let histogramMinLabel = document.getElementById("minLabel");
  let histogramMaxLabel = document.getElementById("maxLabel");
  let histogramTitle = document.getElementById("title");

  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.layout = "vertical";
      histogramChart.average = average;
      histogramChart.labelFormatFunction = formatValue;
      histogramChart.dataLines = [{
        value: 0,
        label: "Equator"
      }, {
        value: 23.43674,
        label: "Tropic of Cancer"
      }, {
        value: -23.43674,
        label: "Tropic of Capricorn"
      }];
      histogramChart.dataLineCreatedFunction = function(lineElement, labelElement, index){
        labelElement.setAttribute("fill", "#d1d1d1");
      };
      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;
          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(){
    layerView.effect = 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
    }) + "°";
  }

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