View in MAUI WPF WinUI View on GitHub

Query a feature table for statistics, grouping and sorting by different fields.

Image of statistical query group and sort

Use case

You can use statistical queries, grouping and sorting to process large amounts of data saved in feature tables. This is helpful for identifying trends and relationships within the data, which can be used to support further interpretations and decisions. For example, a health agency can use information on medical conditions occurring throughout a country to identify at-risk areas or demographics, and decide on further action and preventive measures.

How to use the sample

The sample will start with some default options selected. You can immediately click the “Get Statistics” button to see the results for these options. There are several ways to customize your queries:

  • You can add statistic definitions to the top-left table using the combo boxes and “Add” button. Select a table row and click “Remove” to remove a definition.
  • To change the Group-by fields, check the box by the field you want to group by in the bottom-left list view.
  • To change the Order-by fields, select a Group-by field (it must be checked) and click the ”>>” button to add it to the Order-by table. To remove a field from the Order-by table, select it and click the ”<<” button. To change the sort order of the Order-by field, the cells of the “Sort Order” column are combo-boxes that may be either ASCENDING or DESCENDING.

How it works

  1. Create a ServiceFeatureTable using the URL of a feature service and load the table.
  2. Get the feature tables field names list with featureTable.Fields.
  3. Create StatisticDefinitions specifying the field to compute statistics on and the StatisticType to compute.
  4. Create StatisticsQueryParameters passing in the list of statistic definitions.
  5. To have the results grouped by fields, add the field names to the query parameters’ GroupByFieldNames collection.
  6. To have the results ordered by fields, create OrderBys, specifying the field name and SortOrder. Pass these OrderBys to the parameters’ OrderByFields collection.
  7. To execute the query, call featureTable.QueryStatisticsAsync(queryParameters).
  8. Get the StatisticQueryResult. From this, you can get an iterator of StatisticRecords to loop through and display.

Relevant API

  • Field
  • OrderBy
  • QueryParameters
  • ServiceFeatureTable
  • StatisticDefinition
  • StatisticRecord
  • StatisticsQueryParameters
  • StatisticsQueryResult
  • StatisticType

About the data

This sample uses a Diabetes, Obesity, and Inactivity by US County feature layer hosted on ArcGIS Online.

Tags

correlation, data, fields, filter, group, sort, statistics, table

Sample Code

StatsQueryGroupAndSort.xaml StatsQueryGroupAndSort.xaml StatsQueryGroupAndSort.xaml.cs
<UserControl x:Class="ArcGIS.WinUI.Samples.StatsQueryGroupAndSort.StatsQueryGroupAndSort"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ArcGIS.WinUI.Samples.StatsQueryGroupAndSort">
<UserControl.Resources>
<local:StringFormatConverter x:Key="StringFormatter" />
</UserControl.Resources>
<Grid MinWidth="300"
MinHeight="300"
MaxWidth="1000"
MaxHeight="800"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid Grid.Row="0"
Grid.Column="0"
BorderBrush="Black"
BorderThickness="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
Grid.Column="1"
Grid.ColumnSpan="3"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="Statistics: US States" />
<TextBlock Grid.Row="1"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Text="Field:" />
<ComboBox x:Name="FieldsComboBox"
Grid.Row="1"
Grid.Column="1"
Margin="5"
HorizontalAlignment="Stretch"
VerticalAlignment="Center">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBlock Grid.Row="1"
Grid.Column="2"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Text="Type:" />
<ComboBox x:Name="StatTypeComboBox"
Grid.Row="1"
Grid.Column="3"
Margin="5"
HorizontalAlignment="Stretch"
VerticalAlignment="Center" />
<Button x:Name="AddStatisticButton"
Grid.Row="1"
Grid.Column="4"
Margin="3"
Click="AddStatisticClicked"
Content="+" />
<ScrollViewer Grid.Row="2"
Grid.ColumnSpan="5"
Margin="25,5"
MinHeight="100">
<ListBox x:Name="StatFieldsListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding OnFieldName}" />
<Run Text=" (" />
<Run Text="{Binding StatisticType}" />
<Run Text=")" />
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
<Button x:Name="RemoveStatField"
Grid.Row="3"
Grid.Column="3"
Grid.ColumnSpan="2"
Height="30"
Margin="25,5"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Click="RemoveStatisticClicked"
Content="Remove" />
</Grid>
<Grid Grid.Row="1"
Grid.Column="0"
VerticalAlignment="Top"
Margin="0,25">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="200" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
Margin="0,0,0,5"
HorizontalAlignment="Left"
VerticalAlignment="Bottom"
Text="Group by" />
<ListBox x:Name="GroupFieldsListBox"
Grid.Row="1"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Checked="GroupFieldCheckChanged"
Content="{Binding Name}"
Unchecked="GroupFieldCheckChanged" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel Grid.Row="1"
Grid.Column="1"
VerticalAlignment="Center"
Margin="25,0">
<Button Content="&gt;"
HorizontalAlignment="Center"
Margin="0,0,0,5"
Click="AddSortFieldClicked" />
<Button Content="&lt;"
HorizontalAlignment="Center"
Click="RemoveSortFieldClicked" />
</StackPanel>
<TextBlock Grid.Row="0"
Grid.Column="2"
Margin="0,0,0,5"
HorizontalAlignment="Left"
VerticalAlignment="Bottom"
Text="Order by" />
<ListBox x:Name="OrderByFieldsListBox"
Grid.Row="1"
Grid.Column="2"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding FieldName}" />
<Run Text=" (" />
<Run Text="{Binding SortOrder}" />
<Run Text=")" />
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button x:Name="SortOrderButton"
Grid.Row="2"
Grid.Column="2"
Margin="0,5,0,0"
Padding="5,0"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Click="ChangeFieldSortOrder"
Content="Change sort order" />
</Grid>
<Button x:Name="GetStatisticsButton"
Grid.Row="2"
Grid.Column="0"
Height="40"
Margin="25,5,30,5"
Padding="10,0"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
Click="OnExecuteStatisticsQueryClicked"
Content="Get Statistics" />
<Grid Grid.Row="0"
Grid.RowSpan="3"
Grid.Column="2"
Margin="15,0">
<ListBox x:Name="ResultsList">
<ListBox.ItemTemplate>
<DataTemplate>
<ListView ItemsSource="{Binding Item2}">
<ListView.Header>
<TextBlock Text="{Binding Item1}" FontSize="18" />
</ListView.Header>
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock TextWrapping="Wrap">
<Run Text="{Binding Item1}" FontWeight="SemiBold" />
<Run Text="{Binding Item2}" />
</TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>
</UserControl>