GIS-ArcGIS应用与开发技术(35)

柱状图符号化

1.设计窗体界面

2.编写加载图层列表代码

 private void frmChart_Load(object sender, EventArgs e)
        {
            try
            {
                //将当前图层列表清空
                comboBox1.Items.Clear();

                string layerName;   //设置临时变量存储图层名称

                //对Map中的每个图层进行判断并加载名称
                for (int i = 0; i < currentmap.LayerCount; i++)
                {
                    //如果该图层为图层组类型,则分别对所包含的每个图层进行操作
                    if (currentmap.get_Layer(i) is GroupLayer)
                    {
                        //使用ICompositeLayer接口进行遍历操作
                        ICompositeLayer compositeLayer = currentmap.get_Layer(i) as ICompositeLayer;
                        for (int j = 0; j < compositeLayer.Count; j++)
                        {
                            //将图层的名称添加到comboBoxLayerName控件中
                            layerName = compositeLayer.get_Layer(j).Name;
                            comboBox1.Items.Add(layerName);
                        }
                    }
                    //如果图层不是图层组类型,则直接添加名称
                    else
                    {
                        layerName = currentmap.get_Layer(i).Name;
                        comboBox1.Items.Add(layerName);
                    }
                }
                //将comboBoxLayerName控件的默认选项设置为第一个图层的名称
                comboBox1.SelectedIndex = 0;

            }
            catch { }
        }

 

3.编写字段列表代码

   private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            //首先将字段列表和字段值列表清空
            comboBox2.Items.Clear();
            comboBox3.Items.Clear();

            IField field;   //设置临时变量存储使用IField接口的对象

            for (int i = 0; i < currentmap.LayerCount; i++)
            {
                if (currentmap.get_Layer(i) is GroupLayer)
                {
                    ICompositeLayer compositeLayer = currentmap.get_Layer(i) as ICompositeLayer;
                    for (int j = 0; j < compositeLayer.Count; j++)
                    {
                        //判断图层的名称是否与comboBoxLayerName控件中选择的图层名称相同
                        if (compositeLayer.get_Layer(j).Name == comboBox1.SelectedItem.ToString())
                        {
                            //如果相同则设置为整个窗体所使用的IFeatureLayer接口对象
                            currentFeatureLayer = compositeLayer.get_Layer(j) as IFeatureLayer;
                            break;
                        }
                    }
                }
                else
                {
                    //判断图层的名称是否与comboBoxLayerName中选择的图层名称相同
                    if (currentmap.get_Layer(i).Name == comboBox1.SelectedItem.ToString())
                    {
                        //如果相同则设置为整个窗体所使用的IFeatureLayer接口对象
                        currentFeatureLayer = currentmap.get_Layer(i) as IFeatureLayer;
                        break;
                    }
                }
            }

            //使用IFeatureClass接口对该图层的所有属性字段进行遍历,并填充listBoxFields控件
            for (int i = 0; i < currentFeatureLayer.FeatureClass.Fields.FieldCount; i++)
            {
                //根据索引值获取图层的字段
                field = currentFeatureLayer.FeatureClass.Fields.get_Field(i);
                //排除SHAPE字段,并在其它字段名称前后添加字符"和字符"
                if (field.Type == esriFieldType.esriFieldTypeDouble ||
                 field.Type == esriFieldType.esriFieldTypeInteger ||
                 field.Type == esriFieldType.esriFieldTypeSingle ||
                 field.Type == esriFieldType.esriFieldTypeSmallInteger)
                 comboBox2.Items.Add(field.Name);
                 comboBox3.Items.Add(field.Name);
            }

        }

 

3.编写渲染柱状图代码

  private void button1_Click(object sender, EventArgs e)
        {
            IGeoFeatureLayer pGeoFeatureLayer = currentFeatureLayer as IGeoFeatureLayer;
            pGeoFeatureLayer.ScaleSymbols = true;

            IFeatureClass pFeatureClass = currentFeatureLayer.FeatureClass;

            //定义柱状图渲染组建对象

            IChartRenderer pChartRenderer = new ChartRendererClass();

            //定义渲染字段对象并给字段对象实例化为pChartRenderer 
            IRendererFields pRendererFields;

            pRendererFields = (IRendererFields)pChartRenderer;

            //向渲染字段对象中添加字段--- 待补充自定义添加 
            pRendererFields.AddField(comboBox2.SelectedItem.ToString());
            pRendererFields.AddField(comboBox3.SelectedItem.ToString());
            //pRendererFields.AddField(pFieldName2);

            ITable pTable = pGeoFeatureLayer as ITable;
            int[] pFieldIndecies = new int[2];

            pFieldIndecies[0] = pTable.FindField(comboBox2.SelectedItem.ToString());
            pFieldIndecies[1] = pTable.FindField(comboBox3.SelectedItem.ToString());

            //pFieldIndecies[1] = pTable.FindField(pFieldName2);

            IDataStatistics pDataStat = new DataStatisticsClass();

            IFeatureCursor pFtCursor = currentFeatureLayer.FeatureClass.Search(null, false);
            pDataStat.Cursor = pFtCursor as ICursor;

            pDataStat.Field = comboBox3.SelectedItem.ToString();

            double pMax = pDataStat.Statistics.Maximum;

            // 定义并设置渲染时用的chart marker symbol

            IBarChartSymbol pBarChartSymbol = new BarChartSymbolClass();
            pBarChartSymbol.Width = 6;

            IChartSymbol pChartSymbol;

            pChartSymbol = pBarChartSymbol as IChartSymbol;

            IMarkerSymbol pMarkerSymbol;

            pMarkerSymbol = (IMarkerSymbol)pBarChartSymbol;

            IFillSymbol pFillSymbol;

            //设置pChartSymbol的最大值 
            pChartSymbol.MaxValue = pMax;

            // 设置bars的最大高度

            pMarkerSymbol.Size = 80;

            //下面给每一个bar设置符号

            //定义符号数组

            ISymbolArray pSymbolArray = (ISymbolArray)pBarChartSymbol;

            //添加第一个符号

            pFillSymbol = new SimpleFillSymbolClass();
            pFillSymbol.Color = GetRgbColor(193, 252, 179) as IColor;
            pSymbolArray.AddSymbol(pFillSymbol as ISymbol);

            //添加第二个符号

            pFillSymbol = new SimpleFillSymbolClass();
            pFillSymbol.Color = GetRgbColor(145, 55, 251) as IColor;
            pSymbolArray.AddSymbol(pFillSymbol as ISymbol);

            pChartRenderer.ChartSymbol = pChartSymbol as IChartSymbol;

            //pChartRenderer.Label = "AREA"; 
            pFillSymbol = new SimpleFillSymbolClass();

            pFillSymbol.Color = GetRgbColor(239, 228, 190);
            pChartRenderer.BaseSymbol = (ISymbol)pFillSymbol;
            pChartRenderer.CreateLegend();

            pChartRenderer.UseOverposter = false;

            //将柱状图渲染对象与渲染图层挂钩

            pGeoFeatureLayer.Renderer = (IFeatureRenderer)pChartRenderer;

           

            mapcontrol1.Refresh();
            toccontrol1.Update();
            this.Close();
        }

 

4.编写获取RGB值代码

 private RgbColor GetRgbColor(int red, int green, int blue)
        {
            RgbColor rgbColor = new RgbColorClass();
            rgbColor.Red = red;
            rgbColor.Green = green;
            rgbColor.Blue = blue;

            return rgbColor;
        }

5.添加引用

using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using System.Collections;
using ESRI.ArcGIS.Controls;

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值