How to create a histogram graph


Summary
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:
[VB.NET]
Namespace ConsoleApplication1VBNET

Class Program
    Shared Sub Main(ByVal args() As String)
    Dim ao As IAoInitialize = New AoInitializeClass()
    ao.Initialize(esriLicenseProductCode.esriLicenseProductCodeBasic)
    SampleHistogram(@"path to your shapefile", "field name", binCount , @"path to your output image")
    ao.Shutdown()
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.SourceData = table
seriesProps.SetField(0, seriesFieldName)
' Set the histogram properties.
Dim histogramSeriesProps As IHistogramSeriesProperties = CType(seriesProps, IHistogramSeriesProperties)
histogramSeriesProps.BinCount = binCount
' Set titles.
dataGraphT.GeneralProperties.Title = "Histogram of " + seriesFieldName
dataGraphT.AxisProperties(0).Title = "COUNT"
' Update the data graph.
dataGraphT.Update(Nothing)
' Export the graph to file (the format depends on the file extension).
dataGraphT.ExportToFile(pathToOutImage)
End Sub

End Class

End Namespace
[C#]
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            IAoInitialize ao = new AoInitializeClass();
            ao.Initialize(esriLicenseProductCode.esriLicenseProductCodeBasic);
            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
                ShapefileWorkspaceFactoryClass();
            IWorkspace shapefileWorkspace;
            shapefileWorkspace = shapefileWorkspaceFactory.OpenFromFile
                (System.IO.Path.GetDirectoryName(pathToShapeFile), 0);
            IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)
                shapefileWorkspace;
            // Create a data graph.
            IDataGraphT dataGraphT = new DataGraphTClass();
            // Get the table for the input shapefile.
            ITable table = (ITable)featureWorkspace.OpenFeatureClass
                (System.IO.Path.GetFileNameWithoutExtension(pathToShapeFile));

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






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 for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced