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


How to create graph series with different color types (ArcObjects .NET 10.5 SDK)

How to create graph series with different color types


Summary
The code in this topic demonstrates different ways to apply color settings to a graph series (in this case, the horizontal bar graph). See esriGraphColorType Enum for more information on color types.

Creating a graph series with different color types

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

            // Add the first bar series to apply the esriGraphColorType.esriGraphColorCustomAll color mode.
            ISeriesProperties seriesProps=dataGraphT.AddSeries("bar:horizontal");
            seriesProps.SourceData=table;
            seriesProps.SetField(1, seriesFieldName);

            // Set multiple bar properties for the first series.
            IBarSeriesProperties barSeriesProps=(IBarSeriesProperties)seriesProps;
            barSeriesProps.MultipleBarType=esriMultiBarType.esriSideAllMultiBar;

            // Set color properties for the first series.
            seriesProps.Name="esriGraphColorCustomAll";
            seriesProps.ColorType=esriGraphColorType.esriGraphColorCustomAll;
            seriesProps.CustomColor=0x0000ff00;

            // Add the second bar series to apply the esriGraphColorType.esriGraphColorPalette color mode.
            seriesProps=dataGraphT.AddSeries("bar:horizontal");
            seriesProps.SourceData=table;
            seriesProps.SetField(1, seriesFieldName);

            // Set color properties for the second series.
            seriesProps.Name="esriGraphColorPalette";
            seriesProps.ColorType=esriGraphColorType.esriGraphColorPalette;
            seriesProps.ColorPalette="WindowsXP";

            // Add the third bar series to apply the esriGraphColorType.esriGraphColorCustomEach color mode.
            seriesProps=dataGraphT.AddSeries("bar:horizontal");
            seriesProps.SourceData=table;
            seriesProps.SetField(1, seriesFieldName);

            // Set color properties for the third series.
            seriesProps.Name="esriGraphColorCustomEach";
            seriesProps.ColorType=esriGraphColorType.esriGraphColorCustomEach;
            int oidField=table.FindField(table.OIDFieldName);
            ICursor tableCursor=table.Search(null, true);
            int color=0;
            while (true)
            {
                IRow row=tableCursor.NextRow();
                if (row != null)
                {
                    int oid=(int)row.get_Value(oidField);
                    seriesProps.set_Color(oid, color);
                    color += 20;
                }
                else
                    break;
            };

            // Add the fourth bar series to apply the esriGraphColorType.esriGraphColorMatch color mode.
            seriesProps=dataGraphT.AddSeries("bar:horizontal");

            // Create a feature layer from the input dataset.
            IFeatureLayer featureLayer=new FeatureLayerClass();
            featureLayer.FeatureClass=(IFeatureClass)table;
            seriesProps.SourceData=featureLayer;
            seriesProps.SetField(1, seriesFieldName);

            // Set color properties for the fourth series.
            seriesProps.Name="esriGraphColorMatch";
            seriesProps.ColorType=esriGraphColorType.esriGraphColorMatch;

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

Shared Sub SampleBarH(ByVal pathToShapeFile, ByVal seriesFieldName, ByVal pathToOutImage)

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

' Add the first bar series to apply the esriGraphColorType.esriGraphColorCustomAll color mode.
Dim seriesProps As ISeriesProperties=dataGraphT.AddSeries("bar:horizontal")
seriesProps.SourceData=table
seriesProps.SetField(1, seriesFieldName)

' Set multiple bar properties for the first series.
Dim barSeriesProps As IBarSeriesProperties=CType(seriesProps, IBarSeriesProperties)
barSeriesProps.MultipleBarType=esriMultiBarType.esriSideAllMultiBar

' Set color properties for the first series.
seriesProps.Name="esriGraphColorCustomAll"
seriesProps.ColorType=esriGraphColorType.esriGraphColorCustomAll
seriesProps.CustomColor=110000

' Add the second bar series to apply the esriGraphColorType.esriGraphColorPalette color mode.
seriesProps=dataGraphT.AddSeries("bar:horizontal")
seriesProps.SourceData=table
seriesProps.SetField(1, seriesFieldName)

' Set color properties for the second series.
seriesProps.Name="esriGraphColorPalette"
seriesProps.ColorType=esriGraphColorType.esriGraphColorPalette
seriesProps.ColorPalette="WindowsXP"

' Add the third bar series to apply the esriGraphColorType.esriGraphColorCustomEach color mode.
seriesProps=dataGraphT.AddSeries("bar:horizontal")
seriesProps.SourceData=table
seriesProps.SetField(1, seriesFieldName)

' Set color properties for the third series.
seriesProps.Name="esriGraphColorCustomEach"
seriesProps.ColorType=esriGraphColorType.esriGraphColorCustomEach
Dim oidField As Integer=table.FindField(table.OIDFieldName)
Dim tableCursor As ICursor=table.Search(Nothing, True)
Dim color As Integer=0
While (True)
    Dim row As IRow=tableCursor.NextRow()
    If Not row Is Nothing Then
        Dim oid As Integer=row.Value(oidField)
        color=seriesProps.Color(oid)
        color +=20
    Else
        Exit While
    End If
End While

' Add the fourth bar series to apply the esriGraphColorType.esriGraphColorMatch color mode.
seriesProps=dataGraphT.AddSeries("bar:horizontal")

' Create a feature layer from the input dataset.
Dim featureLayer As IFeatureLayer=New FeatureLayerClass()
featureLayer.FeatureClass=CType(table, IFeatureClass)
seriesProps.SourceData=featureLayer
seriesProps.SetField(1, seriesFieldName)

' Set color properties for the fourth series.
seriesProps.Name="esriGraphColorMatch"
seriesProps.ColorType=esriGraphColorType.esriGraphColorMatch

' 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