Presentation given by David Parker (MVP) at the Visio Conference 2008 introducing Link Data to Shapes and Data Graphics ... still relevant for Visio 2010
3. Microsoft MVP (Visio)
Microsoft MCP (Visio, SQL)
Pre-Microsoft acquisition
Visio Business Partner, 1996-8
Speaker at Visio Conferences 1997 &1998
Started bVisual in 1998
Post-Microsoft acquisition
Microsoft Visio Solution Provider since 1999
Speaker at Microsoft Office Visio Conferences 2006
Visio Solution Provider Training for Microsoft EMEA
Visualizing Information with Microsoft Office Visio 2007
McGraw-Hill, 2007
• http://www.visualizinginformation.com
DataLegends – Visio add-in to add Data Graphic legends
• http://www.visiotoolbox.com/productdetail.aspx?productid=9
WBS Modeler for Visio and Project
www.wbsmodeler.com
Visio Examples
• http://www.bvisual.net/examples.htm
4.
5. On-line
Shape
Graphics
Data odc Prints
Source
Data
Visio Drawing Reports
Import > Link > Display > Communicate
6. Visual data
Microsoft Office Visio 2007 Standard
Database Wizard
Microsoft Office Visio 2007 Professional
Database Wizard
Database reverse engineering
UML models
Link Data To Shapes *
PivotDiagrams *
Data Graphics *
7. Visio Professional 2007
Shape Data
Renamed from Custom Properties
Link Data to Shapes
Multiple data sources per shape
Read only
Uses Office Data Connections (*.odc files)
PivotDiagrams
Diagrammatic version of PivotTables
Uses Office Data Connections (*.odc files)
Data Graphics
Data sensitive text and graphics on shapes
8. .NET Primary Interoperability Assemblies (PIAs)
Sample code
Save
As
Web
Visio Object Model (COM)
Data Diagramming
Tools & Wizards
Extensibility Support
Visio SDK
ML
VSTO Add-in
Full Visio Visio Drawing
Visio Viewer
COM Add-in Client App Control (ActiveX)
Add-ons
VBA
ShapeStudio
Drawings
ShapeSheet, Smart Shapes
Visio
Visio Engine
9. DataConnection and DataRecordsets
Visio 12.0 Type Lib
Add, AddFromConnectionFile, AddFromXML
UI and code links to:
Microsoft Office Excel & Access
SharePoint Services Lists
SQL Server Tables and Views
OLEDB & ODBC
Can also create link in code to :
SQL Server stored procedures
XML files
10. Declarations & Connection String
Dim dds As Visio.DataRecordset 'The data recordset
Dim ary() As String ‘Array to hold the p key columns
Dim SQLConnStr As String 'The connection string
Dim SQLCommStr As String 'The Command string
Dim datasetName As String 'The dataset name
SQLConnStr = "Provider=SQLOLEDB.1;" & _
"Integrated Security=SSPI;" & _
"Persist Security Info=True;" & _
"Data Source=.;" & _
"Initial Catalog=AdventureWorks;" & _
"Use Procedure for Prepare=1“
12. Connection String
Dim doc As Visio.Document
Dim dst As Visio.DataRecordset
Dim xmlFile As String
Dim dom As New MSXML2.DOMDocument
Dim OK As Boolean
Set doc = Visio.ActiveDocument
xmlFile = “MyXMLFile.xml"
OK = dom.Load(xmlFile)
Set dst = doc.DataRecordsets.AddFromXML( _
dom.XML, 0, “Any Name")
13. UI and code refreshing of DataRecordset
.Refresh
.GetAllRefreshConflicts
.GetMatchingRowsForRefreshConflict
.RemoveRefreshConflict
XML files can only be refreshed in code
.RefreshUsingXML
Automation requires assigned name and file
14. Dim doc As Visio.Document
Dim dst As Visio.DataRecordset
Dim xmlFile As String
Dim dom As New MSXML2.DOMDocument
Dim OK As Boolean
Set doc = Visio.ActiveDocument
xmlFile = “MyXMLFile.xml"
OK = dom.Load(xmlFile)
For Each dst In Visio.ActiveDocument.DataRecordsets
If dst.Name = “Any Name" Then
dst.RefreshUsingXML dom.XML
Exit For
End If
Next
15. External Data Window
Select a Master – Drag data row(s)
Drag a data row onto any shape
Shape Data key field(s) contain values -
AutoLink
16. What the user sees
Shape Data automatically added to shapes
Basic Data Graphics usually added by
default
17. Behind the scenes
User-defined Cells
Shape Data
_VisDM_ prefixes column names
Unless already exist!
19. Definition
DropManyLinkedU
ObjectsToInstance() array of variants
XYs() array of doubles
DataRecordsetID long
DataRowIDs() array of longs
ApplyDataGraphicAfterLink boolean
ShapeIDs() out array of longs
20. Example
'Retrieve the row ids from the datarecordset
Dim aryRowIDs() As Long
aryRowIDs = drs.GetDataRowIDs("")
'Drop the master in random co-ords, and linked to the
'datarecordset, applying a DataGraphic
Dim aryShapeIDs() As Long
Dim applyDataGraphics As Boolean
Visio.ActiveDocument.DocumentSheet.Cells( _
"User.msvLastAppliedDataGraphic").FormulaU = _
"=""Data - Employees"""
applyDataGraphics = True
Visio.ActivePage.DropManyLinkedU _
aryMsts, aryXYs, drs.ID, aryRowIDs, _
applyDataGraphics , aryShapeIDs
22. Using DataRecordsets in code
Many types of diagrams require shapes to
be connected
Organization Charts, Network diagrams,
Process
Connecting shapes example code
ConnectSubordinates
ConnectDottedLineManagers
Can then use different layouts for display
LayoutPage... example code
LayoutSelection... example code
23. Code excerpt
For iEmp = 0 To UBound(aryShapeIDs)
'Get the potential manager row
Set shpEmp = Visio.ActivePage.Shapes.ItemFromID(aryShapeIDs(iEmp))
rowID = shpEmp.GetLinkedDataRow(drs.ID)
varRowData = drs.GetRowData(rowID)
'Arrays are zero based
empID = CLng(varRowData(empColumn - 1))
'Filter the rows to get sub-ordinates of the current employee
aryRowIDs = drs.GetDataRowIDs("ManagerID = " & CStr(empID))
'Iterate thru the subordinate datarecordset rows
For iSub = 0 To UBound(aryRowIDs)
v = drs.GetRowData(aryRowIDs(iSub))
'Get the subordinate shapes
Visio.ActivePage.GetShapesLinkedToDataRow drs.ID, aryRowIDs(iSub), arySubShapeIDs
'Check that there is actually something in the array
If IsArrayAllocated(arySubShapeIDs) = True Then
'Iterate thru the subordinate shapes
For iSubShape = 0 To UBound(arySubShapeIDs)
Set shpSub = Visio.ActivePage.Shapes.ItemFromID(arySubShapeIDs(iSubShape))
'Create a dynamic connection from manager to subordinate
shpEmp.AutoConnect shpSub, visAutoConnectDirUp
Next iSubShape
End If
Next iSub
Next iEmp
24. Standard Hierachy Org Chart Page Layout
Modified with Selection Layout
cf Org Chart Wizard
27. 'Get the DataRecordset
Dim drs As DataRecordset
Set drs = Visio.ActiveWindow.Windows.ItemFromID( _
Visio.visWinIDExternalData).SelectedDataRecordset
...
‘Get the connection string
Dim conStr As String
conStr = drs.DataConnection.ConnectionString
‘Use the connection string to create a connection
Dim dbCnxn As New ADODB.Connection
dbCnxn.ConnectionString = conStr
dbCnxn.Open
dbCnxn.BeginTrans
...
30. What the user sees
Document stencil only shows visible
masters
Drawing Explorer displays all (but not type)
31. Behind the scenes
Public Sub EnumMasters()
Dim mst As Visio.Master
Debug.Print "ID", "Hidden", "Type", "IsDGCallout", "Name"
For Each mst In Visio.ActiveDocument.Masters
Debug.Print mst.ID, mst.Hidden, mst.Type, _
mst.Shapes(1).IsDataGraphicCallout, mst.name
Next mst
End Sub
32. Normal
Master
Master
GraphicItem
instance with
Master
DataGraphic
DataGraphic
Master
Document Stencil Document Page
33. Create a new DataGraphic master
Document.Masters.AddEx(visTypeDataGraphic)
Open the new Master
Master.Open
Copy an existing GraphicItem to it
.GraphicItems.AddCopy(existingGI)
Amend GraphicItems
.HorizontalPosition & .VerticalPosition
.SetExpression VisGraphicField, string
Close Master
34. SetDataBarMinMaxValues code
A Data Bar graphic item can be
Multiple times in same Data Graphic
In multiple Data Graphics
Each instance has different value ranges
Min and max values read from shape
instances
Prop. msvCalloutField
Data Bar can have multiple fields
E.g. Multi-bar graph
Prop. msvCalloutPropFieldn where n=2 to 5
Update master using .Open
35. 5. Update
DataGraphic
Master Data Bar
GraphicItem
Min/Max Values 4. Read
Min/Max of
2. Collect Data Bar
Data Bar Graphic Items
GraphicItems
1. Get Active
DataGraphic 3. Create Selection
Master
Document Stencil Document Page
36. Start Part
Const UserType As String = "User.msvCalloutType“
Const UserDGID As String = "User.visDGItemID "
Const PropMax As String = "Prop.msvCalloutPropMax"
Const PropMin As String = "Prop.msvCalloutPropMin“
'Get the active Data Graphic
Dim mstDG As Master
Set mstDG = GetActiveDataGraphic
im dicDataBarShapes As New Dictionary
Dim gi As Visio.GraphicItem
'Create a dictionary of data bars
For Each gi In mstDG.GraphicItems
If gi.Type = visTypeDataBar Then
dicDataBarShapes.Add CStr(gi.ID), 0
End If
Next gi
37. Middle Part
Dim colDataBarShapes As New Collection
Dim itmGraphic As Visio.shape
Dim gID As String
'Update the data bars dictionary with shape pseudo-index
For Each itmGraphic In mstDG.Shapes(1).Shapes
If itmGraphic.IsDataGraphicCallout = True Then
If itmGraphic.Cells(UserType).ResultStr("") = "Data Bar" Then
gID = CStr(itmGraphic.Cells(UserDGID).ResultInt("", 0))
dicDataBarShapes.Item(gID) = itmGraphic.NameU
colDataBarShapes.Add gID
End If
End If
Next itmGraphic
'Iterate thru each page to collect min/max values
For Each pag In Visio.ActiveDocument.Pages
'Get all shapes that use this data graphic master
Set sel = pag.CreateSelection(visSelTypeByDataGraphic, 0, mstDG)
...
Next shp
Next pag
38. End Part
...
'Finally - update the ItemGraphic in the DataGraphic Master
Dim mstCopy As Visio.Master
Set mstCopy = mstDG.Open
For Each itmGraphic In mstCopy.Shapes(1).Shapes
If itmGraphic.IsDataGraphicCallout = True Then
If itmGraphic.Cells(UserType).ResultStr("") = "Data Bar" Then
gID = CStr(itmGraphic.Cells(UserDGID).ResultInt("", 0))
itmGraphic.Cells(PropMax).FormulaU = "=" & dicMaxVal.Item(gID)
itmGraphic.Cells(PropMin).FormulaU = "=" & dicMinVal.Item(gID)
End If
End If
Next itmGraphic
'Close the copy to update all instances
mstCopy.Close
41. Breakdown
PivotDiagram is an add-on
Not an API extension
Only launched in code from SharePoint
Services List (soon to be published MSDN
article)
Use code to select in various ways
Aids in applying DataGraphics and extra
DataRecordsets
E.g. SelectSameBreakdownName sample
42. ...
Set vsoWindow = Visio.Application.ActiveWindow
If vsoWindow.Selection.Count > 0 Then
Set shp = vsoWindow.Selection.PrimaryItem
If IsPivotNode(shp) = True Then
breakdownName = _
GetGroupNameByID(GetBreakdownID(shp))
SelectByBreakdownName breakdownName
End If
End If
43. IsPivotNode = isPivotShapeType(shape, ePivotShapeType.Node)
Private Function isPivotShapeType(ByVal shape As Visio.shape, _
ByVal pivotShapeType As ePivotShapeType) As Boolean
If Not shape.CellExists(UserDDShapeType, _
Visio.VisExistsFlags.visExistsAnywhere) = 0 Then
If shape.Cells(UserDDShapeType).ResultIU = pivotShapeType Then
isPivotShapeType = True
Else
isPivotShapeType = False
End If
Else
isPivotShapeType = False
End If
End Function
47. David J Parker
Microsoft MVP (Visio)
http://www.bVisual.net
Book
http://www.visualizinginformation.com
davidp@bvisual.net
48. Visio 2007 SDK
http://msdn2.microsoft.com/en-us/library/ms409183.aspx
Visio 2007 Solution Development Workshop
http://www.microsoft.com/downloads/details.aspx?FamilyID=332c211e-
581e-4029-9839-8f45de10b4c2&DisplayLang=en
Visio Developer Portal
http://msdn2.microsoft.com/en-us/office/aa905478.aspx
Visio Insight blog
http://blogs.msdn.com/visio/
Visio MVP web site
http://visio.mvps.org/
My Visio Blog
http://bvisual.spaces.live.com/
51. Time End User Track Solution Developer Track
(Lassen Room) (Kodiak Room)
1:45pm - 2:45pm Integrating Visio 2007 with Visio 2007 as a Solutions
Yesterday Other Office Applications Development Platform
3:00pm - 4:00pm Get Creative with Data in Developing Visio Solutions with
Yesterday Visio 2007 Visual Studio Tools for Office
4:30pm - 5:30pm Understanding and Visio Solution Developer Pain
Yesterday Demystifying Visio Shapes Points
10:45am - 11:45am Visio Futures - API Insights
Today
1:15pm - 2:15pm Visualizing Information the Smart
Today (Diagram) Way
2:30pm - 3:30pm Going Further with Data
Today Connectivity
Visio.Application.DataFeaturesEnabled will return false for standard
Ignoring SolutionXML – not visibleData1, 2 and 3 were once the only visible shape data
Visio 2007 also has the ability to export to Xps and PdfXps is a zipped format which can be extended, I will show an example of enhancing the Xps output to include Shape Data for smarter communication.
In VBA, requires reference to Microsoft Scripting Runtime
Requires reference to MicrosoftXML
Requires reference to MicrosoftXMLNeed to know both the file and Name used when added
Can switch off automatic generation of graphics
Can switch off automatic generation of graphics
Remember that Blue rows are not inherited from the Master!Therefore, consider having named shape data in Master before linking.
Run code examples:CreateRecordsetDropEmployees
Setting these properties can improve speed in automationApplication.AutoLayoutApplication.InhibitSelectChangeApplication.ScreenUpdatingApplication.ShowChangesEnsure that "=""Data - Employees""“ exists
Run code examples:CreateRecordsetDropEmployees
Private Sub LayoutPage( _ByValplaceStyle As Visio.VisCellVals, _ByValrouteStyle As Visio.VisCellVals, _ByVallineRouteExt As Visio.VisCellVals)
Run code examples:ConnectSubordinatesDropEmployees
Needs reference to ADODB
Run code examples:
The order of the graphic items in the Z-order
Note that DataGraphic and GraphicItem masters are both hidden by defaultCan only really recognise a GraphicItem by its main shape
Cannot create new GraphicItems in code ... only copy existingConstant Value Description visGraphicPropertyLabel1The label of a shape data item.visGraphicExpression2The ShapeSheet formula of a shape data item.
User.msvCalloutIconNumber controls display of elements
Visio Toolbox for alternative icon sets using imageshttp://www.visiotoolbox.com/downloads.aspx?resourceid=2#atdimgtopNot as efficient as using shape geometry
Public Function GetActiveDataGraphic() As Visio.MasterDim mstName As String 'You can get the Data Graphics master from a particlar cell value If Visio.ActiveDocument.DocumentSheet.CellExistsU( _ "User.msvLastAppliedDataGraphic", Visio.visExistsAnywhere) <> 0 ThenmstName = Visio.ActiveDocument.DocumentSheet.Cells( _ "User.msvLastAppliedDataGraphic").ResultStr("") End If If Len(mstName) = 0 Then Set GetActiveDataGraphic = Nothing Else Set GetActiveDataGraphic = Visio.ActiveDocument.Masters(mstName) End If Exit FunctionEnd Function
Do this before saving a document
Data Visualization competency is for Information Worker SolutionsData Management Solutions is just another competency that bVisual has