How to create a vertical stack area graph


Summary
The code in this topic demonstrates how to create stacked area graphs, turn off automatic scaling of an axis, and adjust legend positions and titles.

Creating a vertical stack area graph

To use the code in this topic, modify the following parameters:
  • pathToShapeFile—Path to shapefile to build the area graph.
  • seriesFieldName—Numerical field used to build the first series of the area graph.
  • seriesFileName2—Numerical field used to build the second series of the area graph.
  • 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 vertical stack area graph, see the following code:
[C#]
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            IAoInitialize ao = new AoInitializeClass();
            ao.Initialize(esriLicenseProductCode.esriLicenseProductCodeBasic);

            SampleAreaV(@"path to your shapefile", "field name", "field name2", @
                "path to your output image");

            ao.Shutdown();
        }

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

            // Open a 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();

            // Turn off automatic minimum calculation for the vertical y-axis.
            dataGraphT.get_AxisProperties(0).AutomaticMinimum = false;
            dataGraphT.get_AxisProperties(0).Minimum = 0;

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

            // Set area properties for the multiple area graph.
            IAreaSeriesProperties areaSeriesProps = (IAreaSeriesProperties)
                seriesProps;
            areaSeriesProps.MultipleAreaType =
                esriMultiAreaType.esriStackedMultiArea;

            // Add the second area series.
            seriesProps = dataGraphT.AddSeries("area:vertical");
            seriesProps.SourceData = table;
            seriesProps.SetField(1, seriesFieldName2);
            seriesProps.CustomColor = 0x0000ff00;
            seriesProps.Name = seriesFieldName2;

            // Set legend properties.
            dataGraphT.LegendProperties.Alignment =
                esriDataGraphTLegendAlignment.esriDataGraphTLegendTop;
            dataGraphT.LegendProperties.Title = "Stacking of";

            // 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)
    SampleAreaV("path to your shapefile", "field name", "field name2", "path to your output image")
    ao.Shutdown()
End Sub

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

' Open a 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()

' Turn off automatic minimum calculation for the vertical y-axis.
dataGraphT.AxisProperties(0).AutomaticMinimum = False
dataGraphT.AxisProperties(0).Minimum = 0

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

' Set area properties for the multiple area graph.
Dim areaSeriesProps As IAreaSeriesProperties = CType(seriesProps, IAreaSeriesProperties)
areaSeriesProps.MultipleAreaType = esriMultiAreaType.esriStackedMultiArea

' Add the second area series.
seriesProps = dataGraphT.AddSeries("area:vertical")
seriesProps.SourceData = table
seriesProps.SetField(1, seriesFieldName2)
seriesProps.CustomColor = 110000
seriesProps.Name = seriesFieldName2

' Set legend properties.
dataGraphT.LegendProperties.Alignment = esriDataGraphTLegendAlignment.esriDataGraphTLegendTop
dataGraphT.LegendProperties.Title = "Stacking of"

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