Limit the classes displayed in a legend to features shown in the view

Last Published: April 25, 2020


You can use the following Avenue script to limit the classes displayed in a legend to only those that correspond to features within the current extent of the view.


  1. Open a new script window.

    A. Activate the Project window.
    B. Click the Scripts icon.
    C. Click New.

  2. Copy the following code into the new window:

    '-- This script temporarily limits each theme to just the records
    '-- in the display's extent and uses the built-in capability of a
    '-- legend to return class counts to determine which classes
    '-- it can dispense with. It is a general approach that
    '-- does not need to worry about the details of each legend.

    '-- Obtain the view's extent. It will be used to select the
    '-- features for classification.
    if (theView.Is(View).Not) then return NIL end
    theExtent=theView.GetDisplay.ReturnExtent '-- Will select in the visible extents

    '-- Process each active theme, if any.
    sLog = "" ' Records what happens
    for each theTheme in theView.GetActiveThemes
    if (theTheme.Is(FTheme) and theTheme.CanSelect and
    theTheme.CanReturnClassCounts) then
    '-- Select visible features.
    '-- Limit the theme to the selected features.
    '-- Remove unused classes from the legend.
    lstCounts = theTheme.ReturnClassCounts
    lstClassifications = theTheme.GetLegend.GetClassifications
    lstSymbols = theTheme.GetLegend.GetSymbols
    n = 0 ' Counts the number of classes removed.
    for each i in lstClassifications.Count-2..0 by -1 ' Skip the NoData class
    if (lstCounts.Get(i) = 0) then
    n = n+1
    theTheme.UpdateLegend ' Cause the legend to redraw in the TOC
    '-- Log the changes.
    if (n = 1) then sClass = "class" else sClass = "classes" end
    sLog = sLog + NL + "[" + theTheme.GetName + "]: " +
    n.AsString ++ sClass ++ "removed."
    '-- Restore the original definition and selection.

    else '-- This is an image or grid theme, most likely.
    sLog = sLog + NL + "[" + theTheme.GetName + "] could not be modified."

    '-- Summarize results.
    if (sLog <> "") then
    MsgBox.Report("Summary of changes made to legends:" + NL + sLog, Script.The.GetName)
    '-- End of Script

  3. Attach the script to a button on the View GUI.

    A. Compile the script.
    B. Switch to the Project window.
    C. Select Customize from the Project menu.
    D. On the Customize dialog box, click the Type dropdown arrow and click View.
    E. Select Buttons under Category.
    F. Click the New button.
    G. Double-click the Click property in the Customize dialog box.
    H. Type the name of the script in the Script Manager and click Select.
    I. Close the Customize dialog box.

    For more information, see "Customize dialog box" in ArcView Help.

  4. Make the theme active. This is the theme for which you wish to alter the legend classes.
  5. Zoom to a location within the theme extent.
  6. Click the new button to run the script.

Article ID:000004666

  • Legacy Products

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options