English

How To: Insert a data graph into the page layout using Visual Basic for Applications and ArcObjects

Summary

Instructions provided describe how to insert a data graph into ArcMap's page layout.

Note:
The following procedure does not apply to ArcGIS version 9.2 and higher.

Procedure

The code sample uses an envelope to determine the location for the data graph. Modify the envelope's xmin, xmax, ymin, and ymax coordinates to fit the map.

  1. Open ArcMap.
  2. Open the Visual Basic Editor.

    In ArcMap, select Tools > Macros > Visual Basic Editor.

  3. In the Project Explorer window, expand Project.mxt and select ArcMap Objects > ThisDocument, right-click and select View Code.
    [O-Image] Visual Basic Project  Explorer

    Note:
    Code in the project's ThisDocument code module only runs in the current map document. To store the code in all the map documents, open the Normal.mxt ThisDocument code module instead.

  4. Paste the following code into the code module:

    Code:

    Sub InsertGraph()
    Dim pDoc As IMxDocument
    Set pDoc = ThisDocument
    'Qi for IDataGraphs
    Dim pDataGraphs As IDataGraphs
    Set pDataGraphs = pDoc

    'Name of the graph to insert
    Dim strGraphName As String
    strGraphName = "Graph of Sw"

    'Loop through the data graphs
    'and find the one to insert.
    Dim pDataGraph As IDataGraph
    For i = 0 To pDataGraphs.Count - 1
    'Exit the loop once the correct graph
    'is found.
    If pDataGraphs.Item(i).Name = strGraphName Then
    Set pDataGraph = pDataGraphs.Item(i)
    Exit For
    End If
    Next i

    'Check to make sure a graph is found.
    If pDataGraph Is Nothing Then
    MsgBox "No graph named " & strGraphName & " exists"
    End If

    'Get the DataGraphElement.
    Dim pDGElem As IDataGraphElement
    Set pDGElem = New DataGraphElement
    Set pDGElem.DataGraph = pDataGraph
    Dim pElem As IElement
    Set pElem = pDGElem
    'Insert the graph into the page layout.
    Dim pGc As IGraphicsContainer
    Set pGc = pDoc.PageLayout
    'Create an envelope that defines
    'the location to add the data graph.
    Dim pEnv As IEnvelope
    Set pEnv = New Envelope
    'Set xmin, xmax, ymin, ymax.
    pEnv.XMin = 0.924503
    pEnv.XMax = 4.343488
    pEnv.YMin = 0.785541
    pEnv.YMax = 2.949117
    pElem.Geometry = pEnv
    'Add the graph to the layout's graphic container.
    pGc.AddElement pElem, 0
    'Refresh the view.
    pDoc.ActiveView.Refresh

    End Sub

  5. Close the Visual Basic Editor.
  6. Run the code.

    A. Click Tools > Macros > Macros to display the Macros dialog box.
    B. Select a macro and click Run.