c unique value renders the instance code

  • 2020-05-27 07:02:06
  • OfStack

The coloring method 1 sentence coloring the attribute value of a numeric field of the element class in the layer, according to which a separate display symbol style is assigned for each different value element. The key is to get the unique 1 value of all the elements in this field (that is, all the elements with the same attribute value in this field are grouped into one), which can be rendered using the AddValue method of UniqueValueRenderer

The class library you need to add


using System.Collections;
using System.Windows.Forms;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;

Concrete code (in this case, single-value rendering)


private ISymbol GetSymbol(IColor pColor)
        {
            ISymbol pSymbol;
            ISimpleFillSymbol pSymbolFillSymbol = new SimpleFillSymbolClass();
            pSymbolFillSymbol.Color = pColor;
            pSymbolFillSymbol.Outline.Width = 0.4;
            pSymbol = pSymbolFillSymbol as ISymbol;
            return pSymbol;
        }
 private void uniqueValueRendererToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IMap pMap = this.axMapControl1.ActiveView.FocusMap;
            if (pMap.LayerCount== 0)
            {
                MessageBox.Show(" The map is empty, please load the map! ", " prompt ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            IGeoFeatureLayer pGeoLayer = this.axMapControl1.get_Layer(0) as IGeoFeatureLayer ;
            ITable pTable = pGeoLayer.FeatureClass as ITable;
            ICursor pCursor;
            IQueryFilter pQueryFilter = new QueryFilter();
            pQueryFilter.AddField("PERIMETER");
            pCursor = pTable.Search(pQueryFilter, true);// For field 
            IEnumerator pEnumreator;
            // Gets the attributes of each element in the field 1 value 
            IDataStatistics pDataStatistics = new DataStatisticsClass();
            pDataStatistics.Field = "PERIMETER";// Get the statistics field 
            pDataStatistics.Cursor = pCursor;
            pEnumreator = pDataStatistics.UniqueValues;
            int fieldcount = pDataStatistics.UniqueValueCount;// only 1 Number of values to determine the color band range 
            IUniqueValueRenderer pUniqueValueR = new UniqueValueRendererClass();
            pUniqueValueR.FieldCount = 1;// Single value rendering 
            pUniqueValueR.set_Field(0, "PERIMETER");// Rendering field 
            IEnumColors pEnumColor = GetColorRamp(fieldcount).Colors;
            pEnumColor.Reset();
            while (pEnumreator.MoveNext())
            {
                string value = pEnumreator.Current.ToString();
                if (value != null)
                {
                    IColor pColor = pEnumColor.Next();
                    ISymbol pSymbol = GetSymbol(pColor);
                    pUniqueValueR.AddValue(value, "PERIMETER", pSymbol);
                    //pUniqueValueR.set_Symbol(value, pSymbol);

                }

            }
            pGeoLayer.Renderer = pUniqueValueR as IFeatureRenderer;
            this.axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
            this.axTOCControl1.Update();

        }
        private IRandomColorRamp GetColorRamp(int size)
        {
            IRandomColorRamp pRandomColorRamp = new RandomColorRampClass();
            pRandomColorRamp.StartHue = 10;
            pRandomColorRamp.EndHue = 300;
            pRandomColorRamp.MaxSaturation =100;
            pRandomColorRamp.MinSaturation = 0;
            pRandomColorRamp.MaxValue = 100;
            pRandomColorRamp.MinValue = 0;
            pRandomColorRamp.Size = size;
            bool ok = true;
            pRandomColorRamp.CreateRamp(out ok);
            return pRandomColorRamp;
        }

Where datastatistic and field of uniqueValueRenderer must be explicit and have the same value


Related articles: