2

I'm trying to use ClassBreak renderer in ArcGIS API v3.27. I have a dynamic layer with ZIP as one of its fields. I would like to add breaks based on the ZIP value. But the class breaks usually don't follow a format. For example if i have 1 to 100 ZIPs ( just for ease of explanation) ZIP 1,2,5 might be class break 1 where as 3,6,7 might be class break 2 and so on. How do i use these values for class breaks as they don't fall in a particular range.

Below is the code:

crFeatureLayer.on("load", function(){

            var renderer = createRenderer();
            var optionsArray = [];
            var drawingOptions = new LayerDrawingOptions();
            drawingOptions.renderer = renderer;
            optionsArray[0] = drawingOptions;
            crFeatureLayer.setLayerDrawingOptions(optionsArray);

        });

var createRenderer = function() {

        var calculateColorValue = function(value) {
            if (value.hasOwnProperty("attributes"))
                var ZipCR = value.attributes.ZipCR;

            ///Some logic to retrieve class breaks will be inserted here
         }

        var renderer = new ClassBreaksRenderer(null, calculateColorValue);
        renderer.setMaxInclusive(true);
        for (var i = 0; i < ntaData.length; i++)
        {
            addNtaClassBreak(renderer, i);  
        }

        return renderer;
    };

     var addNtaClassBreak = function(renderer, index) {
        renderer.addBreak({
            minValue: index,
            maxValue: index+1,
            label: null,
            symbol: new SimpleFillSymbol().setColor(colorsAvailable[index])
        });
    };

The value of the ZipCR is always undefined. How to pass the layer info to the function in case of Dynamic layer. If i use the same code for generating the rendered and apply it to a Feature Layer instead of a Dynamic layer it works like a charm.

1

The Class Breaks Renderer can accept a function rather than the field name, see the example here, which uses the custom function called calculateSquareMiles.

Below is code to illustrate your case:

yourCustomFunction = function(value) {
  if (value.attributes.ZIP == '1,2,5') { 
     return 1;
  }
  else if (value.attributes.ZIP == '3,6,7') { 
     return 2;
  }
};

var renderer = new ClassBreaksRenderer(null, yourCustomFunction);
renderer.addBreak(0, 1, new SimpleFillSymbol().setColor(new Color([56, 168, 0, 0.5])));
renderer.addBreak(1, 2, new SimpleFillSymbol().setColor(new Color([139, 209, 0, 0.5])));
  • Thanks. Will work on that. – Ravi Mar 15 at 18:22
  • This worked well for a normal feature layer but when i'm trying it with a Dynamic Layer, the value of the 'value' variable in the custom function is undefined – Ravi Mar 18 at 15:29
  • The issue here i think is that while defining the renderer for a Dynamic Layer, we create and define the renderer and then assign it to particular layer through drawing options unlike for a Feature Layer. Hence its not able to get to the attributes while creating the renderer. – Ravi Mar 18 at 15:37

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.