[GIS] Creating a graph from a Field values (VBA ArcObjects)

arcobjectsstatisticsvba

I have a combobox that shows all fields of a shapefile.

I want to get these values programmatically in a label–Count, Minimum, Maximum, Sum, Mean, Standard Deviation–and populate a graph of the selectable fields in the combobox. All that in the same form.


Thank you Jakub, it work, but sometimes it give an error that close all the ArcGIS opened windows.

I did some changes to the code.
This is my code:

Private Sub CmboFld_Change()
  Dim pMxDoc As esriArcMapUI.IMxDocument, pFLayer As esriCarto.IFeatureLayer, pData As esriGeoDatabase.IDataStatistics
  Dim pCursor As esriGeoDatabase.ICursor, pStatResults As esriSystem.IStatisticsResults
  Set pMxDoc = ThisDocument
  Set pFLayer = pMxDoc.FocusMap.Layer(0)
  Set pCursor = pFLayer.Search(Nothing, False)

  Set pData = New esriGeoDatabase.DataStatistics
  pData.Field = CmboFld.Text
  Set pData.Cursor = pCursor

  Dim pEnumVar As esriSystem.IEnumVariantSimple, value As Variant
  Set pEnumVar = pData.UniqueValues
  value = pEnumVar.Next

  Set pCursor = pFLayer.Search(Nothing, False)
  Set pData.Cursor = pCursor
  Set pStatResults = pData.Statistics
  Txt1.Caption = pStatResults.Maximum
  Txt2.Caption = pStatResults.Minimum
  Txt3.Caption = pStatResults.Sum
  Txt4.Caption = pStatResults.Mean
  Txt5.Caption = pStatResults.StandardDeviation
End Sub

and :

Private Sub UserForm_Initialize()
  '------------- Remplir la combobox------------------------------------
  'clear combobox
  CmboFld.Clear
  Dim i As Integer
  Dim pMxDoc As IMxDocument
  Set pMxDoc = ThisDocument
  Dim pFeatureLayer As IFeatureLayer
  Set pFeatureLayer = pMxDoc.FocusMap.Layer(0) '1st layer
  For i = 0 To pFeatureLayer.FeatureClass.Fields.FieldCount - 1
    CmboFld.AddItem pFeatureLayer.FeatureClass.Fields.Field(i).Name
  Next i
End Sub

It work, now I have a problem, how to populate a graph, I did used this:

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 = "Graphe"
    pDataGraphT.LegendProperties.Title = "Legende"
    pDataGraphBase.Name = "Graphe représentant - " & 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 = Form1.Dir & layerName & ".jpg"
        pDataGraphT.ExportToFile fileName
End Sub

Private Sub CmdGrph_Click()
    Graph
End Sub

It give me an empty graph, with a text zone "There is no selection"
when I double click at that zone text, it give me, graph properties
I choose this information:

Graph Type: Vertical Bar

Layer/Table: pMxDoc.FocusMap.Layer(0)

Value Field: CmboFld.Text

then it populate the graph.

I want to populate that Graph AUTOMATICALLY when I click at Graph Button.

Best Answer

iDataStatistics

Example here: http://edndoc.esri.com/arcobjects/9.2/ComponentHelp/esriGeoDatabase/IDataStatistics_Example.htm