This sample demonstrates performing a query and generating statistics for fields. The query is run when the sample loads.
In the sample's code-behind, a list of OutStatistic instances is created. Each OutStatistic uses OnStatisticField to specify the field to generate a statistic for, OutStatisticFieldName to specify the field to contain the statistic in the output, and StatisticType to specify the type of statistic to generate. This sample shows both StatisticType.Sum and StatisticType.Count. The query is then executed to generate the requested statistics, and the results are used to populate the DataGrid.
Download Sample Application
< UserControl x:Class = " ArcGISWPFSDK.Statistics "
xmlns = " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
xmlns:x = " http://schemas.microsoft.com/winfx/2006/xaml "
xmlns:d = " http://schemas.microsoft.com/expression/blend/2008 "
xmlns:mc = " http://schemas.openxmlformats.org/markup-compatibility/2006 "
xmlns:esri = " http://schemas.esri.com/arcgis/client/2009 " >
< Grid x:Name = " LayoutRoot " >
< ScrollViewer x:Name = " DataGridScrollViewer " HorizontalScrollBarVisibility = " Hidden " VerticalScrollBarVisibility = " Auto " >
< DataGrid x:Name = " OutStatisticsDataGrid " AutoGenerateColumns = " False " HeadersVisibility = " Column "
Background = " White " Loaded = " OutStatisticsDataGrid_Loaded " >
< DataGrid.Columns >
< DataGridTextColumn Width = " 110 " Binding = " {Binding Attributes[sub_region] } " Header = " US Region " />
< DataGridTextColumn Width = " 110 " Binding = " {Binding Attributes[numberofstates] } " Header = " State Count " />
< DataGridTextColumn Width = " 110 " Binding = " {Binding Attributes[subregionpopulation] } " Header = " Population " />
</ DataGrid.Columns >
</ DataGrid >
</ ScrollViewer >
</ Grid >
</ UserControl >
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client.Tasks;
namespace ArcGISWPFSDK
{
public partial class Statistics : UserControl
{
QueryTask queryTask;
public Statistics()
{
InitializeComponent();
queryTask = new QueryTask("http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/2" );
queryTask.ExecuteCompleted += queryTask_ExecuteCompleted;
}
private void OutStatisticsDataGrid_Loaded(object sender, RoutedEventArgs e)
{
Query query = new Query()
{
GroupByFieldsForStatistics = new List<string > { "sub_region" },
OutStatistics = new List<OutStatistic> {
new OutStatistic(){
OnStatisticField = "pop2000" ,
OutStatisticFieldName = "subregionpopulation" ,
StatisticType = StatisticType.Sum
},
new OutStatistic(){
OnStatisticField = "sub_region" ,
OutStatisticFieldName = "numberofstates" ,
StatisticType = StatisticType.Count
}
}
};
queryTask.ExecuteAsync(query);
}
void queryTask_ExecuteCompleted(object sender, QueryEventArgs e)
{
FeatureSet featureSet = e.FeatureSet;
if (featureSet != null && featureSet.Features.Count > 0)
{
OutStatisticsDataGrid.ItemsSource = featureSet.Features;
}
}
}
}
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client.Tasks
Namespace ArcGISWPFSDK
Partial Public Class Statistics
Inherits UserControl
Private queryTask As QueryTask
Public Sub New ()
InitializeComponent()
queryTask = New QueryTask("http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/2" )
AddHandler queryTask.ExecuteCompleted, AddressOf queryTask_ExecuteCompleted
End Sub
Private Sub OutStatisticsDataGrid_Loaded(sender As Object , e As RoutedEventArgs)
Dim query As New Query() With { _
.GroupByFieldsForStatistics = New List(Of String )() From { _
"sub_region" _
}, _
.OutStatistics = New List(Of OutStatistic)() From { _
New OutStatistic() With { _
.OnStatisticField = "pop2000" , _
.OutStatisticFieldName = "subregionpopulation" , _
.StatisticType = StatisticType.Sum _
}, _
New OutStatistic() With { _
.OnStatisticField = "sub_region" , _
.OutStatisticFieldName = "numberofstates" , _
.StatisticType = StatisticType.Count _
} _
} _
}
queryTask.ExecuteAsync(query)
End Sub
Private Sub queryTask_ExecuteCompleted(sender As Object , e As QueryEventArgs)
Dim featureSet As FeatureSet = e.FeatureSet
If featureSet IsNot Nothing AndAlso featureSet.Features.Count > 0 Then
OutStatisticsDataGrid.ItemsSource = featureSet.Features
End If
End Sub
End Class
End Namespace
5/16/2014