Esri Leaflet
This content has moved to developers.arcgis.com. Please update your bookmarks!

Dynamic feature charting with Cedar

Plot feature attributes on a dynamic chart that updates as users pan and zoom. This demo relies on Esri Cedar to render an interactive scatterplot.

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title>Dynamic feature charting with Cedar</title>
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />

  <!-- Load Leaflet from CDN -->
  <link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css"
    integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A=="
    crossorigin=""/>
  <script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"
    integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
    crossorigin=""></script>

  <!-- Load Esri Leaflet from CDN -->
  <script src="https://unpkg.com/esri-leaflet@3.0.8/dist/esri-leaflet.js"
    integrity="sha512-E0DKVahIg0p1UHR2Kf9NX7x7TUewJb30mxkxEm2qOYTVJObgsAGpEol9F6iK6oefCbkJiA4/i6fnTHzM6H1kEA=="
    crossorigin=""></script>

  <!-- Load Esri Leaflet Vector from CDN -->
  <script src="https://unpkg.com/esri-leaflet-vector@4.0.0/dist/esri-leaflet-vector.js"
    integrity="sha512-EMt/tpooNkBOxxQy2SOE1HgzWbg9u1gI6mT23Wl0eBWTwN9nuaPtLAaX9irNocMrHf0XhRzT8B0vXQ/bzD0I0w=="
    crossorigin=""></script>

  <style>
    body { margin:0; padding:0; }
    #map { position: absolute; top:0; bottom:0; right:0; left:0; }
  </style>
</head>
<body>

<!-- Load Cedar files from CDN -->
<!-- load the amCharts base library -->
<script src="https://www.amcharts.com/lib/3/amcharts.js"></script>
<!-- for scatter and bubble charts -->
<script src="https://www.amcharts.com/lib/3/xy.js"></script>
<!-- load the arcgis-rest-js scripts -->
<script src="https://unpkg.com/@esri/arcgis-rest-request"></script>
<!-- optionally load an amcharts theme; cedar provides a calcite theme -->
{{!-- <script src="https://unpkg.com/@esri/cedar/dist/umd/themes/amCharts/calcite.js"></script> --}}
<!-- load cedar -->
<script src="https://unpkg.com/@esri/cedar/dist/umd/cedar.js"></script>

<style>
  .chart-container {
    height: 245px;
    width: 245px;
  }

  #info-pane {
    position: absolute;
    top: 10px;
    right: 10px;
    z-index: 400;
    background: white;
  }

  .amcharts-chart-div > a {
    display: none !important;
  }

  #chart {
    height: 100%;
  }
</style>

<div id="map"></div>
<div id="info-pane" class="leaflet-bar chart-container">
  <div id="chart" ></div>
</div>

<script>
  // STEP 1: MAKE A MAP AND ADD LAYERS
  var map = L.map('map').setView([45.526, -122.667], 13);

  L.esri.Vector.vectorBasemapLayer('ArcGIS:Community', {
    apikey: apiKey // Replace with your API key - https://developers.arcgis.com
  }).addTo(map);

  // create and add a feature layer
  // features from this layer will appear in the Cedar scatterplot
  var treesFeatureLayer = L.esri.featureLayer({
    url: 'https://www.portlandmaps.com/arcgis/rest/services/Public/Parks_Misc/MapServer/21/'
  });

  treesFeatureLayer.addTo(map);

  // STEP 2: DEFINE A CHART
  var chart;
  treesFeatureLayer.once('load', function () {
    var datasets = [getDataset(treesFeatureLayer)];

    // designate a one or more series to show the data on the chart
    var series = [{
      value: { field: 'HEIGHT', label: 'tree height' },
      category: { field: 'DIAMETER', label: 'tree diameter' }
    }];

    // optionally override any of the cart type's default styles
    var overrides = {
      hideXScrollbar: true,
      hideYScrollbar: true
    };

    // create a cedar chart using the known 'scatter' type
    var elementId = 'chart';
    chart = new cedar.Chart(elementId, { type: 'scatter' })
      .datasets(datasets)
      .series(series)
      .overrides(overrides);

    // render the chart
    chart.show();
  });

  // STEP 3: MAKE THE CHART DYNAMIC BY ESTABLISHING MAP-TO-CHART COMMUNICATION
  // show in the scatterplot only the features in the map's current extent
  map.on('zoom move', function () {
    var datasets = [getDataset(treesFeatureLayer)];
    chart.datasets(datasets);
    chart.show();
  });

  // Helper function, used by both createChart() and updateChart(),
  // to get the data from the leaflet map (current extent) and return it
  // in the proper format for Cedar to use.
  function getDataset (layer) {
    var scatterPlotDataArray = [];
    layer.eachActiveFeature(function (e) {
      scatterPlotDataArray.push({
        attributes: {
          HEIGHT: e.feature.properties.HEIGHT,
          DIAMETER: e.feature.properties.DIAMETER
        },
        geometry: {
          x: e.feature.geometry.coordinates[0],
          y: e.feature.geometry.coordinates[1]
        }
      });
    });
    return {
      data: {
        features: scatterPlotDataArray
      }
    };
  }
</script>

</body>
</html>