Calculate sum in several fields with StatisticsDefinition()

1243
6
03-28-2017 01:56 AM
AlbertoCañivano
New Contributor III

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;
0 Kudos
6 Replies
RobertScheitlin__GISP
MVP Emeritus

Alberto,

   What do you mean by it does not work? What error are you getting?

JohnGrayson
Esri Regular Contributor

Please note that 'outStatistics' is an array of StatisticDefinition instances, and 'onStatisticField' is the name of a single field. Try something like this:

var sumStats_1= new StatisticDefinition();
sumStats_1.statisticType = "sum";
sumStats_1.onStatisticField = "POB_TOT";
sumStats_1.outStatisticFieldName = "Sum_POB_TOT";

var sumStats_2= new StatisticDefinition();
sumStats_2.statisticType = "sum";
sumStats_2.onStatisticField = "EDAD0015";
sumStats_2.outStatisticFieldName = "Sum_EDAD0015";

var sumStats_3= new StatisticDefinition();
sumStats_3.statisticType = "sum";
sumStats_3.onStatisticField = "EDAD1664";
sumStats_3.outStatisticFieldName = "Sum_EDAD1664";

var sumStats_4= new StatisticDefinition();
sumStats_4.statisticType = "sum";
sumStats_4.onStatisticField = "EDAD65_";
sumStats_4.outStatisticFieldName = "Sum_EDAD65_";

var myQuery = new Query();
myQuery.where = "1 = 1";
myQuery.geometry = circle;
myQuery.spatialRelationship = Query.SPATIAL_REL_INTERSECTS;
myQuery.outStatistics = [sumStats_1,sumStats_2,sumStats_3,sumStats_4];‍‍‍‍‍‍‍‍‍‍‍

var myQueryTask = new QueryTask(_poblacion);
myQueryTask.execute(myQuery).then(...);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
AlbertoCañivano
New Contributor III

I will try. Thanks you both of you! 

0 Kudos
AlbertoCañivano
New Contributor III

Here is my attempt; almost done, but I have the Type Error Cannot set property 'innerHTML' of null; I don´t understand because var stats give me back values.

var myQueryTask = new QueryTask(_poblacion);
myQueryTask.execute(myQuery,show);

function show(results){
var stats = results.features[0].attributes;

dom.byId("pobtot").innerHTML = stats.Sum_POB_TOT;
dom.byId("menos15").innerHTML = stats.Sum_EDAD0015;
dom.byId("entre16_64").innerHTML = stats.Sum_EDAD1664;
dom.byId("mas65").innerHTML = stats.Sum_EDAD65_;
};

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Alberto,

   In templated widget development you do not normally use ids in the widget.html. The standard is to use a data-dojo-attach-point="xyz" in the Widget.html and in the Widget.js you use this.xyz.innerHTML = "blah blah";

AlbertoCañivano
New Contributor III

Oh thanks. I am learning bit by bit. Congrats for your publish widgets, Robert

0 Kudos