/// <summary>
/// Defining a renderer for your layer
/// </summary>
/// <param name="pGeoFeatureLayer">图层</param>
/// <param name="fieldName">sde数据库中字段名</param>
/// <param name="pQueryFilter">过滤条件</param>
private void DefineUniqueValueRenderer(IGeoFeatureLayer pGeoFeatureLayer, string fieldName, IQueryFilter pQueryFilter)
{
// IRandomColorRamp 对象
IRandomColorRamp pRandomColorRamp = new RandomColorRampClass();
//Make 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;
//Make 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 对象
IFeatureCursor pFeatureCursor = pDisplayTable.SearchDisplayTable(pQueryFilter, false) as IFeatureCursor;
//IFeature对象
IFeature pFeature = pFeatureCursor.NextFeature();
//bool对象,pUniqueValueRenderer中是否找到字段值
bool ValFound;
//字段索引号
int fieldIndex;
//IFields对象
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 is new and it 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;
//ok
bool bOK;
pRandomColorRamp.CreateRamp(out bOK);
//IEnumColors 对象, 颜色集合
IEnumColors pEnumColors = pRandomColorRamp.Colors;
pEnumColors.Reset();
for (int j = 0; j <= pUniqueValueRenderer.ValueCount - 1; j++)
{
//value值
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对象
ITable pTable = pDisplayTable as ITable;
//是否string
bool isString = pTable.Fields.get_Field(fieldIndex).Type == esriFieldType.esriFieldTypeString;
pUniqueValueRenderer.set_FieldType(0, isString);
pGeoFeatureLayer.Renderer = pUniqueValueRenderer as IFeatureRenderer;
}