English

How To: Create bivariate symbology using ArcObjects

Summary

A bivariate symbol can be created through the use of ArcObjects and Visual Basic for Applications.

Procedure

  1. Start ArcMap.
  2. Open the Visual Basic Editor.

    In ArcMap, select Tools > Macros > Visual Basic Editor.

  3. In the Project Explorer window, expand Project.mxt and select ArcMap Objects > ThisDocument. Right-click and click View Code.
    [O-Image] Visual Basic Project  Explorer
    Note:
    Code in ThisDocument code module will only run in the current map document. To store the code in all your map documents, open the Normal.mxt ThisDocument code module.

  4. Copy the following code into the code module.

    Option Explicit

    Const NUM_FEATURES As Integer = 51
    Const FIELD_NAME As String = "Sub_Region"

    Sub BuildBivariateRenderer()

    Dim pMxDoc As IMxDocument
    Set pMxDoc = Application.Document
    Dim pFeatureLayer As IFeatureLayer
    Set pFeatureLayer = pMxDoc.ActiveView.FocusMap.Layer(0)

    Dim pLayer As IFeatureLayer
    Dim pGeoLayer As IGeoFeatureLayer
    Set pLayer = pFeatureLayer
    Set pGeoLayer = pLayer

    ' --------------------------------------------------------------------------------
    ' Set up a UniqueValueRenderer for .MainRenderer

    Dim pColorRamp As IRandomColorRamp
    Set pColorRamp = New RandomColorRamp
    pColorRamp.MinSaturation = 20
    pColorRamp.MaxSaturation = 40
    pColorRamp.MinValue = 85
    pColorRamp.MaxValue = 100
    pColorRamp.StartHue = 0
    pColorRamp.EndHue = 65
    pColorRamp.UseSeed = True
    pColorRamp.Seed = 43

    ' Make the renderer
    Dim pRender As IUniqueValueRenderer
    Set pRender = New UniqueValueRenderer

    pRender.FieldCount = 1
    pRender.Field(0) = FIELD_NAME

    Dim pFeatClass As IFeatureClass
    Dim pQueryFilter As IQueryFilter
    Dim pFeatCursor As IFeatureCursor
    Set pFeatClass = pFeatureLayer.FeatureClass
    Set pQueryFilter = New QueryFilter
    Set pFeatCursor = pFeatClass.Search(pQueryFilter, False)

    Dim pSimpleFillSym As ISimpleFillSymbol
    Dim pFeat As IFeature
    Dim strValue As String
    Dim i As Integer
    Dim bNoValueFound As Boolean
    Dim j As Integer
    i = 0
    Do While i <= NUM_FEATURES - 1 ' feature loop
    Set pSimpleFillSym = New SimpleFillSymbol
    pSimpleFillSym.Style = esriSFSSolid
    Set pFeat = pFeatCursor.NextFeature
    strValue = pFeat.Value(5) ' get value in "SUB_REGION" field
    bNoValueFound = True
    For j = 0 To (pRender.ValueCount - 1)
    If pRender.Value(j) = strValue Then
    bNoValueFound = False
    Exit For
    End If
    Next j
    If bNoValueFound Then
    pRender.AddValue strValue, "SUB_REGION", pSimpleFillSym
    pRender.Label(strValue) = strValue
    End If
    i = i + 1
    Loop
    Dim bOk As Boolean
    pColorRamp.Size = pRender.ValueCount
    pColorRamp.CreateRamp bOk

    Dim pEnumColors As IEnumColors
    Set pEnumColors = pColorRamp.Colors
    pEnumColors.Reset

    For j = 0 To (pRender.ValueCount - 1)
    strValue = pRender.Value(j)
    If strValue <> "" Then
    Set pSimpleFillSym = pRender.Symbol(strValue)
    pSimpleFillSym.Color = pEnumColors.Next
    pRender.Symbol(strValue) = pSimpleFillSym
    End If
    Next j

    pRender.FieldType(0) = True ' meaning String type

    ' Do not use default symbol
    pRender.UseDefaultSymbol = False

    ' --------------------------------------------------------------------------------
    ' Set up a ClassBreaksRenderer for .VariationRenderer

    Dim cb(5) As Double
    cb(0) = 1000000
    cb(1) = 2000000
    cb(2) = 5000000
    cb(3) = 10000000
    cb(4) = 50000000

    Dim pCBRender As IClassBreaksRenderer
    Set pCBRender = New ClassBreaksRenderer
    pCBRender.Field = "Pop1990"
    pCBRender.BreakCount = 5
    pCBRender.MinimumBreak = 0

    Dim pAOColorRamp As IAlgorithmicColorRamp
    Set pAOColorRamp = aoAlgorithmicColorRamp(aoRGBColor(255, 210, 210, 0), aoRGBColor(190, 0, 170, 0), 1)
    pAOColorRamp.Size = 5
    pAOColorRamp.CreateRamp (bOk)

    Set pEnumColors = pAOColorRamp.Colors
    pEnumColors.Reset

    Dim pCBUI As IClassBreaksUIProperties
    Set pCBUI = pCBRender

    pCBRender.Break(0) = cb(0)
    pCBRender.Label(0) = pCBRender.MinimumBreak & " - " & cb(0)
    Set pSimpleFillSym = New SimpleFillSymbol
    pSimpleFillSym.Color = pEnumColors.Next
    pCBRender.Symbol(0) = pSimpleFillSym
    pCBUI.LowBreak(0) = pCBRender.MinimumBreak

    For i = 1 To 4
    pCBRender.Break(i) = cb(i)
    pCBRender.Label(i) = cb(i - 1) & " - " & cb(i)
    Set pSimpleFillSym = New SimpleFillSymbol
    pSimpleFillSym.Color = pEnumColors.Next
    pCBRender.Symbol(i) = pSimpleFillSym
    pCBUI.LowBreak(i) = pCBRender.Break(i - 1)
    Next i

    pCBUI.ColorRamp = "Custom"

    ' --------------------------------------------------------------------------------
    ' Set up BivariateRenderer

    Dim pBiVariateRend As IBivariateRenderer
    Set pBiVariateRend = New BiUniqueValueRenderer

    Set pBiVariateRend.MainRenderer = pRender ' UniqueValueRenderer
    Set pBiVariateRend.VariationRenderer = pCBRender ' ClassBreaksRenderer

    pBiVariateRend.CreateLegend

    Set pGeoLayer.Renderer = pBiVariateRend
    pMxDoc.ActiveView.Refresh
    pMxDoc.UpdateContents

    End Sub

    Private Function aoRGBColor(R, G, B, IsNull) As IRgbColor
    '** Makes color objects given RGB values
    Set aoRGBColor = New RgbColor
    With aoRGBColor
    .Red = R
    .Green = G
    .Blue = B
    .NullColor = IsNull
    End With
    End Function

    Private Function aoAlgorithmicColorRamp(StartColor As IRgbColor, EndColor As IRgbColor, clab) As IAlgorithmicColorRamp
    '** Makes a new Algorithmic Color Ramp
    Set aoAlgorithmicColorRamp = New AlgorithmicColorRamp
    If clab = 1 Then
    aoAlgorithmicColorRamp.Algorithm = esriCIELabAlgorithm
    ElseIf clab = 2 Then
    aoAlgorithmicColorRamp.Algorithm = esriHSVAlgorithm
    ElseIf clab = 3 Then
    aoAlgorithmicColorRamp.Algorithm = esriLabLChAlgorithm
    End If
    aoAlgorithmicColorRamp.FromColor = StartColor
    aoAlgorithmicColorRamp.ToColor = EndColor
    End Function


  5. Close the Visual Basic Editor.
  6. Run the code.

    A. Click Tools > Macros > Macros to display the Macros dialog box.
    B. Select a macro and click Run.

Related Information