This document is archived and information here might be outdated.  Recommended version.


How to create multiple vertical bar graphs (ArcObjects .NET 10.5 SDK)

How to create multiple vertical bar graphs


Summary
The code in this topic shows how to create multiple bar-style vertical bar graphs and how to export the graphs to file.

Creating multiple vertical bar graphs

To use the code in this topic, modify the following parameters:
  • pathToShapeFile—Path to shapefile from which to create the series.
  • seriesFieldName—Numerical field used for creating the first bar series.
  • seriesFieldName2—Numerical field used for creating the second bar series.
  • 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.
[C#]
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            IAoInitialize ao=new AoInitializeClass();
            ao.Initialize(esriLicenseProductCode.esriLicenseProductCodeBasic);
            SampleBarV(@"path to your shapefile", "field name", "field name2", @
                "path to your output image");
            ao.Shutdown();
        }

        static void SampleBarV(String pathToShapeFile, String seriesFieldName,
            String seriesFieldName2, String pathToOutImage)
        {

            // Open the workspace for the input shapefile.
            IWorkspace shapefileWorkspace=null;
            IWorkspaceFactory shapefileWorkspaceFactory=new
                ShapefileWorkspaceFactoryClass();
            shapefileWorkspace=shapefileWorkspaceFactory.OpenFromFile
                (System.IO.Path.GetDirectoryName(pathToShapeFile), 0);
            IFeatureWorkspace featureWorkspace=(IFeatureWorkspace)
                shapefileWorkspace;

            // Get the table for the input shapefile.
            ITable table=(ITable)featureWorkspace.OpenFeatureClass
                (System.IO.Path.GetFileNameWithoutExtension(pathToShapeFile));

            // Create the data graph.
            IDataGraphT dataGraphT=new DataGraphTClass();

            // Add the first bar series.
            ISeriesProperties seriesProps=dataGraphT.AddSeries("bar:vertical");
            seriesProps.SourceData=table;
            seriesProps.SetField(1, seriesFieldName);

            // Set the bar properties for the first series.
            IBarSeriesProperties barSeriesProps=(IBarSeriesProperties)seriesProps;
            barSeriesProps.MultipleBarType=esriMultiBarType.esriSideAllMultiBar;
            barSeriesProps.BarStyle=esriBarStyle.esriRectGradientBar;
            barSeriesProps.BarSize=50;

            // Add the second bar series.
            seriesProps=dataGraphT.AddSeries("bar:vertical");
            seriesProps.SourceData=table;
            seriesProps.SetField(1, seriesFieldName2);
            seriesProps.PenProperties.Visible=true;

            // Set the bar properties for the second series.
            barSeriesProps=(IBarSeriesProperties)seriesProps;
            barSeriesProps.BarStyle=esriBarStyle.esriRectGradientBar;
            barSeriesProps.BarSize=50;

            // Update the data graph.
            dataGraphT.Update(null);

            // Export the graph to file (the format depends on the file extension).
            dataGraphT.ExportToFile(pathToOutImage);
        }
    }
}
[VB.NET]
Namespace ConsoleApplication1VBNET

Class Program
    Shared Sub Main(ByVal args() As String)
    Dim ao As IAoInitialize=New AoInitializeClass()
    ao.Initialize(esriLicenseProductCode.esriLicenseProductCodeBasic)
    SampleBarV("path to your shapefile", "field name", "field name2", "path to your output image")
    ao.Shutdown()
End Sub

Shared Sub SampleBarV(ByVal pathToShapeFile As String, ByVal seriesFieldName As String, ByVal seriesFieldName2 As String, ByVal pathToOutImage As String)

' Open the workspace for the input shapefile.
Dim shapefileWorkspace As IWorkspace=Nothing
Dim shapefileWorkspaceFactory As IWorkspaceFactory=New ShapefileWorkspaceFactoryClass()
shapefileWorkspace=shapefileWorkspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(pathToShapeFile), 0)
Dim featureWorkspace As IFeatureWorkspace=CType(shapefileWorkspace, IFeatureWorkspace)

' Get the table for the input shapefile.
Dim table As ITable=CType(featureWorkspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(pathToShapeFile)), ITable)

' Create the data graph.
Dim dataGraphT As IDataGraphT=New DataGraphTClass()

' Add the first bar series.
Dim seriesProps As ISeriesProperties=dataGraphT.AddSeries("bar:vertical")
seriesProps.SourceData=table
seriesProps.SetField(1, seriesFieldName)

' Set the bar properties for the first series.
Dim barSeriesProps As IBarSeriesProperties=CType(seriesProps, IBarSeriesProperties)
barSeriesProps.MultipleBarType=esriMultiBarType.esriSideAllMultiBar
barSeriesProps.BarStyle=esriBarStyle.esriRectGradientBar
barSeriesProps.BarSize=50

' Add the second bar series.
seriesProps=dataGraphT.AddSeries("bar:vertical")
seriesProps.SourceData=table
seriesProps.SetField(1, seriesFieldName2)
seriesProps.PenProperties.Visible=True

' Set the bar properties for the second series.
barSeriesProps=CType(seriesProps, IBarSeriesProperties)
barSeriesProps.BarStyle=esriBarStyle.esriRectGradientBar
barSeriesProps.BarSize=50

' 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






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