Simple Column Chart


This sample creates a very basic column chart. The code does not specify an X or Y axis field forcing the DataGraph object to automatically select these. For column charts, use IDataGraph::FieldSet1 to specify the y-axis and IDataGraphProperties::XAxisLabelField to specify the x-axis.

This sample provides an simple introduction to creating charts programmatically. There are many different interfaces and members that can be used to create a chart.

How to use:
  1. Paste the code into VBA.
  2. Select a map layer in the table of contents.
  3. From the Macros dialog, run the CreateNewChart routine.
  4. The new data graphy automatically selects two fields to use.
Public Sub CreateNewChart()
    Dim pMxDoc As IMxDocument
    Dim pDataGraph As IDataGraph
    Dim pDataGraphProperties As IDataGraphProperties
    Dim pGraphWindow As IDataGraphWindow
    Dim pDataGraphs As IDataGraphs

    Set pMxDoc = Application.Document
    If pMxDoc.SelectedLayer Is Nothing Then Exit Sub
    If Not TypeOf pMxDoc.SelectedLayer Is IFeatureLayer Then Exit Sub
    'Create a new graph
    Set pDataGraph = New DataGraph
    'Set the default Table, DataGraph will select a default graph type and some fields
    Set pDataGraph.Table = pMxDoc.SelectedLayer
    'Specifically give the chart a name and title = & " Chart"
    Set pDataGraphProperties = pDataGraph 'QI
    pDataGraphProperties.Title = "My Chart"
    pDataGraphProperties.ShowXAxisLabels = True
    pDataGraphProperties.ShowLegend = True
    pDataGraphProperties.SubTitle = pDataGraph.FieldSet1 & " vs. " & pDataGraphProperties.XAxisLabelField
    'Associate the data graph with a data graph window
    Set pGraphWindow = New DataGraphWindow
    Set pGraphWindow.DataGraph = pDataGraph
    Set pGraphWindow.Application = Application
    'Add the graph to the system
    Set pDataGraphs = pMxDoc 'QI
    pDataGraphs.Add pDataGraph
End Sub