How To: Limit the classes displayed in a legend to features shown in the view
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.
- Open a new script window. A. Activate the Project window.
B. Click the Scripts icon.
C. Click New.
- 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
'-- 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
- 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.
- Make the theme active. This is the theme for which you wish to alter the legend classes.
- Zoom to a location within the theme extent.
- Click the new button to run the script.