Adding the Sixth Legend Icon in Visio 2010

In my previous two articles, I described how to have 6 icons in a Data Graphic Icon Set, and provided some VBA code to make your own icons sets from existing Visio shapes or images. One thing that I overlooked was the Insert Legend command in Visio 2010, because it does not add the sixth icon automatically, so this blog describes how to add it. As a side effect, the code also enables the legend labels of icon and CBV (Color By Value) items to be edited.

image

When you use the Insert Legend command, it examines the Data Graphics used on the active page, to create legends for each Icon Set or Color By Value settings that it finds. Closer examination of the legend shapes, shows that an instance of the Icon Set master shape is added for each of the icons found.

image

So, my method for having a sixth icon as the default if a match is not found present a problem, because it is not displayed automatically.

However, it is quite simple to add the sixth one. Select the icon above the one that is missing, then enter CTL+D to duplicate the shape, and notice that it will be added below it.

image image

So, now there are two icons that look the same, and are both labelled the same.

So, I decided to write a small macro to provide a simple way to display the default icon, and to change the text.

Firstly, the macro needs to recognise if the selected shape is actually a legend shape item for an icon set. This can be tested by checking that the cell User.msvDGLegendShapeType = “IconItem”

The label text is stored in the User.msvDGLegendText cell.

image

If the legend item is for an icon set, then the sub-shape has the cell User.msvCalloutType with the value “Icon Set” :

image

The displayed icon is controlled by the value in the User.msvCalloutIconNumber cell.

So, I wrote a macro, ChangeLegendTextOrIcon , that makes these changes simple:

Public Sub ChangeLegendTextOrIcon()
Dim shpLegendItem As Visio.Shape
Dim shpIconSet As Visio.Shape
Dim txt As String
Dim sNumber As String
Dim iNumber As Integer

    'Abort if no shape or too many shapes selected
    If Not ActiveWindow.Selection.Count = 1 Then
        Exit Sub
    End If
    
    'Abort if selected shape is not a legend item
    If ActiveWindow.Selection.PrimaryItem.CellExists("User.msvDGLegendShapeType", Visio.visExistsAnywhere) = 0 Then
        Exit Sub
    Else
        Set shpLegendItem = ActiveWindow.Selection.PrimaryItem
    End If
    
    'Prompt to change the label text
    txt = InputBox("Enter the desired label text", "Change Legend Text", _
        shpLegendItem.Cells("User.msvDGLegendText").ResultStr(""))
    shpLegendItem.Cells("User.msvDGLegendText").FormulaU = "=""" & txt & """"
    
    'Prompt to change the icon number (not for CBVItem)
    If shpLegendItem.Cells("User.msvDGLegendShapeType").ResultStr("") = "IconItem" Then
        Set shpIconSet = shpLegendItem.shapes(1)
        sNumber = InputBox("Enter the icon number ( 0 to 4 normally, or anything else for the default) ", _
            "Change Legend Icon", Int(shpIconSet.Cells("User.msvCalloutIconNumber").ResultIU))
        If IsNumeric(sNumber) Then
            iNumber = Int(sNumber)
            shpIconSet.Cells("User.msvCalloutIconNumber").FormulaU = "=" & iNumber
        Else
            MsgBox "You must enter a number", vbInformation
        End If
    End If
    
End Sub

image

Then enter the desired icon number. For example, enter 5 to display the default icon:

image

The legend will now be changed as desired:

image

 

Similarly, you can use the macro to change the label of a CBV legend item. In this example, the header is not displayed on one line because the width of the column is controlled by the widest item. So, I selected one item and ran the macro:

image

I then added some spaces after the text:

image

This forced the whole column to be widened, thus providing enough width for the header text:

image

I have added the code to to the stencil from the previous blog – bVisualIconSetMaker.vss :

https://skydrive.live.com/redir.aspx?cid=3350d61bc93733a9&resid=3350D61BC93733A9!2548&parid=3350D61BC93733A9!197&authkey=!AJaBnvdIOg8NVe4

One Response to “Adding the Sixth Legend Icon in Visio 2010”


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Chris Webb's BI Blog

Microsoft Analysis Services, MDX, DAX, Power Pivot, Power Query and Power BI

davecra.wordpress.com/

Microsoft Office 365 Development, and more...

PowerShell.Amsterdam

Automate, Accelerate, Accurate

johnvisiomvp

Life with Visio and other Microsoft Toys!

Title (Required)

Windows Server Essentials Tips & Tricks

Nilsandrey's Weblog

Just another WordPress.com weblog

Things that Should be Easy

Every so often (too often in the IT industry) I encounter things that should have been very easy to do but turned out to be far too complicated. My favorite topics include SharePoint, .Net development, and software architecture, especially distributed systems.

Visio Guy

Shapes, Stencils, Drawings Templates, Tutorials, Tips & Developer Info for Microsoft Visio

Hannes's Virtual Earth

Tips & Tricks around Mapping and Cloud Computing

Pluralsight blog

be smart, be clear, be visual ...

%d bloggers like this: