HOW TO

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

Last Published: April 25, 2020

Summary

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.

Procedure



  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:

    Code:
    '-- 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.
    theView=av.GetActiveDoc
    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
    theFTab=theTheme.GetFtab
    '
    '-- Select visible features.
    theFTab.RememberSelection
    theTheme.SelectbyRect(theExtent,#VTAB_SELTYPE_NEW)
    '
    '-- Limit the theme to the selected features.
    theFTab.SetDefBitmap(theFTab.GetSelection.Clone)
    theFTab.UpdateDefBitmap
    '
    '-- 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
    lstClassifications.Remove(i)
    lstSymbols.Remove(i)
    n = n+1
    end
    end
    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.
    theFTab.SetDefinition(theFTab.GetDefinition)
    theFTab.UpdateDefBitmap
    theFTab.SetSelection(theFTab.GetLastSelection)

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

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

Software:
  • Legacy Products

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Discover more on this topic