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


How to sort data in the graph (ArcObjects .NET 10.8 SDK)
ArcObjects Help for .NET developers > ArcObjects Help for .NET developers > Developing with ArcGIS > Learning ArcObjects > Interacting with and configuring maps, layers, and graphics > Working with graphs > How to sort data in the graph

How to sort data in the graph


Summary
The code in this topic demonstrates different ways to sort data in a bar or line graph. There are four types of sorting options: unsorted series, unsorted series with X field initialized, sorted series, and sorted series with X field initialized.

Sorting data in the graph

To use the code in this topic, modify the following parameters:
  • bCreateLineNotBar - The flag in selecting which type of graph to build (line or bar graph).
  • pathToShapeFile - The path to shapefile to build (line or bar graph).
  • seriesFieldName - The field used in building the line or bar graph.
  • seriesSortFieldName - The field used as an x-coordinate and/or sorting the field for line or bar graph.
  • pathToOutImage - The output file containing an image of the graph. See the application programming interface (API) documentation for the appropriate format extension in IDataGraphBase.ExportToFile.
  • pathToOutImage0 - The output file containing an image of the graph. See the API documentation for the appropriate format extension in IDataGraphBase.ExportToFile.
  • pathToOutImage1 - The output file containing an image of the graph. See the API documentation for the appropriate format extension in IDataGraphBase.ExportToFile.
  • pathToOutImage2 - The output file containing an image of the graph. See the API documentation for the appropriate format extension in IDataGraphBase.ExportToFile.
The code in this topic uses the data from the Geostatistical Analyst Tutorial folder as follows:
  • <your ArcGIS Install location>\ArcTutor\Geostatistics\ca_ozone_pts.shp - as the pathToShapeFile
  • ozone - as the seriesFieldName
  • longitude - as the seriesSortFieldName
To use other data, see the note comment in the following code example for code modification:
[C#]
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            IAoInitialize ao=new AoInitializeClass();
            ao.Initialize(esriLicenseProductCode.esriLicenseProductCodeBasic);
            SampleSortingBarLine(booleanLineNotBar, @"path to your shapefile", 
                "field name", "sort field name", @"path to your output image", @
                "path to your output image0", @"path to your output image1", @
                "path to your output image2");

            ao.Shutdown();
        }

        static void SampleSortingBarLine(bool bCreateLineNotBar, String
            pathToShapeFile, String seriesFieldName, String seriesSortFieldName,
            String pathToOutImage, String pathToOutImage0, String pathToOutImage1,
            String pathToOutImage2)
        {

            // 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 a data graph.
            IDataGraphT dataGraphT=new DataGraphTClass();

            // Add a bar or line series.
            ISeriesProperties seriesProps=dataGraphT.AddSeries(bCreateLineNotBar ?
                "line:vertical" : "bar:vertical");
            if (bCreateLineNotBar)
            {
                seriesProps.PenProperties.Width=4;
                ILineSeriesProperties lineSeriesProps=(ILineSeriesProperties)
                    seriesProps;
                lineSeriesProps.SymbolProperties.Style =
                    esriDataGraphTSymbolType.esriDataGraphTSymbolTriangle;
                lineSeriesProps.SymbolProperties.BorderProperties.Width=2;
                lineSeriesProps.SymbolProperties.BorderProperties.Color=0x00ff00;
            }

            // Assign color to features for showing changes in feature position on the graph.
            // Note: This is data-dependent code. Remove or modify the next eight lines 
            // if you use data other than the sample data referenced in this topic.
            seriesProps.WhereClause="fid < 10";
            seriesProps.ColorType=esriGraphColorType.esriGraphColorCustomEach;
            int color=0;
            for (int fid=0; fid < 10; fid++)
            {
                seriesProps.set_Color(fid, color);
                color += 20;
            }

            // 1. Make an unsorted series and export the graph as an image.
            dataGraphT.GeneralProperties.Title="Unsorted";
            seriesProps.SourceData=table;
            seriesProps.SetField(1, seriesFieldName);
            dataGraphT.Update(null);
            dataGraphT.ExportToFile(pathToOutImage);

            // 2. Make an unsorted series with X field initialized and export the graph as an image.
            dataGraphT.GeneralProperties.Title="Unsorted with X";
            seriesProps.SetField(0, seriesSortFieldName);
            seriesProps.SetField(1, seriesFieldName);
            dataGraphT.Update(null);
            dataGraphT.ExportToFile(pathToOutImage0);

            // 3. Make a sorted series and export the graph as an image.
            dataGraphT.GeneralProperties.Title="Sorted";
            seriesProps.SetField(0, "");
            seriesProps.SetField(1, seriesFieldName);
            IDataSortSeriesProperties sortProps=(IDataSortSeriesProperties)
                seriesProps;
            int index= - 1;
            sortProps.AddSortingField(seriesSortFieldName, true, ref index);
            dataGraphT.Update(null);
            dataGraphT.ExportToFile(pathToOutImage1);

            // 4. Make a sorted series with X field initialized and export the graph as an image.
            dataGraphT.GeneralProperties.Title="Sorted with X";
            seriesProps.SetField(0, seriesSortFieldName);
            seriesProps.SetField(1, seriesFieldName);

            // No need to add a sorting field here, as it was already done.
            sortProps.AddSortingField(seriesSortFieldName, true, ref index);
            dataGraphT.Update(null);
            dataGraphT.ExportToFile(pathToOutImage2);
        }
    }
}
[VB.NET]
Namespace ConsoleApplication1VBNET

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

Shared Sub SampleSortingBarLine(ByVal bCreateLineNotBar, ByVal pathToShapeFile, ByVal seriesFieldName, ByVal seriesSortFieldName, ByVal pathToOutImage, ByVal pathToOutImage0, ByVal pathToOutImage1, ByVal pathToOutImage2)

' 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 a data graph.
Dim dataGraphT As IDataGraphT=New DataGraphTClass()

' Add a bar or line series.
Dim seriesProps As ISeriesProperties
If bCreateLineNotBar=True Then
    seriesProps=dataGraphT.AddSeries("line:vertical")
ElseIf bCreateLineNotBar=False Then
    seriesProps=dataGraphT.AddSeries("bar:vertical")
End If
If (bCreateLineNotBar) Then
    seriesProps.PenProperties.Width=4
    Dim lineSeriesProps As ILineSeriesProperties=CType(seriesProps, ILineSeriesProperties)
    lineSeriesProps.SymbolProperties.Style=esriDataGraphTSymbolType.esriDataGraphTSymbolTriangle
    lineSeriesProps.SymbolProperties.BorderProperties.Width=2
    lineSeriesProps.SymbolProperties.BorderProperties.Color=110000
End If

' Assign color to features for showing changes in feature position on the graph.
' Note: This is data-dependent code. Remove or modify the next eight lines
' if you use data other than the sample data referenced in this topic.
seriesProps.WhereClause="fid < 10"
seriesProps.ColorType=esriGraphColorType.esriGraphColorCustomEach
Dim color As Integer=0
Dim fid As Integer
For fid=0 To 10 - 1 Step fid + 1
    color=seriesProps.Color(fid)
    color +=20
Next

' 1. Make an unsorted series and export the graph as an image.
dataGraphT.GeneralProperties.Title="Unsorted"
seriesProps.SourceData=table
seriesProps.SetField(1, seriesFieldName)
dataGraphT.Update(Nothing)
dataGraphT.ExportToFile(pathToOutImage)

' 2. Make an unsorted series with X field initialized and export the graph as an image.
dataGraphT.GeneralProperties.Title="Unsorted with X"
seriesProps.SetField(0, seriesSortFieldName)
seriesProps.SetField(1, seriesFieldName)
dataGraphT.Update(Nothing)
dataGraphT.ExportToFile(pathToOutImage0)

' 3. Make a sorted series and export the graph as an image.
dataGraphT.GeneralProperties.Title="Sorted"
seriesProps.SetField(0, "")
seriesProps.SetField(1, seriesFieldName)
Dim sortProps As IDataSortSeriesProperties=CType(seriesProps, IDataSortSeriesProperties)
Dim index As Integer=-1
sortProps.AddSortingField(seriesSortFieldName, True, index)
dataGraphT.Update(Nothing)
dataGraphT.ExportToFile(pathToOutImage1)

' 4. Make a sorted series with X field initialized and export the graph as an image.
dataGraphT.GeneralProperties.Title="Sorted with X"
seriesProps.SetField(0, seriesSortFieldName)
seriesProps.SetField(1, seriesFieldName)

' No need to add a sorting field here as it was already done.
sortProps.AddSortingField(seriesSortFieldName, True, ref index);
dataGraphT.Update(Nothing)
dataGraphT.ExportToFile(pathToOutImage2)
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):
Additional Requirements
  • <your ArcGIS Install location>\ArcTutor\Geostatistics\ca_ozone_pts.shp

Development licensing Deployment licensing
ArcGIS Desktop Basic ArcGIS Desktop Basic
ArcGIS Desktop Standard ArcGIS Desktop Standard
ArcGIS Desktop Advanced ArcGIS Desktop Advanced