How to create a selection set using a scatter plot graph (ArcObjects .NET 10.6 SDK)
ArcObjects Help for .NET developers > Developing with ArcGIS > Learning ArcObjects > Interacting with and configuring maps, layers, and graphics > Working with graphs > How to create a selection set using a scatter plot graph (ArcObjects .NET 10.6 SDK)

How to create a selection set using a scatter plot graph


Summary
The code in this topic shows how to create a scatter plot graph and the various ways to present the selection from the data source on the graph.

Creating a selection set using a scatter plot graph

To use the code in this topic, modify the following parameters:
  • pathToShapeFile—Path to shapefile to create scatter plot.
  • seriesFieldName—Numerical field to use as an x-coordinate in the scatter plot.
  • seriesFieldName2—Numerical field to use as a y-coordinate in the scatter plot.
  • whereClauseForSelection—Expression to build a selection query.
  • pathToOutImage—Output file containing the image of the type of selection handling; the image should contain a graph with the selection highlighted. Refer to the application programming interface (API) documentation for the appropriate format extension in IDataGraphBase.ExportToFile.
  • pathToOutImage2—Output file containing the image of the type of selection handling; the image should contain a graph with selected features only. Refer to the 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);
            SampleScatterPlot(@"path to your shapefile", "field name", "field name2",
                "whereClause", @"path to your output image", @
                "path to your output image2");
            ao.Shutdown();
        }

        static void SampleScatterPlot(String pathToShapeFile, String seriesFieldName,
            String seriesFieldName2, String whereClauseForSelection, String
            pathToOutImage, String pathToOutImage2)
        {

            // 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;

            // Create a feature layer from the input dataset.
            IFeatureLayer featureLayer=new FeatureLayerClass();
            featureLayer.FeatureClass=featureWorkspace.OpenFeatureClass
                (System.IO.Path.GetFileNameWithoutExtension(pathToShapeFile));

            // Select features using the input WhereClause. If there is an invalid expression, 
            // the function SelectFeatures throws an exeption.
            IQueryFilter queryFilter=new QueryFilterClass();
            queryFilter.WhereClause=whereClauseForSelection;
            IFeatureSelection featureSelection=(IFeatureSelection)featureLayer;
            try
            {
                featureSelection.SelectFeatures(queryFilter,
                    esriSelectionResultEnum.esriSelectionResultNew, false);
            }
            catch (Exception e)
            {
                return ;
            }

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

            // Set the graph selection handling to highlight the selection on a graph.
            dataGraphT.UseSelectedSet=true;
            dataGraphT.HighlightSelection=true;

            // Add the scatter plot series.
            ISeriesProperties seriesProps=dataGraphT.AddSeries("scatter_plot");
            seriesProps.SourceData=featureLayer;
            seriesProps.SetField(0, seriesFieldName);
            seriesProps.SetField(1, seriesFieldName2);

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

            // Export the graph to file (the format depends on the file extension).
            dataGraphT.ExportToFile(pathToOutImage);

            // Set the graph selection handling to build a graph only from selected features.
            dataGraphT.UseSelectedSet=true;
            dataGraphT.HighlightSelection=false;

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

            // Export the graph to file (the format depends on the file extension).
            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)
    SampleScatterPlot("path to your shapefile", "field name", "field name2", "whereClause", "path to your output image", "path to your output image2")
    ao.Shutdown()
End Sub

Shared Sub SampleScatterPlot(ByVal pathToShapeFile As String, ByVal seriesFieldName As String, ByVal seriesFieldName2 As String, ByVal whereClauseForSelection As String, ByVal pathToOutImage As String, ByVal pathToOutImage2 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)

' Create a feature layer from the input dataset.
Dim featureLayer As IFeatureLayer=New FeatureLayerClass()
featureLayer.FeatureClass=featureWorkspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(pathToShapeFile))

' Select features using input the WhereClause. If there is an invalid expression,
' the function SelectFeatures throws an exeption.
Dim queryFilter As IQueryFilter=New QueryFilterClass()
queryFilter.WhereClause=whereClauseForSelection
Dim featureSelection As IFeatureSelection=CType(featureLayer, IFeatureSelection)
Try
featureSelection.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultNew, False)
Catch e As Exception
Return
End Try

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

' Set the graph selection handling to highlight the selection on a graph.
dataGraphT.UseSelectedSet=True
dataGraphT.HighlightSelection=True

' Add the scatter plot series.
Dim seriesProps As ISeriesProperties=dataGraphT.AddSeries("scatter_plot")
seriesProps.SourceData=featureLayer
seriesProps.SetField(0, seriesFieldName)
seriesProps.SetField(1, seriesFieldName2)

' Update the data graph.
dataGraphT.Update(Nothing)

' Export the graph to file (the format depends on the file extension).
dataGraphT.ExportToFile(pathToOutImage)

' Set the graph selection handling to build a graph only from selected features.
dataGraphT.UseSelectedSet=True
dataGraphT.HighlightSelection=False

' Update the data graph.
dataGraphT.Update(Nothing)

' Export the graph to file (the format depends on the file extension).
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):
Development licensing Deployment licensing
ArcGIS Desktop Basic ArcGIS Desktop Basic
ArcGIS Desktop Standard ArcGIS Desktop Standard
ArcGIS Desktop Advanced ArcGIS Desktop Advanced