How to create a simple line graph


You can use this sample's code to quickly create a simple line graph.

How to use

  1. Add the data into ArcMap. You may use "TimSerTool" from your developer kit installation location in the Samples/Data/StreamFlowDateTime folder.
  2. Add the code into a module in your Visual Basic Editor(Tools>Marcos) in ArcMap.
  3. Run the procedure. You should see a line graph pop up in a separate graph window.
[VBA]
Private Sub Graph()
    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    
    ' get the name of the layer containing feature points
    Dim pLayer As ILayer
    Set pLayer = pMxDoc.FocusMap.Layer(0)
    
    ' create graph
    Dim pDataGraphBase As IDataGraphBase
    Dim pDataGraphT As IDataGraphT
    Set pDataGraphBase = New DataGraphT
    Set pDataGraphT = pDataGraphBase
    
    Dim layerName As String
    layerName = pLayer.Name
    
    ' graph and legend titles
    pDataGraphT.GeneralProperties.Title = "Graph"
    pDataGraphT.LegendProperties.Title = "Legend"
    pDataGraphBase.Name = "Graph of " & layerName
    
    ' create vertical line series
    Dim pSP As ISeriesProperties
    Set pSP = pDataGraphT.AddSeries("line:vertical")
    pSP.colorType = esriGraphColorMatch
    pSP.WhereClause = "GAGE_NO_ = 2 AND YEAR_ = 99"
    pSP.InLegend = True
    
    pSP.SourceData = pLayer
    pSP.SetField 0, "TSDateTime"
    pSP.SetField 1, "TSValue"
    Dim pSortFlds As IDataSortSeriesProperties
    Set pSortFlds = pSP
    Dim idx As Long
    pSortFlds.AddSortingField "TSDateTime", True, idx
    
    Dim pCancelTracker As ITrackCancel
    Set pCancelTracker = New CancelTracker
    pDataGraphT.Update pCancelTracker
    
    ' create data graph window within ArcMap
    Dim pDGWin As IDataGraphWindow2
    Set pDGWin = New DataGraphWindow
    Set pDGWin.DataGraphBase = pDataGraphBase
    Set pDGWin.Application = ThisDocument.Parent
    pDGWin.Show (True)
    
    Dim pDataGraphs As IDataGraphCollection
    Set pDataGraphs = pMxDoc
    pDataGraphs.AddDataGraph pDataGraphBase
    
    '    ' export the graph instead of displaying in ArcMap, use the following code,
    '    ' and comment the above 9 lines
    '    Dim fileName As String
    '    fileName = "c:\" + "graph.jpg"
    '    pDataGraphT.ExportToFile fileName
    
End Sub