Hide Table of Contents
View Show query results with a chart sample in sandbox
Show query results with a chart


This sample shows how you can use query task results to construct a chart with the Google Chart API. When you run the sample, click on a county to see a chart of its demographic data appear in a dismissable InfoWindow.

The function init creates a Map, QueryTask and a Query. Notice that the constructor for the QueryTask requires the URL of a layer in a map service (http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/3). The number 3 at the end of the URL is the index of the counties layer in the map table of contents. To find out the URL to your own map service and its layer indexes, use the Services Directory

The Query object contains the conditions for the query such as what geometry to query, whether or not to return the geometry so it can be displayed on the map, and which fields should be queried.

The following event listener detects when someone clicks the map:

dojo.connect(map, "onClick", doQuery);

The doQuery function in the above listener captures the location of the map click and sets it as the query.Geometry. The county that this point intersects will be queried. The following line runs the query task:


When the query task completes, another event fires, calling the getChart function:

dojo.connect(queryTask, "onComplete", getChart);

getChart reads the query results, which are originally returned as a FeatureSet. The function parses the demographic totals, calculates percentages for each demographic group, and constructs the required URL syntax for the Google Chart API. This function also symbolizes the queried feature with a red dashed outline and displays the InfoWindow after the chart is created.


<!DOCTYPE html>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
    <title>Demographic Charts</title>

    <link rel="stylesheet" href="https://js.arcgis.com/3.28/dijit/themes/claro/claro.css">
    <link rel="stylesheet" href="https://js.arcgis.com/3.28/esri/css/esri.css">
    <script src="https://js.arcgis.com/3.28/"></script>

      var map, queryTask, query, click;
      var wd = 240;
      var ht = 110;
      var chartParams = { cht:"p3", chl:"White|Black|Hispanic|Asian|Other" };

      function init() {     
        map = new esri.Map("map", {
          basemap: "streets",
          center: [-111.494, 35.47],
          zoom: 7
        map.addLayer(new esri.layers.ArcGISDynamicMapServiceLayer("https://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer", { opacity:0.4 }));
        dojo.connect(map, "onClick", doQuery);

        queryTask = new esri.tasks.QueryTask("https://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/3");
        dojo.connect(queryTask, "onComplete", getChart);

        dojo.connect(map.infoWindow, "onHide", function() {map.graphics.clear();});

        query = new esri.tasks.Query();
        query.spatialRelationship = esri.tasks.Query.SPATIAL_REL_INTERSECTS;
        query.outFields = ["NAME", "WHITE", "BLACK", "ASIAN", "HISPANIC", "OTHER"];
        query.returnGeometry = true;
        query.outSpatialReference = {"wkid":102100};


      function doQuery(evt) {
        click = query.geometry = evt.mapPoint;

      function getChart(featureSet) {
        var features = featureSet.features;
        var feature, attributes, white, black, asian, hispanic, other, total;
        for (var i=0; i<features.length; i++) {
          feature = features[i];
          attributes = feature.attributes;

          white = parseInt(attributes.WHITE, 10);
          black = parseInt(attributes.BLACK, 10);
          asian = parseInt(attributes.ASIAN, 10);
          hispanic = parseInt(attributes.HISPANIC, 10);
          other = parseInt(attributes.OTHER, 10);

          total = white + black + asian + hispanic + other;

          white = (white / total) * 100;
          black = (black / total) * 100;
          asian = (asian / total) * 100;
          hispanic = (hispanic / total) * 100;
          other = (other / total) * 100;

          var params = dojo.mixin({
              chs:wd + "x" + ht,
              chd: "t:" + white + "," + black + "," + hispanic + "," + asian + "," + other
            }, chartParams);

          var mySymbol = new esri.symbol.SimpleFillSymbol("none", new esri.symbol.SimpleLineSymbol("dashdot", new dojo.Color([255,0,0]), 2.5), new dojo.Color([255,255,0,0.25]));

          map.infoWindow.setContent("<img src=\"" + "https://chart.googleapis.com/chart?"
                                     + decodeURIComponent(dojo.objectToQuery(params)) + "\" />");

  <body class="claro">
    <span style="font-size:200%; font-weight:bold;">USA County Demographics</span>
    <div id="map" style="width:1000px; height:500px; border:1px solid #000;"></div>
Show Modal