How to create a histogram graph (ArcObjects .NET 10.6 SDK)
ArcObjects Help for .NET developers > Developing with ArcGIS > Learning ArcObjects > Interacting with and configuring maps, layers, and graphics > Working with graphs > How to create a histogram graph (ArcObjects .NET 10.6 SDK)

How to create a histogram graph

The code in this topic demonstrates how to create a histogram graph and adjust its properties.

Creating a histogram graph

To use the code in this topic, modify the following parameters:
  • pathToShapeFile—Path to shapefile to build the histogram.
  • seriesFieldName—Numerical field to use to build the histogram.
  • pathToOutImage—Output file containing an image of the graph. Refer to the application programming interface (API) documentation for the appropriate format extension in IDataGraphBase.ExportToFile).
To create a histogram graph and adjust its properties, see the following code:
Namespace ConsoleApplication1VBNET

Class Program
    Shared Sub Main(ByVal args() As String)
    Dim ao As IAoInitialize=New AoInitializeClass()
    SampleHistogram(@"path to your shapefile", "field name", binCount , @"path to your output image")
End Sub

Shared Sub SampleHistogram(ByVal pathToShapeFile As String, ByVal seriesFieldName As String, ByVal binCount As Integer, ByVal pathToOutImage As String)
' Open a workspace for the input shapefile.
Dim shapefileWorkspaceFactory As IWorkspaceFactory=New ShapefileWorkspaceFactoryClass()
Dim shapefileWorkspace As IWorkspace
shapefileWorkspace=shapefileWorkspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(pathToShapeFile), 0)
Dim featureWorkspace As IFeatureWorkspace=CType(shapefileWorkspace, IFeatureWorkspace)
' Create a data graph.
Dim dataGraphT As IDataGraphT=New DataGraphTClass()
' Get the table for the input shapefile.
Dim table As ITable=CType(featureWorkspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(pathToShapeFile)), ITable)

' Add the histogram series.
Dim seriesProps As ISeriesProperties=dataGraphT.AddSeries("bar:histogram")
seriesProps.SetField(0, seriesFieldName)
' Set the histogram properties.
Dim histogramSeriesProps As IHistogramSeriesProperties=CType(seriesProps, IHistogramSeriesProperties)
' Set titles.
dataGraphT.GeneralProperties.Title="Histogram of " + seriesFieldName
' Update the data graph.
' Export the graph to file (the format depends on the file extension).
End Sub

End Class

End Namespace
namespace ConsoleApplication1
    class Program
        static void Main(string[] args)
            IAoInitialize ao=new AoInitializeClass();
            SampleHistogram("path to your shapefile", "field name", binCount, 
                "path to your output image")ao.Shutdown();

        static void SampleHistogram(String pathToShapeFile, String seriesFieldName,
            int binCount, String pathToOutImage)
            // Open a workspace for the input shapefile.
            IWorkspaceFactory shapefileWorkspaceFactory=new
            IWorkspace shapefileWorkspace;
                (System.IO.Path.GetDirectoryName(pathToShapeFile), 0);
            IFeatureWorkspace featureWorkspace=(IFeatureWorkspace)
            // Create a data graph.
            IDataGraphT dataGraphT=new DataGraphTClass();
            // Get the table for the input shapefile.
            ITable table=(ITable)featureWorkspace.OpenFeatureClass

            // Add the histogram series.
            ISeriesProperties seriesProps=dataGraphT.AddSeries("bar:histogram");
            seriesProps.SetField(0, seriesFieldName);
            // Set the histogram properties.
            IHistogramSeriesProperties histogramSeriesProps=
            // Set titles.
            dataGraphT.GeneralProperties.Title="Histogram of " + seriesFieldName;
            // Update the data graph.
            // Export the graph to file (the format depends on the file extension).

To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcGIS Desktop Basic ArcGIS Desktop Basic
ArcGIS Desktop Standard ArcGIS Desktop Standard
ArcGIS Desktop Advanced ArcGIS Desktop Advanced