UniqueValueRenderer function attribute not working with a second attribute field

2983
2
12-14-2013 12:20 PM
CarisaSmith
New Contributor III
Let me know if I'm missing something here, but it seems to me that the JavaScript UniqueValueRenderer function option works fine if you just use one attributeField for your function, but if you have one function and another attributeField as an actual attributeField.  The function no longer is called.  Two attribute fields only work fine as well.  I realize the first attributeField is the only one that appears to be open for the use of a function call and that is which field I'm using, yet also using attributeField2 as an attributeField from the Feature Class.

Modifying ESRI's example doesn't seem to work either (modified code below to use UniqueValueRenderer & pick three values I know exist from their sample):
Note: the code below works if I put in the values of "M163_07" instead of function calculateSquareMiles

https://developers.arcgis.com/en/javascript/jssamples/renderer_function.html

    var map, calculateSquareMiles;
      require([
        "esri/map", "esri/layers/FeatureLayer", "esri/InfoTemplate",
        "esri/symbols/SimpleFillSymbol", "esri/symbols/SimpleLineSymbol", "esri/renderers/UniqueValueRenderer",
        "esri/dijit/Legend",
        "dojo/_base/Color", "dojo/number", "dojo/domReady!"
      ], function(
        Map, FeatureLayer, InfoTemplate,
        SimpleFillSymbol, SimpleLineSymbol, UniqueValueRenderer,
        Legend,
        Color, number
      ) {
        map = new Map("map", {
          basemap: "gray",
          center: [-89.849, 40.369],
          zoom: 6
        });

        // convert acres to square miles
        // also used by the feature layer's info template
        // test for presence to a property named "attributes" to
        // determine whether or the "value" argument is a graphic or number
        calculateSquareMiles = function(value) {
          var acres = (value.hasOwnProperty("attributes")) ? value.attributes.M163_07 : value;
          return number.format(acres / 640, { places: 2 });;
        }
       
        var symbol = new SimpleFillSymbol("solid", null, null);
        var renderer = new UniqueValueRenderer(null, calculateSquareMiles, "STATE", null, ":");
        //renderer.setMaxInclusive(true);
        var color1 = new Color([247, 252, 185]); // yellow
        var color2 = new Color([173, 221, 142]); // light green
        var color3 = new Color([49, 163, 84]); // green
        renderer.addValue("4.67:MO",new SimpleFillSymbol(  //"2987:MO"
            "solid",
            new SimpleLineSymbol("solid", color1, 1),
            color1
          ));
        renderer.addValue("39.83:MO",new SimpleFillSymbol( //"25491:MO"
            "solid",
            new SimpleLineSymbol("solid", color2, 1),
            color2
          ));
        renderer.addValue("252.41:IA", new SimpleFillSymbol( //"161542:IA"
            "solid",
            new SimpleLineSymbol("solid", color3, 1),
            color3
          ));
 
        layer.setRenderer(renderer);
        map.addLayers([layer]);
0 Kudos
2 Replies
ArtemisFili
Esri Contributor
If you want to use a function, there is no need to specify one or more attribute fields when creating the renderer. You should only pass two args to the unique value renderer constructor like so:

        var renderer = new UniqueValueRenderer(null, calculateSquareMiles);

You can then access whichever attributes you like from your function, like this:

        calculateSquareMiles = function(value) {
          var acres;
          if ( value.hasOwnProperty("attributes") ) {
            acres = value.attributes.M163_07;
            acres = number.format(acres / 640, { places: 2 });
            acres = acres + ":" + value.attributes.STATE;
          } else {
            acres = value;
          }
          return acres;
        }
0 Kudos
CarisaSmith
New Contributor III
Thanks, Artemis... I actually do have a need for the unique value renderer to use the function and base which icon I use on a different attribute field as well, possibly even a third attribute field.  The example I gave you doesn't explain what I really want to do.  I was trying to determine which image to use based on if the heading of a vehicle was > 180 or not (so the vehicle didn't look upside down on the map) and then also group by a vehicle grouping attribute.  I realize I could put this all into a function and then display the results, yet many different variations could occur and could be a bit of a mess.  I'll either go that route or change my vehicle image to the top view of the truck, so I can then just use the renderer.setRotationInfo() method instead of my own function.

I told ESRI support to change their documentation saying if you use a function with the UniqueValueRenderer, you don't have the option to use the attributeField2 and attributeField3 options.  To make things more clear.  Thanks for the help!

https://developers.arcgis.com/en/javascript/jsapi/uniquevaluerenderer-amd.html
0 Kudos