This sample consists of a VBA macro that creates a new graduated color type classification, sets the class breaks (thus a manual classification), and assigns it to a feature layer. The sample also demonstrates how to build and use a color ramp, including loading/saving from a style. The macro also shows how to set the properties of the user interface to allow editing of layer symbology after rendering.
How to use
- Open ArcMap and create a new map document.
- Use the 'Add Data' button to add to your map the states.shp file located in your ArcGIS Developer Kit installation location in the Samples/Data/Usa folder.
- Create a new macro (Tools--> Macros--> Create). Copy the code provided above and paste into the Visual Basic Editor of your map document.
- Run the macro
Public Sub ClassifyManual()
' this VBA macro (1) demonstrates how to create and set a custom classification
' for a ClassBreaks type renderer and (2) how to build and use a color ramp to
' symbolize the rendered layer
Dim pMxDoc As IMxDocument
Dim pGeoLayer As IGeoFeatureLayer
Dim pFClass As IFeatureClass
Dim pFeature As IFeature
Dim pRender As IClassBreaksRenderer
Dim pColorEnum As IEnumColors
Dim pAColorRamp As IAlgorithmicColorRamp
Dim pColor1 As IRgbColor
Dim pColor2 As IRgbColor
Dim i As Integer
Dim pSFSym As ISimpleFillSymbol
Dim pUIProperties As IClassBreaksUIProperties
Dim pStyleGallery As IStyleGallery
Dim pItem As IStyleGalleryItem
Dim bFoundIt As Boolean
Dim pRendererPropPage As IRendererPropertyPage
' --------------------------------------------------------------------------
' Renderer Properties:
' get feature class
Set pMxDoc = Application.Document
Set pGeoLayer = pMxDoc.ActiveView.FocusMap.Layer(0) ' first layer from active dataframe
Set pFClass = pGeoLayer.FeatureClass
' make new class breaks renderer with 5 classes
Set pRender = New ClassBreaksRenderer
pRender.Field = "POP90_SQMI"
pRender.BreakCount = 5 ' five classes
' set class breaks
pRender.MinimumBreak = 1 ' min value in dataset
pRender.Break(0) = 110 ' max value in class(0)
pRender.Break(1) = 300 ' max value in class(1)
pRender.Break(2) = 700
pRender.Break(3) = 1500
pRender.Break(4) = 9187 ' max value in dataset
' set class labels (not necessary, but these look better than the default).
pRender.Label(0) = "1-110"
pRender.Label(1) = "111-300"
pRender.Label(2) = "301-700"
pRender.Label(3) = "701-1500"
pRender.Label(4) = "1501-9187"
' --------------------------------------------------------------------------
' Symbology:
' setup an algorithmic color ramp
Set pColor1 = New RgbColor
pColor1.RGB = RGB(242, 233, 250) ' lavender
Set pColor2 = New RgbColor
pColor2.RGB = RGB(56, 45, 121) ' deep purple
Set pAColorRamp = New AlgorithmicColorRamp
pAColorRamp.Algorithm = esriHSVAlgorithm
pAColorRamp.Size = 5
pAColorRamp.FromColor = pColor1
pAColorRamp.ToColor = pColor2
pAColorRamp.CreateRamp (True)
Set pColorEnum = pAColorRamp.Colors
pColorEnum.Reset
' set fill symbol for each class
For i = 0 To 4
Set pSFSym = New SimpleFillSymbol
pSFSym.Color = pColorEnum.Next
pRender.Symbol(i) = pSFSym
Next i
' make sure the color ramp is saved so it can be used later
Set pStyleGallery = New StyleGallery
' look for an existing color ramp item with the same name
Dim pEnum As IEnumStyleGalleryItem
Set pEnum = pStyleGallery.Items("Color Ramps", "", "")
pEnum.Reset
bFoundIt = False
Set pItem = pEnum.Next
Do While (Not bFoundIt) And (Not pItem Is Nothing)
bFoundIt = (pItem.Name = "MyCustomRamp")
Set pItem = pEnum.Next
Loop
' if color ramp is not found, then add it to style gallery now
If Not bFoundIt Then
Set pItem = New StyleGalleryItem
pItem.Name = "MyCustomRamp"
pItem.Item = pAColorRamp ' the algorithmic color ramp we creat
pStyleGallery.AddItem pItem
End If
' --------------------------------------------------------------------------
' UI Properties:
' MUST set user interface (UI) properties to allow editing of the
' rendered layer through the symbology tab
Set pUIProperties = pRender
pUIProperties.ColorRamp = "MyCustomRamp"
pUIProperties.LowBreak(0) = 1
pUIProperties.LowBreak(1) = 111
pUIProperties.LowBreak(2) = 301
pUIProperties.LowBreak(3) = 701
pUIProperties.LowBreak(4) = 1501
pUIProperties.ShowClassGaps = False
' --------------------------------------------------------------------------
' finally apply renderer to layer and associate property page
Set pGeoLayer.Renderer = pRender
Set pRendererPropPage = New GraduatedColorPropertyPage
pGeoLayer.RendererPropertyPageClassID = pRendererPropPage.ClassID
' refresh dataframe
pMxDoc.UpdateContents
pMxDoc.ActiveView.Refresh
End Sub