This sample demonstrates using the ArcGIS Runtime SDK for WPF to perform a query and generate 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.LocalStatistics "
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 " >
< DataGrid x:Name = " OutStatisticsDataGrid " AutoGenerateColumns = " False " HeadersVisibility = " Column "
Background = " White " >
< 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 2010 " />
</ DataGrid.Columns >
</ DataGrid >
</ ScrollViewer >
</ Grid >
</ UserControl >
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client.Tasks;
using ESRI.ArcGIS.Client.Local;
namespace ArcGISWPFSDK
{
public partial class LocalStatistics : UserControl
{
QueryTask queryTask;
LocalMapService _mapService;
public LocalStatistics()
{
InitializeComponent();
LocalMapService.GetServiceAsync("..\\Data\\MPKS\\USCitiesStates.mpk" , (localMapService) =>
{
_mapService = localMapService;
queryTask = new QueryTask();
queryTask.Url = _mapService.UrlMapService + "/2" ;
queryTask.ExecuteCompleted += queryTask_ExecuteCompleted;
queryTask.Failed += queryTask_Failed;
Query query = new Query()
{
GroupByFieldsForStatistics = new List<string > { "SUB_REGION" },
OutStatistics = new List<OutStatistic> {
new OutStatistic(){
OnStatisticField = "POP2010" ,
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;
}
}
void queryTask_Failed(object sender, TaskFailedEventArgs e)
{
MessageBox.Show(e.Error.Message.ToString());
}
}
}
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client.Tasks
Imports ESRI.ArcGIS.Client.Local
Namespace ArcGISWPFSDK
Partial Public Class LocalStatistics
Inherits UserControl
Private queryTask As QueryTask
Private _mapService As LocalMapService
Public Sub New ()
InitializeComponent()
LocalMapService.GetServiceAsync("..\Data\MPKS\USCitiesStates.mpk" , Function (localMapService__1)
_mapService = localMapService__1
queryTask = New QueryTask()
queryTask.Url = _mapService.UrlMapService & "/2"
AddHandler queryTask.ExecuteCompleted, AddressOf queryTask_ExecuteCompleted
AddHandler queryTask.Failed, AddressOf queryTask_Failed
Dim query As New Query() With { _
.GroupByFieldsForStatistics = New List(Of String )() From { _
"SUB_REGION" _
}, _
.OutStatistics = New List(Of OutStatistic)() From { _
New OutStatistic() With { _
.OnStatisticField = "POP2010" , _
.OutStatisticFieldName = "SubRegionPopulation" , _
.StatisticType = StatisticType.Sum _
}, _
New OutStatistic() With { _
.OnStatisticField = "SUB_REGION" , _
.OutStatisticFieldName = "NumberOfStates" , _
.StatisticType = StatisticType.Count _
} _
} _
}
queryTask.ExecuteAsync(query)
End Function )
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
Private Sub queryTask_Failed(sender As Object , e As TaskFailedEventArgs)
MessageBox.Show(e.[Error ].Message.ToString())
End Sub
End Class
End Namespace
5/16/2014