How to define a renderer for your layer


Defining a renderer for your layer

FeatureLayers use feature renderers to define the symbology that the layer draws with. Defining the renderer for a layer is a common task that involves working with the layer's data source, display objects, and the specific renderer class in question.
This document shows how to set up a UniqueValueRenderer for a single field for a layer. The following code shows how to find the unique values as well as how to define a random color ramp to generate the renderer fill symbols. 
This renderer is written specifically to use fill symbols for polygon feature classes. The code can be modified to generate marker symbols for points and line symbols for polylines.
[Java]
static void defineUniqueValueRenderer(IGeoFeatureLayer geoFeatureLayer, String
    fieldName)throws Exception{

    IRandomColorRamp colorRamp = new RandomColorRamp();
    //Make the color ramp for the symbols in the renderer.
    colorRamp.setMinSaturation(20);
    colorRamp.setMaxSaturation(40);
    colorRamp.setMinValue(85);
    colorRamp.setMaxValue(100);
    colorRamp.setStartHue(76);
    colorRamp.setEndHue(188);
    colorRamp.setUseSeed(true);
    colorRamp.setSeed(43);

    //Make the renderer.
    IUniqueValueRenderer uniqueValueRenderer = new UniqueValueRenderer();

    ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbol();
    simpleFillSymbol.setStyle(esriSimpleFillStyle.esriSFSSolid);
    simpleFillSymbol.getOutline().setWidth(0.4);

    //These properties should be set prior to adding values.
    uniqueValueRenderer.setFieldCount(1);
    uniqueValueRenderer.setField(0, fieldName);
    uniqueValueRenderer.setDefaultSymbol((ISymbol)simpleFillSymbol);
    uniqueValueRenderer.setUseDefaultSymbol(true);

    IDisplayTable displayTable = (IDisplayTable)geoFeatureLayer;
    IFeatureCursor featureCursor = (IFeatureCursor)displayTable.searchDisplayTable
        (null, false);
    IFeature feature = featureCursor.nextFeature();


    boolean valueFound;
    int fieldIndex;

    IFields fields = featureCursor.getFields();
    fieldIndex = fields.findField(fieldName);
    while (feature != null){
        ISimpleFillSymbol classSymbol = new SimpleFillSymbol();
        classSymbol.setStyle(esriSimpleFillStyle.esriSFSSolid);
        classSymbol.getOutline().setWidth(0.4);

        String classValue = (String)feature.getValue(fieldIndex);

        //Test to see if this value was added
        //to the renderer. If not, add it.
        valueFound = false;
        for (int i = 0; i <= uniqueValueRenderer.getValueCount() - 1; i++){
            if (uniqueValueRenderer.getValue(i).equals(classValue)){
                valueFound = true;
                break; //Exit the loop if the value was found.
            }
        }
        //If the value was not found, it is new and it will be added.
        if (valueFound == false){
            uniqueValueRenderer.addValue(classValue, fieldName, (ISymbol)classSymbol)
                ;
            uniqueValueRenderer.setLabel(classValue, classValue);
            uniqueValueRenderer.setSymbol(classValue, (ISymbol)classSymbol);
        }

        feature = featureCursor.nextFeature();
    }
    //Since the number of unique values is known, 
    //the color ramp can be sized and the colors assigned.
    colorRamp.setSize(uniqueValueRenderer.getValueCount());
    boolean[] ok = new boolean[1];
    colorRamp.createRamp(ok);

    IEnumColors enumColors = colorRamp.getColors();
    enumColors.reset();
    for (int j = 0; j <= uniqueValueRenderer.getValueCount() - 1; j++){
        String xv = uniqueValueRenderer.getValue(j);
        if (!xv.equals("")){
            ISimpleFillSymbol pSimpleFillColor = (ISimpleFillSymbol)
                uniqueValueRenderer.getSymbol(xv);
            pSimpleFillColor.setColor(enumColors.next());
            uniqueValueRenderer.setSymbol(xv, (ISymbol)pSimpleFillColor);
        }
    }

    //'** If you didn't use a predefined color ramp
    //'** in a style, use "Custom" here. Otherwise,
    //'** use the name of the color ramp you selected.
    uniqueValueRenderer.setColorScheme("Custom");
    ITable pTable = (ITable)displayTable;
    boolean isString = pTable.getFields().getField(fieldIndex).getType() ==
        esriFieldType.esriFieldTypeString;
    uniqueValueRenderer.setFieldType(0, isString);
    geoFeatureLayer.setRendererByRef((IFeatureRenderer)uniqueValueRenderer);

    //This makes the layer properties symbology tab
    //show the correct interface.
    IUID uid = new UID();
    uid.setValue("{683C994E-A17B-11D1-8816-080009EC732A}");
    geoFeatureLayer.setRendererPropertyPageClassID((UID)uid);

}






Development licensing Deployment licensing
ArcView ArcView
ArcEditor ArcEditor
ArcInfo ArcInfo
Engine Developer Kit Engine Runtime