About defining a renderer for a layer
Feature layers use feature renderers to define the symbology with which the layer draws. Defining the renderer for a layer is a common task that involves working with the layer's data source and display objects, as well as the specific renderer class in question.
The following code example shows how to set up a unique value renderer for a single field for a layer, find the unique values, and 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.
static void DefineUniqueValueRenderer(IGeoFeatureLayer pGeoFeatureLayer, string
fieldName)
{
IRandomColorRamp pRandomColorRamp = new RandomColorRampClass();
//Create the color ramp for the symbols in the renderer.
pRandomColorRamp.MinSaturation = 20;
pRandomColorRamp.MaxSaturation = 40;
pRandomColorRamp.MinValue = 85;
pRandomColorRamp.MaxValue = 100;
pRandomColorRamp.StartHue = 76;
pRandomColorRamp.EndHue = 188;
pRandomColorRamp.UseSeed = true;
pRandomColorRamp.Seed = 43;
//Create the renderer.
IUniqueValueRenderer pUniqueValueRenderer = new UniqueValueRendererClass();
ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbolClass();
pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;
pSimpleFillSymbol.Outline.Width = 0.4;
//These properties should be set prior to adding values.
pUniqueValueRenderer.FieldCount = 1;
pUniqueValueRenderer.set_Field(0, fieldName);
pUniqueValueRenderer.DefaultSymbol = pSimpleFillSymbol as ISymbol;
pUniqueValueRenderer.UseDefaultSymbol = true;
IDisplayTable pDisplayTable = pGeoFeatureLayer as IDisplayTable;
IFeatureCursor pFeatureCursor = pDisplayTable.SearchDisplayTable(null, false)as
IFeatureCursor;
IFeature pFeature = pFeatureCursor.NextFeature();
bool ValFound;
int fieldIndex;
IFields pFields = pFeatureCursor.Fields;
fieldIndex = pFields.FindField(fieldName);
while (pFeature != null)
{
ISimpleFillSymbol pClassSymbol = new SimpleFillSymbolClass();
pClassSymbol.Style = esriSimpleFillStyle.esriSFSSolid;
pClassSymbol.Outline.Width = 0.4;
string classValue;
classValue = pFeature.get_Value(fieldIndex)as string;
//Test to see if this value was added to the renderer. If not, add it.
ValFound = false;
for (int i = 0; i <= pUniqueValueRenderer.ValueCount - 1; i++)
{
if (pUniqueValueRenderer.get_Value(i) == classValue)
{
ValFound = true;
break; //Exit the loop if the value was found.
}
}
//If the value was not found, it's new and will be added.
if (ValFound == false)
{
pUniqueValueRenderer.AddValue(classValue, fieldName, pClassSymbol as
ISymbol);
pUniqueValueRenderer.set_Label(classValue, classValue);
pUniqueValueRenderer.set_Symbol(classValue, pClassSymbol as ISymbol);
}
pFeature = pFeatureCursor.NextFeature();
}
//Since the number of unique values is known, the color ramp can be sized and the colors assigned.
pRandomColorRamp.Size = pUniqueValueRenderer.ValueCount;
bool bOK;
pRandomColorRamp.CreateRamp(out bOK);
IEnumColors pEnumColors = pRandomColorRamp.Colors;
pEnumColors.Reset();
for (int j = 0; j <= pUniqueValueRenderer.ValueCount - 1; j++)
{
string xv;
xv = pUniqueValueRenderer.get_Value(j);
if (xv != "")
{
ISimpleFillSymbol pSimpleFillColor = pUniqueValueRenderer.get_Symbol(xv)
as ISimpleFillSymbol;
pSimpleFillColor.Color = pEnumColors.Next();
pUniqueValueRenderer.set_Symbol(xv, pSimpleFillColor as ISymbol);
}
}
//'** 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.
pUniqueValueRenderer.ColorScheme = "Custom";
ITable pTable = pDisplayTable as ITable;
bool isString = pTable.Fields.get_Field(fieldIndex).Type ==
esriFieldType.esriFieldTypeString;
pUniqueValueRenderer.set_FieldType(0, isString);
pGeoFeatureLayer.Renderer = pUniqueValueRenderer as IFeatureRenderer;
//This makes the layer properties symbology tab show the correct interface.
IUID pUID = new UIDClass();
pUID.Value = "{683C994E-A17B-11D1-8816-080009EC732A}";
pGeoFeatureLayer.RendererPropertyPageClassID = pUID as UIDClass;
}
[VB.NET]
Shared Sub DefineUniqueValueRenderer(ByVal pGeoFeatureLayer As IGeoFeatureLayer, ByVal fieldName As String)
Dim pRandomColorRamp As IRandomColorRamp = New RandomColorRampClass()
'Create the color ramp for the symbols in the renderer.
pRandomColorRamp.MinSaturation = 20
pRandomColorRamp.MaxSaturation = 40
pRandomColorRamp.MinValue = 85
pRandomColorRamp.MaxValue = 100
pRandomColorRamp.StartHue = 76
pRandomColorRamp.EndHue = 188
pRandomColorRamp.UseSeed = True
pRandomColorRamp.Seed = 43
'Create the renderer.
Dim pUniqueValueRenderer As IUniqueValueRenderer = New UniqueValueRendererClass()
Dim pSimpleFillSymbol As ISimpleFillSymbol = New SimpleFillSymbolClass()
pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid
pSimpleFillSymbol.Outline.Width = 0.4
'These properties should be set prior to adding values.
pUniqueValueRenderer.FieldCount = 1
pUniqueValueRenderer.Field(0) = fieldName
pUniqueValueRenderer.DefaultSymbol = pSimpleFillSymbol
pUniqueValueRenderer.UseDefaultSymbol = True
Dim pDisplayTable As IDisplayTable = pGeoFeatureLayer
Dim pFeatureCursor As IFeatureCursor = pDisplayTable.SearchDisplayTable(Nothing, False)
Dim pFeature As IFeature = pFeatureCursor.NextFeature()
Dim ValFound As Boolean
Dim fieldIndex As Integer
Dim pFields As IFields = pFeatureCursor.Fields
fieldIndex = pFields.FindField(fieldName)
While Not pFeature Is Nothing
Dim pClassSymbol As ISimpleFillSymbol = New SimpleFillSymbolClass()
pClassSymbol.Style = esriSimpleFillStyle.esriSFSSolid
pClassSymbol.Outline.Width = 0.4
Dim classValue As String
classValue = pFeature.Value(fieldIndex)
'Test to see if this value was added to the renderer. If not, add it.
ValFound = False
Dim i As Integer
For i = 0 To pUniqueValueRenderer.ValueCount - 1 Step i + 1
If pUniqueValueRenderer.Value(i) = classValue Then
ValFound = True
Exit For 'Exit the loop if the value is found.
As break
End If
Next
'If the value was not found, it's new and will be added.
If ValFound = False Then
pUniqueValueRenderer.AddValue(classValue, fieldName, pClassSymbol)
pUniqueValueRenderer.Label(classValue) = classValue
pUniqueValueRenderer.Symbol(classValue) = pClassSymbol
End If
pFeature = pFeatureCursor.NextFeature()
End While
'Since the number of unique values is known, the color ramp can be sized and the colors assigned.
pRandomColorRamp.Size = pUniqueValueRenderer.ValueCount
Dim bOK As Boolean
pRandomColorRamp.CreateRamp( bOK)
Dim pEnumColors As IEnumColors = pRandomColorRamp.Colors
pEnumColors.Reset()
Dim j As Integer
For j = 0 To pUniqueValueRenderer.ValueCount - 1 Step j + 1
Dim xv As String
xv = pUniqueValueRenderer.Value(j)
If xv <> "" Then
Dim pSimpleFillColor As ISimpleFillSymbol = pUniqueValueRenderer.Symbol(xv)
pSimpleFillColor.Color = pEnumColors.Next()
pUniqueValueRenderer.Symbol(xv) = pSimpleFillColor
End If
Next
''** 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.
pUniqueValueRenderer.ColorScheme = "Custom"
Dim pTable As ITable = pDisplayTable
Dim isString As Boolean = pTable.Fields.Field(fieldIndex).Type = esriFieldType.esriFieldTypeString
pUniqueValueRenderer.FieldType(0) = isString
pGeoFeatureLayer.Renderer = pUniqueValueRenderer
'This makes the layer properties symbology tab show the correct interface.
Dim pUID As IUID = New UIDClass()
pUID.Value = "{683C994E-A17B-11D1-8816-080009EC732A}"
pGeoFeatureLayer.RendererPropertyPageClassID = pUID
End Sub
Development licensing | Deployment licensing |
---|---|
ArcGIS for Desktop Basic | ArcGIS for Desktop Basic |
ArcGIS for Desktop Standard | ArcGIS for Desktop Standard |
ArcGIS for Desktop Advanced | ArcGIS for Desktop Advanced |
Engine Developer Kit | Engine |