You are here: > ESRI Forums > arcgis desktop discussion forums > Thread Replies

ArcGIS Desktop Discussion Forums

ArcGIS Desktop - ArcMap Page Layout forum

Showing record count in a legend   Erin Whiteley Mar 10, 2006
Re: Showing record count in a legend   Miles Hitchen Mar 10, 2006
Re: Showing record count in a legend   Erin Whiteley Mar 13, 2006
Re: Showing record count in a legend   Miles Hitchen Mar 13, 2006
Re: Showing record count in a legend   Erin Whiteley Mar 14, 2006
Re: Showing record count in a legend   Miles Hitchen Mar 14, 2006
Re: Showing record count in a legend   Erin Whiteley Mar 14, 2006
Re: Showing record count in a legend   Kristy Goddard Feb 28, 2007
Re: Showing record count in a legend   Miles Hitchen Mar 01, 2007
Re: Showing record count in a legend   Michael Slavnik Jun 06, 2007
Re: Showing record count in a legend   Raymond Wang Feb 27, 2008
Re: Showing record count in a legend   Tim Lomas Oct 21, 2009
Re: Showing record count in a legend   Raymond Wang Oct 21, 2009
Re: Showing record count in a legend   jef duncan Aug 28, 2009
Re: Showing record count in a legend   Kathryn Clifton May 19, 2010
Re: Showing record count in a legend   Miles Hitchen May 19, 2010
Re: Showing record count in a legend   Kathryn Clifton May 25, 2010
Re: Showing record count in a legend   kit allen May 26, 2010
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Showing record count in a legend 
Author Erin Whiteley 
Date Mar 10, 2006 
Message In a map showing community population ranges, I want to display the number of communities in each range in the legend. This is something that was available on an option button in MapInfo, but I can't find a similar function in ArcGIS.

Is there an easy way to do this? I searched the Knowledge Base and didn't see any articles on it. If there is no option for it, but a code that will do it, I'd love to have the code, along with brief instructions on how to attach it to a button. Thanks in advance. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Showing record count in a legend 
Author Miles Hitchen 
Date Mar 10, 2006 
Message Hi Erin,

The code below will add the Feature Count to the selected layer in the TOC. You can then add a Legend to your layout view and the feature counts will appear there too.

To use the code, paste it into a new module in the VBA editor. Then select a layer and run the macro called "AddFeatureCountToTOC"

Hope that helps,
Miles.
 
 
Public Sub AddFeatureCountToTOC()
Dim pMxDoc As IMxDocument
Dim pLyr As ILayer
Dim pFtrLyr As IFeatureLayer
Dim pFtrLyrDef As IFeatureLayerDefinition
Dim pQryFltr As IQueryFilter

    Set pMxDoc = ThisDocument
    Set pLyr = pMxDoc.SelectedLayer
    If Not (TypeOf pLyr Is IFeatureLayer) Then Exit Sub
    
    Set pFtrLyr = pLyr
    Set pFtrLyrDef = pFtrLyr
    If pFtrLyrDef.DefinitionExpression = "" Then
        Set pQryFltr = Nothing
    Else
        Set pQryFltr = New QueryFilter
        pQryFltr.SubFields = ""
        pQryFltr.WhereClause = pFtrLyrDef.DefinitionExpression
    End If
    
    pFtrLyr.Name = pFtrLyr.Name + " (Features: " + CStr(pFtrLyr.FeatureClass.FeatureCount(pQryFltr)) + ")"
    pMxDoc.UpdateContents

End Sub
 
  Miles Hitchen
Software Engineer
Geospatial Team
Ordnance Survey
UK
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Showing record count in a legend 
Author Erin Whiteley 
Date Mar 13, 2006 
Message Thank you for taking the time to get that code for me, it's appreciated. Unfortunately, it's not quite the right thing.

I put this in a module like you said, and was able to run it fine. It added a feature count to the table of contents, but it doesn't appear in the legend. In addition, it's giving the feature count for the entire layer, but I'm looking for feature counts in each of 6 classes, so it's not quite the right thing.

Thank you for the code, but is there a slight variation that will count the records in each class? 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Showing record count in a legend 
Author Miles Hitchen 
Date Mar 13, 2006 
Message Hi Erin,

Can you tell me how you're symbolizing your data, ie are you using class breaks and if so, what field is used and are you using a normalization field?

The reason the count is not being displayed in the map legend is probably because the map legend was vreated before you ran the macro. Delete the map legend and the re-insert it, you should see the counts in the new legend.

Once you let me know the details of your symbology I'll modify the code for you.

Cheers,
Miles.
 
  Miles Hitchen
Software Engineer
Geospatial Team
Ordnance Survey
UK
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Showing record count in a legend 
Author Erin Whiteley 
Date Mar 14, 2006 
Message I am using custom classes on a field called "Pop2001". There is no normalization field. There are 6 classes in total, which are the following ranges:
0-500
500-1000
1000-2000
2000-4000
4000-10000
10000-30000

Thank you very much for your help Miles, this is very much appreciated, and let me know if you need any additional info.

Erin 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Showing record count in a legend 
Author Miles Hitchen 
Date Mar 14, 2006 
Message ** Code Edited **

Hi Erin,

The Macro below will add the feature count to all the class breaks in the selected layer. Add it to a VBA module as before, then select the layer in the TOC and then run the macro "LabelClassBreaks". Now you can add a legend to your map and the feature counts will be there.

Hope that helps,
Miles.

 
 
Public Sub LabelClassBreaks()
Dim pMxDoc As IMxDocument
Dim pGeoFtrLyr As IGeoFeatureLayer
Dim pClsBrkRend As IClassBreaksRenderer

    ' Get the Class Break Renderer of the selected layer
    Set pMxDoc = ThisDocument
    Set pGeoFtrLyr = pMxDoc.SelectedLayer
    Set pClsBrkRend = pGeoFtrLyr.Renderer
    
    ' Get the field name used in the class break
    ' and encapsulate it correctly
    Dim fldName As String
    fldName = pClsBrkRend.Field
    Dim pDataset As IDataset
    Set pDataset = pGeoFtrLyr.FeatureClass
    If pDataset.Workspace.WorkspaceFactory.WorkspaceType = esriFileSystemWorkspace Then
        fldName = """" + fldName + """"
    Else
        fldName = "[" + fldName + "]"
    End If
    
    ' Create a query filter
    Dim pQryFltr As IQueryFilter
    Set pQryFltr = New QueryFilter
    pQryFltr.SubFields = ""
    
    ' Loop through the breaks getting the record counts
    Dim ftrCnt As Long
    Dim cb As Long
    For cb = 0 To pClsBrkRend.BreakCount - 1
        If cb = 0 Then
            pQryFltr.WhereClause = fldName + " <= " + CStr(pClsBrkRend.Break(cb))
        Else
            pQryFltr.WhereClause = fldName + " > " + CStr(pClsBrkRend.Break(cb - 1)) + _
                " AND " + fldName + " <= " + CStr(pClsBrkRend.Break(cb))
        End If
        ftrCnt = pGeoFtrLyr.FeatureClass.FeatureCount(pQryFltr)
        pClsBrkRend.Label(cb) = pClsBrkRend.Label(cb) + " (RecCnt: " + CStr(ftrCnt) + ")"
    Next cb
    
    pMxDoc.UpdateContents

End Sub
 
  Miles Hitchen
Software Engineer
Geospatial Team
Ordnance Survey
UK
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Showing record count in a legend 
Author Erin Whiteley 
Date Mar 14, 2006 
Message That worked beautifully. Thank you Miles, I am very grateful for your help.

Erin 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Showing record count in a legend 
Author Kristy Goddard 
Date Feb 28, 2007 
Message Hi Miles,

I've been using your code to display the frequency count in a legend where the symbology uses a series of ranges. I would like to be able to use it for single symbols as well. Is it possible to modify this code so that it could be used either for range symbology or single symobology?

Kristy. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Showing record count in a legend 
Author Miles Hitchen 
Date Mar 01, 2007 
Message Hi Kristy,

I've modified the code to label a simple renderer as well as a class-break renderer. Also, I've modified the code so that the count will reflect any definition queries that might be setup on the layers.

Cheers,
Miles.

 
 
Public Sub LabelSimpleAndClassBreaks()
Dim pMxDoc As IMxDocument
Dim pGeoFtrLyr As IGeoFeatureLayer
Dim pSimpRend As ISimpleRenderer
Dim pClsBrkRend As IClassBreaksRenderer
Dim pTbl As ITable
Dim ftrCnt As Long

    ' Get the selected layer
    Set pMxDoc = ThisDocument
    Set pGeoFtrLyr = pMxDoc.SelectedLayer
    Set pTbl = pGeoFtrLyr
    
    ' Check the renderer type
    If TypeOf pGeoFtrLyr.Renderer Is ISimpleRenderer Then
    
        ' Simple Renderer
        Set pSimpRend = pGeoFtrLyr.Renderer
        ftrCnt = pTbl.RowCount(Nothing)
        pSimpRend.Label = pSimpRend.Label + " (RecCnt: " + CStr(ftrCnt) + ")"
    
    Else
        
        'Class-break renderer
        Set pClsBrkRend = pGeoFtrLyr.Renderer
        
        ' Get the field name used in the class break
        ' and encapsulate it correctly
        Dim fldName As String
        fldName = pClsBrkRend.Field
        Dim pDataset As IDataset
        Set pDataset = pGeoFtrLyr.FeatureClass
        If pDataset.Workspace.WorkspaceFactory.WorkspaceType = esriFileSystemWorkspace Then
            fldName = """" + fldName + """"
        Else
            fldName = "[" + fldName + "]"
        End If
        
        ' Create a query filter
        Dim pQryFltr As IQueryFilter
        Set pQryFltr = New QueryFilter
        pQryFltr.SubFields = ""
        
        ' Loop through the breaks getting the record counts
        Dim cb As Long
        For cb = 0 To pClsBrkRend.BreakCount - 1
            If cb = 0 Then
                pQryFltr.WhereClause = fldName + " <= " + CStr(pClsBrkRend.Break(cb))
            Else
                pQryFltr.WhereClause = fldName + " > " + CStr(pClsBrkRend.Break(cb - 1)) + _
                    " AND " + fldName + " <= " + CStr(pClsBrkRend.Break(cb))
            End If
            ftrCnt = pTbl.RowCount(pQryFltr)
            pClsBrkRend.Label(cb) = pClsBrkRend.Label(cb) + " (RecCnt: " + CStr(ftrCnt) + ")"
        Next cb
        
    End If
    
    ' Refresh the TOC
    pMxDoc.UpdateContents

End Sub


 
  Miles Hitchen
Software Engineer
Geospatial Team
Ordnance Survey
UK
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Showing record count in a legend 
Author Michael Slavnik 
Date Jun 06, 2007 
Message That works great thanks for the code.
I have one other request to add here :)
Is it possible to include another 'Else' to use this functionality with a Unique Value (Categories) Legend?

thanks 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Showing record count in a legend 
Author Raymond Wang 
Date Feb 27, 2008 
Message Hi I've patched many codes from the forums. It seems to be working now for UniqueValueRender.

What I needed was...
1) Take QueryDefinition into account
2) Do not reRender existing symbology

Simply run the Macro LabelCounts()

Note: Make sure the FLayer of interest is highlighted.

I'd love to see the code cleaned up. I'm not a professional programmer =) 
  CountMe.bas (opens in new window)
 
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Showing record count in a legend 
Author Tim Lomas 
Date Oct 21, 2009 
Message Raymond, thanks for this... worked great but I had to make a couple of modifications. 1) had to change the iCount variable in the GetLabelDescription function from Integer to Long (I had over 100,000 records that had been categorized and Integer couldn't handle that amount for some reason). 2) Had to set a definition query to eliminate null values.

After making those changes it worked beautifully. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Showing record count in a legend 
Author Raymond Wang 
Date Oct 21, 2009 
Message You're welcome, that's the power of the community. Keep up the good work. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Showing record count in a legend 
Author jef duncan 
Date Aug 28, 2009 
Message miles,

I am trying this, but I do have a normalization field.. my render is total_value (of a parcel) / total sq ft of parcel..

and I do have a query def

so does you code need a change for that case?

cause the numbers I get are not right..

thanks 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Showing record count in a legend 
Author Kathryn Clifton 
Date May 19, 2010 
Message I have been trying to use this code with a graduated color renderer on population data with a normalization by square kilometers, but if I run the code, it just returns zeros. Please help. Thank you. 
  Kathryn Clifton, GISP

City of Salisbury, NC
GIS Coordinator 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Showing record count in a legend 
Author Miles Hitchen 
Date May 19, 2010 
Message Hi Kathryn,

Could you post a sample of your data so that I can investigate what's happening? If you supply the data, it makes it much easier and therefore quicker, as I can spend my time looking at the code rather than inventing data to test it with.

Cheers,
Miles.
 
  Miles Hitchen
Software Engineer
Geospatial Team
Ordnance Survey
UK
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Showing record count in a legend 
Author Kathryn Clifton 
Date May 25, 2010 
Message Hi Miles. Here's a sample of the data. 
  Kathryn Clifton, GISP

City of Salisbury, NC
GIS Coordinator 
  sample.zip (opens in new window)
 
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Showing record count in a legend 
Author kit allen 
Date May 26, 2010 
Message Hi

I was just using this magnificent macro on my data and noticed that when I reverse sorted my ranges in the layer properties, the macro calculated the total in the previous order...
ie.
original:
1-2 (1)
2-3 (2)
3-4 (3)

reverse sorted:
3-4 (1)
2-3 (2)
1-2 (3)