Esri Leaflet

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.

<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.5.1/dist/leaflet.css"
  integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
  crossorigin=""/>
  <script src="https://unpkg.com/leaflet@1.5.1/dist/leaflet.js"
  integrity="sha512-GffPMF3RvMeYyc1LWMHtK8EbPv0iNZ8/oTtHPx9/cc2ILxQ+u905qIwdpULaqDkyBKgOaB57QTMg7ztg8Jm2Og=="
  crossorigin=""></script>


  <!-- Load Esri Leaflet from CDN -->
  <script src="https://unpkg.com/esri-leaflet@2.3.1/dist/esri-leaflet.js"
  integrity="sha512-Np+ry4Dro5siJ1HZ0hTwn2jsmu/hMNrYw1EIK9EjsEVbDge4AaQhjeTGRg2ispHg7ZgDMVrSDjNrzH/kAO9Law=="
  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.basemapLayer('Topographic').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>