Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Statistical query group and sort

This code sample is available for these platforms:
View Sample on GitHub

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

screenshot

How to use the sample

You can add statistic definitions to the list using the combo boxes and "+" 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.

To change the order-by fields, check the box by the field you want to order by.

Use the button to change the sort order for the selected order-by field

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
  • QueryParameters
  • ServiceFeatureTable
  • StatisticDefinition
  • StatisticRecord
  • StatisticType
  • StatisticsQueryParameters
  • StatisticsQueryResult

About the data

This sample uses a Diabetes, Obesity, and Inactivity by US County feature layer.

Tags

group, result, sort, statistics, stats

Sample Code

<UserControl 
    x:Class="ArcGISRuntime.UWP.Samples.StatsQueryGroupAndSort.StatsQueryGroupAndSort"
    xmlns:local="using:ArcGISRuntime.UWP.Samples.StatsQueryGroupAndSort"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
    <UserControl.Resources>
        <CollectionViewSource x:Name="GroupedResultData"
                              Source="{Binding GroupedStatistics}"
                              IsSourceGrouped="True"/>
        <local:StringFormatConverter x:Key="StringFormatter"/>
    </UserControl.Resources>
    
    <Grid HorizontalAlignment="Center" VerticalAlignment="Center"
          MinWidth="300" MaxWidth="800"
          MinHeight="300" MaxHeight="800">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50"/>
            <ColumnDefinition Width="110"/>
            <ColumnDefinition Width="40"/>
            <ColumnDefinition Width="110"/>
            <ColumnDefinition Width="50"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="40"/>
            <RowDefinition Height="40"/>
            <RowDefinition MinHeight="50" Height="*"/>
            <RowDefinition Height="40"/>
            <RowDefinition Height="40"/>
            <RowDefinition MinHeight="50" Height="*"/>
            <RowDefinition Height="30"/>
            <RowDefinition MinHeight="50" Height="*"/>
            <RowDefinition Height="30"/>
            <RowDefinition MinHeight="50" Height="50"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Border Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="5" Grid.RowSpan="4"
                BorderBrush="Black" BorderThickness="2"/>
        <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"
                  HorizontalAlignment="Stretch"
                  Margin="5" 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"
                  HorizontalAlignment="Stretch"
                  Margin="5" VerticalAlignment="Center"/>
        <Button x:Name="AddStatisticButton"
                Grid.Row="1" Grid.Column="4"
                Margin="3"
                Content="+"
                Click="AddStatisticClicked"/>
        <ScrollViewer Grid.Row="2" Grid.ColumnSpan="5"
                 Margin="25,5">
            <ListBox x:Name="StatFieldsListBox"                 
                 Height="120">
                <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"
                HorizontalAlignment="Stretch" VerticalAlignment="Top"
                Content="Remove" 
                Height="30"
                Click="RemoveStatisticClicked"/>
        <TextBlock Text="Group by"
                   Grid.Row="4" Grid.ColumnSpan="2"
                   HorizontalAlignment="Left" VerticalAlignment="Bottom"
                   Margin="25,0,0,5"/>
        <ListBox x:Name="GroupFieldsListBox"
                 Grid.Row="5" Grid.ColumnSpan="5"
                 MinHeight="110"
                 HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                 Margin="25,0">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox Content="{Binding Name}"
                                  Checked="GroupFieldCheckChanged" Unchecked="GroupFieldCheckChanged"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <TextBlock Text="Order by"
                   Grid.Row="6" Grid.ColumnSpan="2"
                   HorizontalAlignment="Left" VerticalAlignment="Bottom"
                   Margin="25,0,0,5"/>
        <ListBox x:Name="OrderByFieldsListBox"
                 Grid.Row="7" Grid.ColumnSpan="5"
                 HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                 MinHeight="110"
                 Margin="25,0">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox IsChecked="{Binding OrderWith}">
                            <CheckBox.Content>
                                <TextBlock>
                                    <Run Text="{Binding OrderInfo.FieldName}"/>
                                    <Run Text=" ("/>
                                    <Run Text="{Binding OrderInfo.SortOrder}"/>
                                    <Run Text=")"/>
                                </TextBlock>
                            </CheckBox.Content>
                        </CheckBox>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Button x:Name="SortOrderButton"
                Grid.Row="8" Grid.Column="3" Grid.ColumnSpan="2"
                HorizontalAlignment="Center" VerticalAlignment="Top"
                Margin="0,5,25,0"
                Padding="5,0"
                Content="Change sort order"
                Click="ChangeFieldSortOrder"/>
        <Button x:Name="GetStatisticsButton"
                Grid.Row="9" Grid.Column="0" Grid.ColumnSpan="5"
                HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                Padding="10,0" Margin="25,5,30,5"
                Content="Get Statistics"
                Click="OnExecuteStatisticsQueryClicked" Height="40"/>

        <ScrollViewer x:Name="SmallResultsPanel" Grid.Row="10" Grid.ColumnSpan="5"
                      Margin="25,0">
            <ListView ItemsSource="{Binding Source={StaticResource GroupedResultData}}">
                <ListView.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.HeaderTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Key}"/>
                            </DataTemplate>
                        </GroupStyle.HeaderTemplate>
                    </GroupStyle>
                </ListView.GroupStyle>

                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ItemsControl ItemsSource="{Binding}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock HorizontalAlignment="Left">
                                        <Run FontWeight="Bold" Text="{Binding Key}"/>
                                        <Run Text=" : "/>
                                        <Run Text="{Binding Value, Converter={StaticResource StringFormatter}, ConverterParameter='{}{0:0,0}'}"/>
                                    </TextBlock>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </ScrollViewer>

        <ScrollViewer x:Name="LargeResultsPanel"
                      Grid.Row="0" Grid.Column="5" Grid.RowSpan="10"
                      Margin="15,0">
            <ListView Width="300"
                      ItemsSource="{Binding Source={StaticResource GroupedResultData}}">
                <ListView.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.HeaderTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Key}"/>
                            </DataTemplate>
                        </GroupStyle.HeaderTemplate>
                    </GroupStyle>
                </ListView.GroupStyle>

                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ItemsControl ItemsSource="{Binding}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock HorizontalAlignment="Left">
                                        <Run FontWeight="Bold" Text="{Binding Key}"/>
                                        <Run Text=" : "/>
                                        <Run Text="{Binding Value, Converter={StaticResource StringFormatter}, ConverterParameter='{}{0:0,0}'}"/>
                                    </TextBlock>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </ScrollViewer>
        
        <!--
        ============================
        Adaptive triggers
        ============================
        -->
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup>
                <!--
                ============================
                Handle screen less than 720 effective pixels
                ============================
                -->
                <VisualState>
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="0" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="SmallResultsPanel.Visibility" Value="Visible"/>
                        <Setter Target="LargeResultsPanel.Visibility" Value="Collapsed"/>
                    </VisualState.Setters>
                </VisualState>
                <!--
                ============================
                Handle screen over 720 effective pixels
                ============================
                -->
                <VisualState>
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="720" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="SmallResultsPanel.Visibility" Value="Collapsed"/>
                        <Setter Target="LargeResultsPanel.Visibility" Value="Visible"/>
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</UserControl>
// Copyright 2017 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.

using Esri.ArcGISRuntime.Data;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;

namespace ArcGISRuntime.UWP.Samples.StatsQueryGroupAndSort
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        "Statistical query group and sort results",
        "Data",
        "Query a feature table to get grouped, sorted statistics.",
        "")]
    public partial class StatsQueryGroupAndSort
    {
        // URI for the US states map service
        private Uri _usStatesServiceUri = new Uri("https://services.arcgis.com/jIL9msH9OI208GCb/arcgis/rest/services/Counties_Obesity_Inactivity_Diabetes_2013/FeatureServer/0");

        // US states feature table
        private FeatureTable _usStatesTable;

        // Collection of (user-defined) statistics to use in the query
        private ObservableCollection<StatisticDefinition> _statDefinitions = new ObservableCollection<StatisticDefinition>();

        // Selected fields for grouping results
        private List<string> _groupByFields = new List<string>();

        // Collection to hold fields to order results by
        private ObservableCollection<OrderFieldOption> _orderByFields = new ObservableCollection<OrderFieldOption>();

        public StatsQueryGroupAndSort()
        {
            InitializeComponent();

            // Initialize the US states feature table and populate UI controls
            Initialize();
        }

        private async void Initialize()
        {
            // Create the US states feature table
            _usStatesTable = new ServiceFeatureTable(_usStatesServiceUri);

            try
            {
                // Load the table
                await _usStatesTable.LoadAsync();

                // Fill the fields combo and "group by" list with fields from the table
                FieldsComboBox.ItemsSource = _usStatesTable.Fields;
                GroupFieldsListBox.ItemsSource = _usStatesTable.Fields;

                // Set the (initially empty) collection of fields as the "order by" fields list data source
                OrderByFieldsListBox.ItemsSource = _orderByFields;

                // Fill the statistics type combo with values from the StatisticType enum
                StatTypeComboBox.ItemsSource = Enum.GetValues(typeof(StatisticType));

                // Set the (initially empty) collection of statistic definitions as the statistics list box data source
                StatFieldsListBox.ItemsSource = _statDefinitions;
            }
            catch (Exception e)
            {
                await new MessageDialog(e.ToString(), "Error").ShowAsync();
            }
        }

        // Execute a statistical query using the parameters defined by the user and display the results
        private async void OnExecuteStatisticsQueryClicked(object sender, RoutedEventArgs e)
        {
            // Verify that there is at least one statistic definition
            if (_statDefinitions.Count == 0)
            {
                ShowMessage("Please define at least one statistic for the query.", "Statistical Query");
                return;
            }

            // Create the statistics query parameters, pass in the list of statistic definitions
            StatisticsQueryParameters statQueryParams = new StatisticsQueryParameters(_statDefinitions);

            // Specify the group fields (if any)
            foreach (string groupField in _groupByFields)
            {
                statQueryParams.GroupByFieldNames.Add(groupField);
            }

            // Specify the fields to order by (if any)
            foreach (OrderFieldOption orderBy in _orderByFields)
            {
                statQueryParams.OrderByFields.Add(orderBy.OrderInfo);
            }

            // Ignore counties with missing data
            statQueryParams.WhereClause = "\"State\" IS NOT NULL";

            try
            {
                // Execute the statistical query with these parameters and await the results
                StatisticsQueryResult statQueryResult = await _usStatesTable.QueryStatisticsAsync(statQueryParams);

                // Format the output for display of grouped results in the list view
                IEnumerable<IGrouping<string,IReadOnlyDictionary<string,object>>> groupedResults = statQueryResult.GroupBy(r => string.Join(", ", r.Group.Values), r => r.Statistics);

                // Apply the results to the list view data source
                GroupedResultData.Source = groupedResults;
            }
            catch (Exception ex)
            {
                await new MessageDialog(e.ToString(), "Error").ShowAsync();
            }
        }
        
        // Helper function to show a message
        private async void ShowMessage(string message, string title)
        {
            MessageDialog messageDialog = new MessageDialog(message, title);
            await messageDialog.ShowAsync();
        }

        // Handle when the check box for a "group by" field is checked on or off by adding or removing the field from the collection
        private void GroupFieldCheckChanged(object sender, RoutedEventArgs e)
        {
            // Get the check box that raised the event (group field)
            CheckBox groupFieldCheckBox = (CheckBox)sender;

            // Get the field name
            string fieldName = groupFieldCheckBox.Content.ToString();

            // See if the field is being added or removed from the "group by" list
            bool fieldAdded = groupFieldCheckBox.IsChecked == true;

            // See if the field already exists in the "group by" list
            bool fieldIsInList = _groupByFields.Contains(fieldName);

            // If the field is being added, and is NOT in the list, add it ...
            if (fieldAdded && !fieldIsInList)
            {
                _groupByFields.Add(fieldName);

                // Also add it to the "order by" list
                OrderBy orderBy = new OrderBy(fieldName, SortOrder.Ascending);
                OrderFieldOption orderOption = new OrderFieldOption(false, orderBy);
                _orderByFields.Add(orderOption);
            }
            // If the field is being removed and it IS in the list, remove it ...
            else if (!fieldAdded && fieldIsInList)
            {
                _groupByFields.Remove(fieldName);

                // Also check for this field in the "order by" list and remove if necessary (only group fields can be used to order results)
                OrderFieldOption orderBy = _orderByFields.FirstOrDefault(field => field.OrderInfo.FieldName == fieldName);
                if (orderBy != null)
                {
                    // Remove the field from the "order by" list
                    _orderByFields.Remove(orderBy);
                }
            }
        }

        // Create a statistic definition and add it to the collection based on the user selection in the combo boxes
        private void AddStatisticClicked(object sender, RoutedEventArgs e)
        {
            // Verify that a field name and statistic type has been selected
            if (FieldsComboBox.SelectedValue == null || StatTypeComboBox.SelectedValue == null) { return; }

            // Get the chosen field name and statistic type from the combo boxes
            string fieldName = FieldsComboBox.SelectedValue.ToString();
            StatisticType statType = (StatisticType)StatTypeComboBox.SelectedValue;

            // Check if this statistic definition has already be created (same field name and statistic type)
            StatisticDefinition existingStatDefinition = _statDefinitions.FirstOrDefault(def => def.OnFieldName == fieldName && def.StatisticType == statType);

            // If it doesn't exist, create it and add it to the collection (use the field name and statistic type to build the output alias)
            if (existingStatDefinition == null)
            {
                StatisticDefinition statDefinition = new StatisticDefinition(fieldName, statType, fieldName + "_" + statType.ToString());
                _statDefinitions.Add(statDefinition);
            }
        }

        // Toggle the sort order (ascending/descending) for the field selected in the sort fields list
        private void ChangeFieldSortOrder(object sender, RoutedEventArgs e)
        {
            // Verify that there is a selected sort field in the list
            OrderFieldOption selectedSortField = OrderByFieldsListBox.SelectedItem as OrderFieldOption;
            if (selectedSortField == null) { return; }

            // Create a new order field info to define the sort for the selected field
            OrderBy newOrderBy = new OrderBy(selectedSortField.OrderInfo.FieldName, selectedSortField.OrderInfo.SortOrder);
            OrderFieldOption newSortDefinition = new OrderFieldOption(true, newOrderBy);

            // Toggle the sort order from the current value
            if (newSortDefinition.OrderInfo.SortOrder == SortOrder.Ascending)
            {
                newSortDefinition.OrderInfo.SortOrder = SortOrder.Descending;
            }
            else
            {
                newSortDefinition.OrderInfo.SortOrder = SortOrder.Ascending;
            }

            // Add the new OrderBy at the same location in the collection and remove the old one
            _orderByFields.Insert(_orderByFields.IndexOf(selectedSortField), newSortDefinition);
            _orderByFields.Remove(selectedSortField);
        }

        // Remove the selected statistic definition from the list
        private void RemoveStatisticClicked(object sender, RoutedEventArgs e)
        {
            // Verify that there is a selected statistic definition
            if (StatFieldsListBox.SelectedItem == null) { return; }

            // Get the selected statistic definition and remove it from the collection
            StatisticDefinition selectedStat = StatFieldsListBox.SelectedItem as StatisticDefinition;
            _statDefinitions.Remove(selectedStat);
        }

        // Add the selected field in the "group by" list to the "order by" list
        private void AddSortFieldClicked(object sender, RoutedEventArgs e)
        {
            // Verify that there is a selected field in the "group by" list
            if (GroupFieldsListBox.SelectedItem == null) { return; }

            // Get the name of the selected field and ensure that it's in the list of selected group fields (checked on in the list, e.g.)
            string selectedFieldName = GroupFieldsListBox.SelectedItem.ToString();
            if (!_groupByFields.Contains(selectedFieldName))
            {
                ShowMessage("Only fields used for grouping can be used to order results.", "Order fields");
                return;
            }

            // Verify that the field isn't already in the "order by" list
            OrderFieldOption existingOrderBy = _orderByFields.FirstOrDefault(f => f.OrderInfo.FieldName == selectedFieldName);
            if (existingOrderBy == null)
            {
                // Create a new OrderBy for this field and add it to the collection (default to ascending sort order)
                OrderBy newOrderBy = new OrderBy(selectedFieldName, SortOrder.Ascending);
                OrderFieldOption orderField = new OrderFieldOption(false, newOrderBy);
                _orderByFields.Add(orderField);
            }
        }

        // Remove the selected field from the list of "order by" fields
        private void RemoveSortFieldClicked(object sender, RoutedEventArgs e)
        {
            // Verify that there is a selected item in the "order by" list
            if (OrderByFieldsListBox.SelectedItem == null) { return; }

            // Get the selected OrderFieldOption object and remove it from the collection
            OrderFieldOption selectedOrderBy = OrderByFieldsListBox.SelectedItem as OrderFieldOption;
            _orderByFields.Remove(selectedOrderBy);
        }        
    }

    // Simple class to describe an "order by" option
    public class OrderFieldOption
    {
        // Whether or not to use this field to order results
        public bool OrderWith { get; set; }

        // The order by info: field name and sort order
        public OrderBy OrderInfo { get; set; }

        public OrderFieldOption(bool orderWith, OrderBy orderInfo)
        {
            OrderWith = orderWith;
            OrderInfo = orderInfo;
        }
    }

    // Value converter to return strings in a specified format
    public class StringFormatConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            // Make sure there's a value
            if (value == null)
                return null;

            // Make sure there's a converter parameter (format string, "{0:0,0}", for example)
            if (parameter == null)
                return value;

            // Return the formatted string
            return string.Format((string)parameter, value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
}