0

I have been able to select points and find the sum of the points. What I really need is to average the points. Here is my (acquired from someone else) code for finding the sum.

define([
'dojo/_base/declare',
'dijit/_WidgetBase',
'dijit/_TemplatedMixin',
'dijit/_WidgetsInTemplateMixin',
'esri/toolbars/draw',
'esri/InfoTemplate',
'esri/layers/FeatureLayer',
'esri/symbols/SimpleMarkerSymbol',
'esri/symbols/SimpleLineSymbol',
'esri/Color',
'esri/tasks/query',
'dijit/form/Button',
'dojo/_base/lang',
'dojo/on',
'dojo/_base/array',
'dojo/dom',
'dojo/text!./Select/templates/Select.html',
'xstyle/css!./Select/css/Select.css'
], function (declare, _WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin, Draw, InfoTemplate, FeatureLayer, SimpleMarkerSymbol, SimpleLineSymbol, Color, Query, Button, lang, on, arrayUtil, dom, SelectTemplate, css) {
    return declare([_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin], {
        widgetsInTemplate: true,
        templateString: SelectTemplate,
        selectTools: null,

        postCreate: function () {
            this.selectTools = new Draw(this.map);

            this.selectQuery = new Query();

            on(this.selectTools, "DrawEnd", lang.hitch(this, function (geometry) {
                this.selectTools.deactivate();
                this.selectQuery.geometry = geometry;

                this.featureLayer.selectFeatures(this.selectQuery,
                FeatureLayer.SELECTION_NEW);
            }));
this.fieldsSelectionSymbol =
             new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_CIRCLE, 10,
new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
new Color([255,0,0]), 1),
new Color([0,255,0,0.25]));


            var infoTemplate = new InfoTemplate(this.title, this.content);

            this.featureLayer = new FeatureLayer(this.featureLayerURL,
            {
                mode: FeatureLayer.MODE_ONDEMAND,
                infoTemplate: infoTemplate,
                outFields: ["*"]
            });

            this.featureLayer.setDefinitionExpression(this.defExpress);
            this.featureLayer.setSelectionSymbol(this.fieldsSelectionSymbol);
            this.featureLayer.on("selection-complete", lang.hitch(this, 'sumGasProduction'));
            var clearMsg = this.clearSelectMsg;
            this.featureLayer.on("selection-clear", function () {
                dom.byId('messages').innerHTML = "<i>" + clearMsg + "</i>";
            });

            this.map.addLayer(this.featureLayer);

        },

        select: function () {
            this.selectTools.activate(Draw.EXTENT);
        },
        clearSelection: function () {
            this.featureLayer.clearSelection();
        },


        sumGasProduction: function (event) {
                        var productionSum = 0;
                        var sumField = this.fieldToSum;

                        arrayUtil.forEach(event.features, function (feature) {
                            productionSum += feature.attributes[sumField];

                        });
                        dom.byId('messages').innerHTML = "<b>" + this.selectSumMsgPrefix + " " + productionSum + " " + this.selectSumUnits + ". </b>";
                    }
    });
});

Secondly my clear selection button is not working.


Here is my code for clear selection:

<div data-dojo-attach-point="clearSelection" class="clearSelection">

    <button data-dojo-type="dijit/form/Button"  data-dojo-attach-event="onClick:clearSelection" >
        Clear your Selection
    </button>
</div>

I have no idea how to get the average. I was trying to use what you said in another answer but I could not figure it out. I have found an example that does find the average

https://developers.arcgis.com/javascript/jssamples/query_bypoly.html

      function average(fset, att) {
    var features = fset.features;
    var sum = 0;
    var featuresLength = features.length;
    for (var x = 0; x < featuresLength; x++) {
      sum = sum + features[x].attributes[att];
    }
    return Math.round(sum/featuresLength);
  }
  • 1
    I provided an answer for calculating the average in your previous question – tmcgee Jan 9 '15 at 19:23
  • Yes I saw that and am grateful! But was unable to get that to work. I kept getting an error of " Uncaught TypeError: Cannot read property 'length' of undefined" – Fiter Jan 9 '15 at 19:41
  • 1
    please show your complete code where you are attempting to calculate the average. – tmcgee Jan 12 '15 at 15:13
1

You did not provide the template HTML for your button so this is just a guess as to the issue. Your button needs an 'onClick' event to execute your 'clearSelection' method. Something like this:

data-dojo-attach-event="onClick:clearSelection"

To display an average instead of a sum, you can use the original method you posted with a slight variation:

avgGasProduction: function (event) {
    var productionSum = 0, productionAvg = 0;
    var sumField = this.fieldToSum;

    arrayUtil.forEach(event.features, function (feature) {
        productionSum += feature.attributes[sumField];
    });

    if (event.features && event.features.length > 0) {
        productionAvg = Math.round(productionSum / event.features.length)
    }

    dom.byId('messages').innerHTML = "<b>" + this.selectSumMsgPrefix + " " + productionAvg + " " + this.selectSumUnits + ". </b>";
}
  • I updated the original question to include the HTML for the clearSelection button – Fiter Jan 9 '15 at 19:44
  • Using chrome, do you get any error messages in the browser console? Add a console.log('test') statement to your clearSelection method to verify that the method is actually being called from the button click. – tmcgee Jan 12 '15 at 15:15
  • I keep getting the error: "Uncaught TypeError: undefined is not a function" – Fiter Jan 12 '15 at 15:16
  • The HTML code for the button you posted is in your template file "./Select/templates/Select.html"? – tmcgee Jan 12 '15 at 15:33
  • Yes, It is in the same place that I have the code to find the sum of the points and that button works. – Fiter Jan 12 '15 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.