English

How To: Use IHistogram::GetHistogram to return the values and frequency for a numeric field

Summary

Instructions provided describe how to perform Frequency commands for numeric fields in ArcMap using VBA and ArcObjects. ArcInfo has a Frequency command that returns a list of unique code occurences and their frequency for numeric and text fields.

Procedure

The specified field must contain numeric values.

  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 select View Code.
    [O-Image] Visual Basic Project  Explorer
    Note:
    Code in the ThisDocument 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.

    Code:
    Sub TestHistogram()
    Dim pDoc As IMxDocument
    Set pDoc = ThisDocument
    Dim pMap As IMap
    Set pMap = pDoc.FocusMap

    Dim pLayer As IFeatureLayer
    Set pLayer = pMap.Layer(0)
    Dim pTable As ITable
    Set pTable = pLayer.FeatureClass

    Dim pTableHistogram As ITableHistogram
    Set pTableHistogram = New TableHistogram

    Dim pHistogram As IHistogram
    Set pHistogram = pTableHistogram

    ' Get values and frequencies for STATE_FIPS
    ' for the US Counties layer into a table histogram object
    pTableHistogram.Field = "STATE_FIPS"
    Set pTableHistogram.Table = pTable

    Dim dataFrequency As Variant
    Dim dataValues As Variant
    pHistogram.GetHistogram dataValues, dataFrequency

    ' Loop through the values returned by GetHistogram
    For i = LBound(dataValues) To UBound(dataValues)
    Debug.Print dataValues(i) & " " & dataFrequency(i)
    Next i
    End Sub

  5. If the Intermediate Window is not open in the Visual Basic Editor, choose View > Intermediate Window to display it.
  6. Run the code.

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

  7. The Intermediate Window will list the unique values found and the frequency of occurences for each value.

Related Information