原文地址:ArcEngine 产生专题图作者:小生
ArcEngine提供多个着色对象用于产生专题图,可以使用标准着色方案,也可以自定义着色方案,ArcEngine提供8中标准着色方案。
一、SimpleRenderer专题图
是使用单一符号进行着色分类,不涉及对要素的数据进行处理。这种专题图同一个图层内的所有元素都是一种符号。
通过SimpleRenderer对象对Symbol进行设置后,赋予IGeoFeatureLayer接口的Renderer属性,对象实现了ITransparencyRenerer接口,通过该接口的属性,可以根据要素的某一个数值字段来设置要素显示的透明度。
该对象还实现了ISimpleRenderer接口,提供了两个重要的属性:Description和Label这两个属性用来设置图例。
/// <summary> /// 简单渲染 /// </summary> /// <param name="layerName">图层名字</param> /// <param name="FillStyle">FillStyle</param> /// <param name="pColor">FillColor</param> /// <param name="OutLineColor">OutLineColor</param> /// <param name="RenderLabel">样式名称注释</param> /// <param name="Descripition">描述</param> 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属性用于设置分级的数目。
/// <summary> /// 分级渲染 /// </summary> /// <param name="layerName">图层名</param> /// <param name="classCount">分级数目</param> /// <param name="ClassField">分级字段</param> /// <param name="FillStyle">填充样式</param> 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对象。
/// <summary> /// 单一值渲染(单字段) /// </summary> /// <param name="layerName">图层名</param> /// <param name="RenderField">渲染字段</param> /// <param name="FillStyle">填充样式</param> /// <param name="valueCount">字段的唯一值个数</param> 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; } /// <summary> /// 单一值渲染(多字段) /// </summary> /// <param name="layerName">图层名</param> /// <param name="RenderField">多字段名</param> /// <param name="FillStyle">样式</param> /// <param name="valueCount">每个字段中唯一值的个数</param> 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要出现的级别数目。
/// <summary> /// 梯度着色 /// </summary> /// <param name="layerName">图层名字</param> /// <param name="ProportionField">要参考的字段</param> /// <param name="FillStyle">填充样式</param> /// <param name="fillColor">填充Color</param> /// <param name="characterMarkerSymbol">特征点符号</param> /// <param name="units">参考单位</param> /// <param name="pLegendSymbolCount">要分成的级数</param> 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符号来修饰要素。
/// <summary> /// 条状图渲染 /// </summary> /// <param name="layerName">图层名字</param> /// <param name="RenderField">要柱状表示的字段</param> /// <param name="fillsymbolColor">这些字段分别需要渲染的颜色</param> /// <param name="BarWidth">每个柱子的宽度</param> /// <param name="BgColor">背景色</param> 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; } /// <summary> /// 创建累计柱状图表(stacked) /// </summary> /// <param name="layerName">Name</param> /// <param name="RenderField">Fields</param> /// <param name="fillsymbolColor">every Color</param> /// <param name="BarWidth">width</param> /// <param name="BgColor">BackGound Color</param> 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; } /// <summary> /// 创建饼图 /// </summary> /// <param name="layerName">name</param> /// <param name="RenderField">Fields</param> /// <param name="fillsymbolColor">Each color</param> /// <param name="BgColor">background Color</param> 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方法用于产生图例。
/// <summary> /// 点密度 /// </summary> /// <param name="layerName">图层名字</param> /// <param name="RenderField">渲染字段</param> /// <param name="dotDensityFillSymbol">点密度符号</param> /// <param name="simpleMarkerSymbol">点符号</param> /// <param name="RenderDensity">点密度</param> 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;