目录
分级着色:(导入命名空间:ESRI.ArcGIS.Geodatabase;)
简单着色:
IMap pMap = axMapControl1.Map;
//这里为面状图层为例
IGeoFeatureLayer pGeoFeatureLayer = pMap.get_Layer(1) as IGeoFeatureLayer;
//新建一个填充符号
IFillSymbol pSimpleFills;
pSimpleFills = new SimpleFillSymbolClass();
IRgbColor color = new RgbColorClass();
color.Red = 120;
color.Green = 110;
color.Blue = 0;
pSimpleFills.Color = color;
//新建线符号
ILineSymbol pLineSymbol = new SimpleLineSymbolClass();
color.Red = 255;
color.Green = 0;
color.Blue = 0;
pLineSymbol.Color = color;
pLineSymbol.Width = 3;
//线符号作为该填充符号的外边缘
pSimpleFills.Outline = pLineSymbol;
ISimpleRenderer pSimpleRenderer;
pSimpleRenderer = new SimpleRendererClass();
pSimpleRenderer.Symbol = (ISymbol)pSimpleFills;
//以POP1990字段作为要素透明设置的属性
ITransparencyRenderer pTransRenderer;
pTransRenderer = pSimpleRenderer as ITransparencyRenderer;
pTransRenderer.TransparencyField = "HousePrice";
pGeoFeatureLayer.Renderer = pTransRenderer as IFeatureRenderer;
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
分级着色:(导入命名空间:ESRI.ArcGIS.Geodatabase;)
IGeoFeatureLayer pGeoFeatureL;
ITable pTable;
IClassifyGEN pClassify;
ITableHistogram pTableHistogram;
IBasicHistogram pHistogram;
object dataFrequency;
object dataValues;
double[] Classes;
int ClassesCount;
IClassBreaksRenderer pClassBreaksRenderer;
IHsvColor pFromColor;
IHsvColor pToColor;
IAlgorithmicColorRamp pAlgorithmicCR;
IEnumColors pEnumColors;
bool ok;
IColor pColor;
ISimpleFillSymbol pSimpleFillS;
int lbreakIndex;
string strPopField = "PersonNum";
int numDesiredClasses = 5;
IMap pMap = axMapControl1.Map;
pMap.ReferenceScale = 0;
pGeoFeatureL = (IGeoFeatureLayer)pMap.get_Layer(1);
//从pTable的POP1990字段中得到信息给dataValues和 dataFrequency两个数组
pTable = (ITable)pGeoFeatureL.FeatureClass;
pTableHistogram = new BasicTableHistogramClass();
pHistogram = (IBasicHistogram)pTableHistogram;
pTableHistogram.Field = strPopField;
pTableHistogram.Table = pTable;
pHistogram.GetHistogram(out dataValues, out dataFrequency);
//下面是分级方法,用于根据获取的值计算出符合要求的数据
pClassify = new EqualIntervalClass();
try
{
pClassify.Classify(dataValues, dataFrequency, ref numDesiredClasses);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
//返回一个数组
Classes = (double[])pClassify.ClassBreaks;
ClassesCount = Classes.GetUpperBound(0);
pClassBreaksRenderer = new ClassBreaksRendererClass();
pClassBreaksRenderer.Field = strPopField;
//设置着色对象的分级数目
pClassBreaksRenderer.BreakCount = ClassesCount;
pClassBreaksRenderer.SortClassesAscending = true;
//产生分级着色需要的颜色带对象的起止颜色对象
pFromColor = new HsvColorClass();
pFromColor.Hue = 60;
pFromColor.Saturation = 100;
pFromColor.Value = 96;
pToColor = new HsvColorClass();
pToColor.Hue = 0;
pToColor.Saturation = 100;
pToColor.Value = 96;
//产生颜色带对象
pAlgorithmicCR = new AlgorithmicColorRampClass();
pAlgorithmicCR.Algorithm = esriColorRampAlgorithm.esriHSVAlgorithm;
pAlgorithmicCR.FromColor = pFromColor;
pAlgorithmicCR.ToColor = pToColor;
pAlgorithmicCR.Size = ClassesCount;
pAlgorithmicCR.CreateRamp(out ok);
//获得颜色
pEnumColors = pAlgorithmicCR.Colors;
//分类着色对象中的symbol和break的下标是从0开始
for (lbreakIndex = 0; lbreakIndex <= ClassesCount - 1; lbreakIndex++)
{
pColor = pEnumColors.Next();
pSimpleFillS = new SimpleFillSymbolClass();
pSimpleFillS.Color = pColor;
pSimpleFillS.Style = esriSimpleFillStyle.esriSFSSolid;
//这里是构造不同颜色着色的方法
pClassBreaksRenderer.set_Symbol(lbreakIndex, (ISymbol)pSimpleFillS);
//着色对象的断点
pClassBreaksRenderer.set_Break(lbreakIndex, Classes[lbreakIndex + 1]);
}
pGeoFeatureL.Renderer = (IFeatureRenderer)pClassBreaksRenderer;
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
唯一值着色:
IGeoFeatureLayer m_pGeoFeatureL;
IUniqueValueRenderer pUniqueValueR;
IFillSymbol pFillSymbol;
IColor pNextUniqueColor;
IEnumColors pEnumRamp;
ITable pTable;
int lfieldNumber;
IRow pNextRow;
IRowBuffer pNextRowBuffer;
ICursor pCursor;
IQueryFilter pQueryFilter;
string codeValue;
IRandomColorRamp pColorRamp;
string strNameField = "NAME99";
IMap pMap = axMapControl1.Map;
pMap.ReferenceScale = 0;
m_pGeoFeatureL = (IGeoFeatureLayer)pMap.get_Layer(1);
pUniqueValueR = new UniqueValueRendererClass();
pTable = (ITable)m_pGeoFeatureL;
lfieldNumber = pTable.FindField(strNameField);
if (lfieldNumber == -1)
{
MessageBox.Show("未能找到字段 " + strNameField);
return;
}
//只用一个字段进行单值着色
pUniqueValueR.FieldCount = 1;
//用于区分着色的字段
pUniqueValueR.set_Field(0, strNameField);
//产生一个随机的颜色条,用的是HSV颜色模式
pColorRamp = new RandomColorRampClass();
pColorRamp.StartHue = 0;
pColorRamp.MinValue = 99;
pColorRamp.MinSaturation = 15;
pColorRamp.EndHue = 360;
pColorRamp.MaxValue = 100;
pColorRamp.MaxSaturation = 30;
//任意产生100个颜色,如果知道要素的数目可以产生精确的颜色个数
pColorRamp.Size = 100;
bool ok = true;
pColorRamp.CreateRamp(out ok);
pEnumRamp = pColorRamp.Colors;
pNextUniqueColor = null;
//产生查询过滤器对象
pQueryFilter = new QueryFilterClass();
pQueryFilter.AddField(strNameField);
//根据某个字段在表中找出指向所有行的游标对象
pCursor = pTable.Search(pQueryFilter, true);
pNextRow = pCursor.NextRow();
//遍历所有的要素
while (pNextRow != null)
{
pNextRowBuffer = pNextRow;
//找出Row为“STATES_NAME”的值,即不同的州名
codeValue = (string)pNextRowBuffer.get_Value(lfieldNumber);
//获取随机颜色带中的任意一个颜色
pNextUniqueColor = pEnumRamp.Next();
if (pNextUniqueColor == null)
{
pEnumRamp.Reset();
pNextUniqueColor = pEnumRamp.Next();
}
pFillSymbol = new SimpleFillSymbolClass();
pFillSymbol.Color = pNextUniqueColor;
//将每次都得的要素字段值和修饰它的符号值放入着色对象中
pUniqueValueR.AddValue(codeValue, strNameField, (ISymbol)
pFillSymbol);
pNextRow = pCursor.NextRow();
}
m_pGeoFeatureL.Renderer = (IFeatureRenderer)pUniqueValueR;
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);