小生原文
ArcEngine提供多个着色对象用于产生专题图,可以使用标准着色方案,也可以自定义着色方案,ArcEngine提供8中标准着色方案。
一、SimpleRenderer专题图
是使用单一符号进行着色分类,不涉及对要素的数据进行处理。这种专题图同一个图层内的所有元素都是一种符号。
通过SimpleRenderer对象对Symbol进行设置后,赋予IGeoFeatureLayer接口的Renderer属性,对象实现了ITransparencyRenerer接口,通过该接口的属性,可以根据要素的某一个数值字段来设置要素显示的透明度。
该对象还实现了ISimpleRenderer接口,提供了两个重要的属性:Description和Label这两个属性用来设置图例。
////// 简单渲染 /// /// 图层名字 /// FillStyle /// FillColor /// OutLineColor /// 样式名称注释 /// 描述 public void createSimpleFillSymbol(string layerName,esriSimpleFillStyle FillStyle,IColor pColor,IColor OutLineColor,string RenderLabel,string Descripition) { //简单填充符号 ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass(); //可以用符号选择器进行 simpleFillSymbol.Style = FillStyle; simpleFillSymbol.Color = pColor; //创建边线符号 ISimpleLineSymbol simpleLineSymbol = new SimpleLineSymbolClass(); simpleLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid; simpleLineSymbol.Color = OutLineColor; ISymbol symbol = simpleLineSymbol as ISymbol; symbol.ROP2 = esriRasterOpCode.esriROPNotXOrPen; simpleFillSymbol.Outline = simpleLineSymbol; ISimpleRenderer simpleRender = new SimpleRendererClass(); simpleRender.Symbol = simpleFillSymbol as ISymbol; simpleRender.Label = RenderLabel; simpleRender.Description = Descripition; IGeoFeatureLayer geoFeatureLayer; geoFeatureLayer = getGeoLayer(layerName); if (geoFeatureLayer != null) { geoFeatureLayer.Renderer = simpleRender as IFeatureRenderer; } }
二、ClassBreakRenderer专题图
为分级专题图,通过要素图层中要素的某个数值字段的值,根据用户要求,将这些值分为多个级别,每个级别用不同的Symbol显示。
该对象实现了IClassBreakRenderer接口,提供了实现分级显示的属性和方法,如Field属性用于设置分级着色的字段,BreakCount属性用于设置分级的数目。
////// 分级渲染 /// /// 图层名 /// 分级数目 /// 分级字段 /// 填充样式 public void createClassBreakRender(string layerName, int classCount, string ClassField, esriSimpleFillStyle FillStyle) { //int classCount = 6; ITableHistogram tableHistogram;//表格直方图 IBasicHistogram basicHistogram;//Provides access to members that control histogram objects created from different data sources. ITable table; IGeoFeatureLayer geoFeatureLayer; geoFeatureLayer = getGeoLayer(layerName); ILayer layer = geoFeatureLayer as ILayer; table = layer as ITable; tableHistogram = new BasicTableHistogramClass(); //按照 数值字段分级 tableHistogram.Table = table; tableHistogram.Field = ClassField; basicHistogram = tableHistogram as IBasicHistogram; object values; object frequencys; //先统计每个值和各个值出现的次数 basicHistogram.GetHistogram(out values, out frequencys); //创建平均分级对象 IClassifyGEN classifyGEN = new QuantileClass(); //用统计结果进行分级 ,级别数目为classCount classifyGEN.Classify(values, frequencys, ref classCount); //获得分级结果,是个 双精度类型数组 double[] classes; classes = classifyGEN.ClassBreaks as double[]; //定义不同等级渲染的色带用色 IEnumColors enumColors = CreateAlgorithmicColorRamp(classes.Length).Colors; IColor color; IClassBreaksRenderer classBreaksRenderer = new ClassBreaksRendererClass(); classBreaksRenderer.Field = ClassField; classBreaksRenderer.BreakCount = classCount;//分级数目 classBreaksRenderer.SortClassesAscending = true;//定义分类是否在TOC中显示Legend ISimpleFillSymbol simpleFillSymbol; for (int i = 0; i < classes.Length - 1; i++) { color = enumColors.Next(); simpleFillSymbol = new SimpleFillSymbolClass(); simpleFillSymbol.Color = color; simpleFillSymbol.Style = FillStyle; classBreaksRenderer.set_Symbol(i, simpleFillSymbol as ISymbol); classBreaksRenderer.set_Break(i, classes[i]); } if (geoFeatureLayer != null) { geoFeatureLayer.Renderer = classBreaksRenderer as IFeatureRenderer; } }
三、UniqueValueRenderer专题图
是依据要素图层中的某个字段的不同值,给每个要素一个单独的颜色,以区分存在的每一个要素。
UniqueValueRenderer实现了IUniqueValueRenderer接口,提供了各种属性和方法,如AddValue方法用于将单个要素的某个字段值和与之相匹配的着色符号加入到UniqueValueRenderer对象。
////// 单一值渲染(单字段) /// /// 图层名 /// 渲染字段 /// 填充样式 /// 字段的唯一值个数 public void createUniqueValueRander(string layerName, string RenderField, esriSimpleFillStyle FillStyle,int valueCount) { IGeoFeatureLayer geoFeatureLayer; geoFeatureLayer = getGeoLayer(layerName); IUniqueValueRenderer uniqueValueRenderer = new UniqueValueRendererClass(); //可以设置多个字段 uniqueValueRenderer.FieldCount = 1;//0-3个 uniqueValueRenderer.set_Field(0, RenderField); //简单填充符号 ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass(); simpleFillSymbol.Style = FillStyle; IFeatureCursor featureCursor = geoFeatureLayer.FeatureClass.Search(null, false); IFeature feature; if (featureCursor != null) { IEnumColors enumColors = CreateAlgorithmicColorRamp(valueCount).Colors; int fieldIndex = geoFeatureLayer.FeatureClass.Fields.FindField(RenderField); for (int i = 0; i < valueCount; i++) { feature = featureCursor.NextFeature(); string nameValue = feature.get_Value(fieldIndex).ToString(); simpleFillSymbol = new SimpleFillSymbolClass(); simpleFillSymbol.Color = enumColors.Next(); uniqueValueRenderer.AddValue(nameValue, RenderField, simpleFillSymbol as ISymbol); } } geoFeatureLayer.Renderer = uniqueValueRenderer as IFeatureRenderer; } ////// 单一值渲染(多字段) /// /// 图层名 /// 多字段名 /// 样式 /// 每个字段中唯一值的个数 public void createUniqueValueRander(string layerName, string[] RenderField, esriSimpleFillStyle FillStyle, int[] valueCount) { IGeoFeatureLayer geoFeatureLayer; geoFeatureLayer = getGeoLayer(layerName); IUniqueValueRenderer uniqueValueRenderer = new UniqueValueRendererClass(); //可以设置多个字段 uniqueValueRenderer.FieldCount = RenderField .Length;//0-3个 for (int i = 0; i < RenderField.Length; i++) { uniqueValueRenderer.set_Field(i, RenderField[i]); } //简单填充符号 ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass(); simpleFillSymbol.Style = FillStyle; IFeatureCursor featureCursor = geoFeatureLayer.FeatureClass.Search(null, false); IFeature feature; if (featureCursor != null) { for (int i = 0; i < RenderField.Length; i++) { IEnumColors enumColors = CreateAlgorithmicColorRamp(valueCount[i]).Colors; int fieldIndex = geoFeatureLayer.FeatureClass.Fields.FindField(RenderField[i]); for (int j = 0; j < valueCount[i]; j++) { feature = featureCursor.NextFeature(); string nameValue = feature.get_Value(fieldIndex).ToString(); simpleFillSymbol = new SimpleFillSymbolClass(); simpleFillSymbol.Color = enumColors.Next(); uniqueValueRenderer.AddValue(nameValue, RenderField[i], simpleFillSymbol as ISymbol); } } } geoFeatureLayer.Renderer = uniqueValueRenderer as IFeatureRenderer; }
四、ProportionalSymbolRenderer专题图
使用梯度着色法制作的专题图。
ProportionalSymbolRenderer实现了IProportionalSymbolRenderer接口。
这种着色法需要用户知道最大和最小点各自代表的字段值、着色基于的字段和着色点使用的符号以及它在Legend要出现的级别数目。
////// 梯度着色 /// /// 图层名字 /// 要参考的字段 /// 填充样式 /// 填充Color /// 特征点符号 /// 参考单位 /// 要分成的级数 public void createProportionSymbolRender(string layerName, string ProportionField, esriSimpleFillStyle FillStyle, IColor fillColor, ICharacterMarkerSymbol characterMarkerSymbol, esriUnits units, int pLegendSymbolCount) { IGeoFeatureLayer geoFeatureLayer; IFeatureLayer featureLayer; IProportionalSymbolRenderer proportionalSymbolRenderer; ITable table; ICursor cursor; IDataStatistics dataStatistics;//用一个字段生成统计数据 IStatisticsResults statisticsResult;//报告统计数据 //stdole.IFontDisp fontDisp;//定义字体 geoFeatureLayer = getGeoLayer(layerName); featureLayer = geoFeatureLayer as IFeatureLayer; table = geoFeatureLayer as ITable; cursor = table.Search(null, true); dataStatistics = new DataStatisticsClass(); dataStatistics.Cursor = cursor; dataStatistics.Field = ProportionField;//确定分级字段 statisticsResult = dataStatistics.Statistics; if (statisticsResult != null) { IFillSymbol fillSymbol = new SimpleFillSymbolClass(); fillSymbol.Color = fillColor; //ICharacterMarkerSymbol characterMarkerSymbol = new CharacterMarkerSymbolClass(); //fontDisp = new stdole.StdFontClass() as stdole.IFontDisp; //fontDisp.Name = "arial"; //fontDisp.Size = 20; //characterMarkerSymbol.Font = fontDisp; //characterMarkerSymbol.CharacterIndex = 90; //characterMarkerSymbol.Color = getRGB(255, 0, 0); //characterMarkerSymbol.Size = 8; proportionalSymbolRenderer = new ProportionalSymbolRendererClass(); proportionalSymbolRenderer.ValueUnit = units; proportionalSymbolRenderer.Field = ProportionField ; proportionalSymbolRenderer.FlanneryCompensation = false;//分级是不是在TOC中显示legend proportionalSymbolRenderer.MinDataValue = statisticsResult.Minimum; proportionalSymbolRenderer.MaxDataValue = statisticsResult.Maximum; proportionalSymbolRenderer.BackgroundSymbol = fillSymbol; proportionalSymbolRenderer.MinSymbol = characterMarkerSymbol as ISymbol; proportionalSymbolRenderer.LegendSymbolCount = pLegendSymbolCount;//要分成的级数 proportionalSymbolRenderer.CreateLegendSymbols(); geoFeatureLayer.Renderer = proportionalSymbolRenderer as IFeatureRenderer; } }
五、ChartRenderer专题图
使用一个饼图或柱图来表示一个要素的多个属性。有水平排列和累计排列两种。
ChartRenderer对象实现了IChartRenderer接口,其中ChartSymbol方法用于设置着色对象的着色符号,Label属性用于设置Legend的标签。
该方法用于比较一个要素中不同属性,在获得着色图层的单个或多个字段时用RendererField对象来操作。该类实现了IRendererField接口,可以通过AddField方法来添加字段。
该着色法是使用饼图来表现要素的多个属性之间的比率关系。该对象实现了IPicChartRenderer接口,使用PieChartSymbol符号来修饰要素。
////// 条状图渲染 /// /// 图层名字 /// 要柱状表示的字段 /// 这些字段分别需要渲染的颜色 /// 每个柱子的宽度 /// 背景色 public void createBarChartSymbol(string layerName, string[] RenderField, IColor[] fillsymbolColor,double BarWidth,IColor BgColor) { IGeoFeatureLayer geoFeatureLayer; IFeatureLayer featureLayer; ITable table; ICursor cursor; IRowBuffer rowBuffer; //获取渲染图层 geoFeatureLayer = getGeoLayer(layerName); featureLayer = geoFeatureLayer as IFeatureLayer; table = featureLayer as ITable; geoFeatureLayer.ScaleSymbols = true; IChartRenderer chartRenderer = new ChartRendererClass(); IRendererFields rendererFields = chartRenderer as IRendererFields; for (int i = 0; i < RenderField.Length; i++) { rendererFields.AddField(RenderField[i], RenderField[i]); } //int[] fieldIndexs = new int[2]; //fieldIndexs[0] = table.FindField(field1); //fieldIndexs[1] = table.FindField(field2); //获取要素最大值,决定柱子的最大高度 double fieldValue = 0.0, maxValue = 0.0; cursor = table.Search(null, true); rowBuffer = cursor.NextRow(); while (rowBuffer != null) { for (int i = 0; i < RenderField.Length; i++) { int index = table.FindField(RenderField[i]); fieldValue = double.Parse(rowBuffer.get_Value(index).ToString()); if (fieldValue > maxValue) { maxValue = fieldValue; } } rowBuffer = cursor.NextRow(); } //创建水平排列符号 IBarChartSymbol barChartSymbol = new BarChartSymbolClass(); barChartSymbol.Width = BarWidth;//柱子的边长 IMarkerSymbol markerSymbol = barChartSymbol as IMarkerSymbol; markerSymbol.Size = 50;//这个是? IChartSymbol chartSymbol = barChartSymbol as IChartSymbol; chartSymbol.MaxValue = maxValue; //添加渲染符号 ISymbolArray symbolArray = barChartSymbol as ISymbolArray; IFillSymbol[] fillsymbol = new IFillSymbol[RenderField.Length]; for (int i = 0; i < RenderField.Length; i++) { //设置不同颜色的柱子 fillsymbol[i] = new SimpleFillSymbolClass(); fillsymbol[i].Color = fillsymbolColor[i]; symbolArray.AddSymbol(fillsymbol[i] as ISymbol); //IFillSymbol fillSymbol = new SimpleFillSymbolClass(); //fillSymbol.Color = getRGB(255, 0, 0);//红色柱子 //symbolArray.AddSymbol(fillSymbol as ISymbol); //fillSymbol = new SimpleFillSymbolClass(); //fillSymbol.Color = getRGB(0, 255, 0);//绿色柱子 //symbolArray.AddSymbol(fillSymbol as ISymbol); } //设置柱状图符号 chartRenderer.ChartSymbol = barChartSymbol as IChartSymbol; //设置底图样式 IFillSymbol pFillSymbol = new SimpleFillSymbolClass(); pFillSymbol.Color = BgColor; chartRenderer.BaseSymbol = pFillSymbol as ISymbol; chartRenderer.UseOverposter = false;//假如那个位置放不下柱状图,是否用线段连接指示位置 //创建图例 chartRenderer.CreateLegend(); geoFeatureLayer.Renderer = chartRenderer as IFeatureRenderer; } ////// 创建累计柱状图表(stacked) /// /// Name /// Fields /// every Color /// width /// BackGound Color public void createStackedChartSymbol(string layerName, string[] RenderField, IColor[] fillsymbolColor, double BarWidth, IColor BgColor) { IGeoFeatureLayer geoFeatureLayer; IFeatureLayer featureLayer; ITable table; ICursor cursor; IRowBuffer rowBuffer; //获取渲染图层 geoFeatureLayer = getGeoLayer(layerName); featureLayer = geoFeatureLayer as IFeatureLayer; table = featureLayer as ITable; geoFeatureLayer.ScaleSymbols = true; IChartRenderer chartRenderer = new ChartRendererClass(); IRendererFields rendererFields = chartRenderer as IRendererFields; for (int i = 0; i < RenderField.Length; i++) { rendererFields.AddField(RenderField[i], RenderField[i]); } //获取要素最大值 double fieldValue = 0.0, maxValue = 0.0; cursor = table.Search(null, true); rowBuffer = cursor.NextRow(); while (rowBuffer != null) { for (int i = 0; i < RenderField.Length ; i++) { int index = table.FindField(RenderField[i]); fieldValue = double.Parse(rowBuffer.get_Value(index).ToString()); if (fieldValue > maxValue) { maxValue = fieldValue; } } rowBuffer = cursor.NextRow(); } //创建累积排列符号 IStackedChartSymbol stackedChartSymbol = new StackedChartSymbolClass(); stackedChartSymbol.Width = 10;//柱子宽度 IMarkerSymbol markerSymbol = stackedChartSymbol as IMarkerSymbol; markerSymbol.Size = 50;//下面的大小 IChartSymbol chartSymbol = stackedChartSymbol as IChartSymbol; chartSymbol.MaxValue = maxValue; //添加渲染符号 ISymbolArray symbolArray = stackedChartSymbol as ISymbolArray; IFillSymbol[] fillsymbol = new IFillSymbol[RenderField.Length]; for (int i = 0; i < RenderField.Length; i++) { //设置不同颜色的柱子 fillsymbol[i] = new SimpleFillSymbolClass(); fillsymbol[i].Color = fillsymbolColor[i]; symbolArray.AddSymbol(fillsymbol[i] as ISymbol); } //设置柱状图符号 chartRenderer.ChartSymbol = stackedChartSymbol as IChartSymbol; IFillSymbol pFillSymbol = new SimpleFillSymbolClass(); pFillSymbol.Color =BgColor; chartRenderer.BaseSymbol = pFillSymbol as ISymbol; chartRenderer.UseOverposter = false; //创建图例 chartRenderer.CreateLegend(); geoFeatureLayer.Renderer = chartRenderer as IFeatureRenderer; } ////// 创建饼图 /// /// name /// Fields /// Each color /// background Color public void createPieChartSymbol(string layerName, string[] RenderField, IColor[] fillsymbolColor, IColor BgColor) { IGeoFeatureLayer geoFeatureLayer; IFeatureLayer featureLayer; ITable table; ICursor cursor; IRowBuffer rowBuffer; //获取渲染图层 geoFeatureLayer = getGeoLayer(layerName); featureLayer = geoFeatureLayer as IFeatureLayer; table = featureLayer as ITable; geoFeatureLayer.ScaleSymbols = true; IChartRenderer chartRenderer = new ChartRendererClass(); IPieChartRenderer pieChartRenderer = chartRenderer as IPieChartRenderer; IRendererFields rendererFields = chartRenderer as IRendererFields; for (int i = 0; i < RenderField.Length; i++) { rendererFields.AddField(RenderField[i], RenderField[i]); } //获取渲染要素的最大值 double fieldValue = 0.0, maxValue = 0.0; cursor = table.Search(null, true); rowBuffer = cursor.NextRow(); while (rowBuffer != null) { for (int i = 0; i < RenderField.Length ; i++) { int index = table.FindField(RenderField[i]); fieldValue = double.Parse(rowBuffer.get_Value(index).ToString()); if (fieldValue > maxValue) { maxValue = fieldValue; } } rowBuffer = cursor.NextRow(); } //设置饼图符号 IPieChartSymbol pieChartSymbol = new PieChartSymbolClass(); pieChartSymbol.Clockwise = true; pieChartSymbol.UseOutline = true; IChartSymbol chartSymbol = pieChartSymbol as IChartSymbol; chartSymbol.MaxValue = maxValue; ILineSymbol lineSymbol = new SimpleLineSymbolClass(); lineSymbol.Color = getRGB(255, 0, 0); lineSymbol.Width = 2; pieChartSymbol.Outline = lineSymbol; IMarkerSymbol markerSymbol = pieChartSymbol as IMarkerSymbol; markerSymbol.Size = 30; //添加渲染符号 ISymbolArray symbolArray = pieChartSymbol as ISymbolArray; IFillSymbol[] fillsymbol =new IFillSymbol[RenderField.Length]; for (int i = 0; i < RenderField.Length; i++) { fillsymbol[i] = new SimpleFillSymbolClass(); fillsymbol[i].Color = fillsymbolColor[i]; symbolArray.AddSymbol(fillsymbol[i] as ISymbol); } //设置背景 chartRenderer.ChartSymbol = pieChartSymbol as IChartSymbol; IFillSymbol pFillSymbol = new SimpleFillSymbolClass(); pFillSymbol.Color = BgColor; chartRenderer.BaseSymbol = pFillSymbol as ISymbol; chartRenderer.UseOverposter = false; //创建图例 chartRenderer.CreateLegend(); geoFeatureLayer.Renderer = chartRenderer as IFeatureRenderer; }
六、DotDensityRenderer专题图
DotDensityRenderer对象使用DotDensityFillSymbol符号对Polygon类型的要素进行着色。该专题图使用随机分布的点的密度来表现要素某个属性值得大小。也可以对图层的多个属性值着色,通过指定不同的点符号来区分。
DotDensityRenderer对象实现IDotDensityRenderer接口,定义了使用点密度着色方法和属性,如DotDensitySymbol用于确定着色点符号,CreateLegend方法用于产生图例。
////// 点密度 /// /// 图层名字 /// 渲染字段 /// 点密度符号 /// 点符号 /// 点密度 public void createDotDensityFillSymbol(string layerName, string RenderField, IDotDensityFillSymbol dotDensityFillSymbol, ISimpleMarkerSymbol simpleMarkerSymbol,double RenderDensity) { IGeoFeatureLayer geoFeatureLayer; IDotDensityRenderer dotDensityRenderer; //获取渲染图层 geoFeatureLayer = getGeoLayer(layerName); dotDensityRenderer = new DotDensityRendererClass(); IRendererFields rendererFields = dotDensityRenderer as IRendererFields; //设置渲染字段 rendererFields.AddField(RenderField, RenderField); ////设置填充颜色和背景色,直接由参数传入,具体定义参考下面的 例子 //dotDensityFillSymbol = new DotDensityFillSymbolClass(); //dotDensityFillSymbol.DotSize = 3; //dotDensityFillSymbol.Color = getRGB(255, 0, 0); //dotDensityFillSymbol.BackgroundColor = getRGB(0, 255, 0); //设置渲染符号 ISymbolArray symbolArray = dotDensityFillSymbol as ISymbolArray; //点符号样式,也可以参考下面的自己定义 //ISimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbolClass(); //simpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSCircle; //simpleMarkerSymbol.Size = 2; //simpleMarkerSymbol.Color = getRGB(0, 0, 255); symbolArray.AddSymbol(simpleMarkerSymbol as ISymbol); dotDensityRenderer.DotDensitySymbol = dotDensityFillSymbol; //设置渲染密度 dotDensityRenderer.DotValue = RenderDensity; //创建图例 dotDensityRenderer.CreateLegend(); geoFeatureLayer.Renderer = dotDensityRenderer as IFeatureRenderer; }
此外还有两个补充方法(在调用这些方法的时候能用到):
private IMap pMap; public Renderer(IMap _pMap) { pMap = _pMap; } //获取颜色对象 private IRgbColor getRGB(int r, int g, int b) { IRgbColor pColor; pColor = new RgbColorClass(); pColor.Red = r; pColor.Green = g; pColor.Blue = b; return pColor; } private IGeoFeatureLayer getGeoLayer(string layerName) { ILayer layer; IGeoFeatureLayer geoFeatureLayer; for (int i = 0; i < this.pMap.LayerCount; i++) { layer = this.pMap.get_Layer(i); if (layer != null && layer.Name == layerName) { geoFeatureLayer = layer as IGeoFeatureLayer; return geoFeatureLayer; } } return null; }