public IFeatureRenderer UnqueValueRenderer(IFeatureWorkspace workspace, IFeatureClass fc)
{
try
{
string tablename = ((IDataset)fc).BrowseName;
ITable ipTable = workspace.OpenTable(tablename);
IFeatureCursor featureCursor = fc.Search(null, false);
IDataStatistics dataStatistics = new DataStatisticsClass();
dataStatistics.Cursor = featureCursor as ICursor;
dataStatistics.Field = "gridcode";
System.Collections.IEnumerator enumerator = dataStatistics.UniqueValues;
enumerator.Reset();
while (enumerator.MoveNext())
{
object myObject = enumerator.Current;
}
int uniqueValues = dataStatistics.UniqueValueCount;
//Create colors for each unique value.
IRandomColorRamp colorRamp = new RandomColorRampClass();
colorRamp.Size = uniqueValues;
colorRamp.Seed = 100;
bool createColorRamp;
colorRamp.CreateRamp(out createColorRamp);
if (createColorRamp == false)
{
return null;
}
IUniqueValueRenderer uvRenderer = new UniqueValueRendererClass();
IFeatureRenderer featRenderer = (IFeatureRenderer)uvRenderer;
uvRenderer.FieldCount = 1;
ISimpleFillSymbol fillSymbol = new SimpleFillSymbolClass();
ISimpleFillSymbol fillSymbol2 = new SimpleFillSymbolClass();
if (ShowNonVisibleData == true)
{
fillSymbol.Color = new RgbColorClass() { Red = 255 } as IColor;
uvRenderer.AddValue("0", "", fillSymbol as ISymbol);
uvRenderer.set_Label("0", "Non-Visible");
}
fillSymbol2.Color = new RgbColorClass() { Green = 255 } as IColor;
uvRenderer.AddValue("1", "", fillSymbol2 as ISymbol);
uvRenderer.set_Label("1", "Visible by 1 Observer");
int field = ipTable.FindField("gridcode");
uvRenderer.set_Field(0, "gridcode");
for (int i = 2; i < uniqueValues; i++)
{
ISimpleFillSymbol newFillSymbol = new SimpleFillSymbolClass();
newFillSymbol.Color = colorRamp.get_Color(i);
uvRenderer.AddValue(i.ToString(), "", newFillSymbol as ISymbol);
string label = "Visible by " + i.ToString() + " Observers";
uvRenderer.set_Label(i.ToString(), label);
}
return featRenderer;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return null;
}
}