Search code examples
javascriptarcgis-js-apiesri-maps

Calculate sum in several fields with StatisticsDefinition()


I have built a widget in WebAppBuilder for ArcGis where you can select an area and see the information that is within it, the selection is based on the radius you choose, in this case between 100 and 300 metres.

I'd like to add a functionality so I would like to create from the first query result the sum of the four fields into the attribute table.

I found out on the API the Statitics Definition class so I used with the result from the first query but it is not working.

I'd appreciate if someone could give some advice about the sum query.

Thanks in advace.

Below the code :

 var myQuery = new Query();
         myQuery.where = "1 = 1";
         myQuery.outFields = ["POB_TOT", "EDAD0015", "EDAD1664","EDAD65_"];

         myQuery.returnGeometry = true;
         var myQueryTask = new QueryTask(_poblacion);

           myQuery.geometry = circle;
           myQuery.spatialRelationship = Query.SPATIAL_REL_INTERSECTS;


    var sumfields = new StatisticDefinition();
          sumfields.statisticType = "sum";

          sumfields.onStatisticField = "POB_TOT", "EDAD0015", "EDAD1664","EDAD65_";

          myQuery.outStatistics = sumfields;

Solution

  • As I can see there are different workaround for this. you can not pass multiple fields However you can pass multiple outStatistics in your case.

    So below the sample in your case-

    Example-

    var sumfields1 = new StatisticDefinition();
    sumfields1.statisticType = "sum";
    sumfields1.onStatisticField = "POB_TOT"; //any one numeric field
    sumfields1.outStatisticFieldName = "sum_Pob_Tot";
    
    var sumfields2 = new StatisticDefinition();
    sumfields2.statisticType = "sum";
    sumfields2.onStatisticField = "EDAD0015"; //any one numeric field
    sumfields2.outStatisticFieldName = "sum_EDAD0015"; 
    
    var sumfields3 = new StatisticDefinition();
    sumfields3.statisticType = "sum";
    sumfields3.onStatisticField = "EDAD1664"; //any one numeric field
    sumfields3.outStatisticFieldName = "sum_EDAD1664";    
    .
    .
    .
    .
    .
    myQuery.outStatistics = [sumfields1, sumfields2, sumfields3 .... ];
    

    This might solve your problem.

    Feel free to shoot your further queries.