Statistical query group and sort

View inWPFUWPFormsiOSAndroid
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 tap 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 tap "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 tap the ">>" button to add it to the Order-by table. To remove a field from the Order-by table, select it and tap 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.cs
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
// 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 System;
using System.Collections.Generic;
using System.Linq;
using CoreGraphics;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Http;
using Foundation;
using UIKit;

namespace ArcGISRuntime.Samples.StatsQueryGroupAndSort
{
    [Register("StatsQueryGroupAndSort")]
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        name: "Statistical query group and sort",
        category: "Data",
        description: "Query a feature table for statistics, grouping and sorting by different fields.",
        instructions: "The sample will start with some default options selected. You can immediately tap the \"Get Statistics\" button to see the results for these options. There are several ways to customize your queries:",
        tags: new[] { "correlation", "data", "fields", "filter", "group", "sort", "statistics", "table" })]
    public class StatsQueryGroupAndSort : UIViewController
    {
        // Hold references to UI controls.
        UIButton _showStatDefinitionsButton;
        UIButton _showGroupFieldsButton;
        UIButton _showOrderByFieldsButton;

        // URI for the US states map service.
        private readonly 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;

        // List of field names from the table.
        private List<string> _fieldNames;

        // Selected fields for grouping results.
        private Dictionary<string, bool> _groupByFields = new Dictionary<string, bool>();

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

        // Model for defining choices in the statistics definition UIPickerView.
        private StatDefinitionModel _statsPickerModel;

        // List of statistics definitions to use in the query.
        private readonly List<StatisticDefinition> _statisticDefinitions = new List<StatisticDefinition>();

        public StatsQueryGroupAndSort()
        {
            Title = "Statistical query group and sort";
        }

        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 field names from the table.
                _fieldNames = _usStatesTable.Fields.Select(field => field.Name).ToList();

                // Create a model that will provide statistic definition choices for the picker.
                _statsPickerModel = new StatDefinitionModel(_fieldNames.ToArray());

                // Create a list of fields the user can select for grouping.
                // Value is initially false, since no fields are selected by default.
                _groupByFields = _fieldNames.ToDictionary(name => name, name => false);
            }
            catch (Exception e)
            {
                new UIAlertView("Error", e.ToString(), (IUIAlertViewDelegate) null, "OK", null).Show();
            }
        }

        private void ShowGroupFields(object sender, EventArgs e)
        {
            // Create a new table.
            UITableViewController fieldsTable = new UITableViewController(UITableViewStyle.Plain)
            {
                // Set the data source on the table.
                TableView = {Source = new GroupFieldsDataSource(_groupByFields)}
            };

            // Show the table view.
            NavigationController.PushViewController(fieldsTable, true);
        }

        // Show fields the user can choose to sort results with (must be one of the group by fields).
        private void ShowOrderByFields(object sender, EventArgs e)
        {
            // Create a new table.
            UITableViewController sortFieldsTable = new UITableViewController(UITableViewStyle.Plain);

            // Get the current list of group fields and create/update the sort field choices.
            List<KeyValuePair<string, bool>> sortFieldChoices = _groupByFields.Where(field => field.Value).ToList();
            foreach (KeyValuePair<string, bool> sortChoice in sortFieldChoices)
            {
                // If this group field is not in the list of available order fields, add it to the list.
                OrderFieldOption existingOption = _orderByFields.Find(opt => opt.OrderInfo.FieldName == sortChoice.Key);
                if (existingOption == null)
                {
                    existingOption = new OrderFieldOption(false, new OrderBy(sortChoice.Key, SortOrder.Ascending));
                    _orderByFields.Add(existingOption);
                }
            }

            // Also make sure to remove any order by fields that were removed as 'group by' fields.
            for (int i = _orderByFields.Count - 1; i >= 0; i--)
            {
                // If this field is not in the grouped field list, remove it from the order fields list.
                OrderFieldOption opt = _orderByFields.ElementAt(i);
                KeyValuePair<string, bool> existingGroupField = sortFieldChoices.FirstOrDefault(field => field.Key == opt.OrderInfo.FieldName);
                if (existingGroupField.Key == null)
                {
                    _orderByFields.RemoveAt(i);
                }
            }

            // Set the data source on the table.
            sortFieldsTable.TableView.Source = new OrderByFieldsDataSource(_orderByFields);

            // Show the table view.
            NavigationController.PushViewController(sortFieldsTable, true);
        }

        private void ShowStatDefinitions(object sender, EventArgs e)
        {
            // Create a new UIPickerView and assign a model that will show fields and statistic types.
            UIPickerView statisticPicker = new UIPickerView
            {
                Model = _statsPickerModel
            };

            // Create a new table.
            UITableViewController statsTable = new UITableViewController(UITableViewStyle.Plain);

            // Create an instance of a custom data source to show statistic definitions in the table.
            // Pass in the list of statistic definitions and the picker (for defining new ones).
            StatisticDefinitionsDataSource statDefsDataSource = new StatisticDefinitionsDataSource(_statisticDefinitions, statisticPicker);

            // Set the data source on the table.
            statsTable.TableView.Source = statDefsDataSource;

            // Put the table in edit mode (to show add and delete buttons).
            statDefsDataSource.WillBeginTableEditing(statsTable.TableView);
            statsTable.SetEditing(true, true);

            // Show the table view.
            NavigationController.PushViewController(statsTable, true);
        }

        private async void ExecuteStatisticsQuery(object sender, EventArgs e)
        {
            // Remove the placeholder "Add statistic" row (if it exists).
            StatisticDefinition placeholderRow = _statisticDefinitions.LastOrDefault();
            if (placeholderRow != null && placeholderRow.OutputAlias == "")
            {
                _statisticDefinitions.Remove(placeholderRow);
            }

            // Verify that there is at least one statistic definition.
            if (!_statisticDefinitions.Any())
            {
                ShowAlert("Statistical Query", "Please define at least one statistic for the query.");
                return;
            }

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

            // Specify the selected group fields (if any).
            if (_groupByFields != null)
            {
                foreach (KeyValuePair<string, bool> groupField in _groupByFields.Where(field => field.Value))
                {
                    statQueryParams.GroupByFieldNames.Add(groupField.Key);
                }
            }

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

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

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

                // Get results formatted as a dictionary (group names and their associated dictionary of results).
                Dictionary<string, IReadOnlyDictionary<string, object>> resultsLookup = statQueryResult.ToDictionary(result => string.Join(", ", result.Group.Values), result => result.Statistics);

                // Create an instance of a custom data source to display the results.
                StatisticQueryResultsDataSource statResultsDataSource = new StatisticQueryResultsDataSource(resultsLookup);

                // Create a new table with a grouped style for displaying rows.
                UITableViewController statResultsTable = new UITableViewController(UITableViewStyle.Grouped)
                {
                    // Set the table view data source.
                    TableView = {Source = statResultsDataSource}
                };

                // Show the table view.
                NavigationController.PushViewController(statResultsTable, true);
            }
            catch (ArcGISWebException exception)
            {
                ShowAlert("There was a problem performing the query.", exception.ToString());
            }
        }

        private void ShowAlert(string title, string message)
        {
            // Create a new Alert Controller.
            UIAlertController alert = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert);

            // Add an Action to dismiss the alert.
            alert.AddAction(UIAlertAction.Create("Dismiss", UIAlertActionStyle.Cancel, null));

            // Display the alert.
            PresentViewController(alert, true, null);
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            Initialize();
        }

        public override void LoadView()
        {
            // Create the views.
            View = new UIView {BackgroundColor = ApplicationTheme.BackgroundColor};

            UIToolbar toolbar = new UIToolbar();
            toolbar.TranslatesAutoresizingMaskIntoConstraints = false;
            toolbar.Items = new[]
            {
                new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace),
                new UIBarButtonItem("Get statistics", UIBarButtonItemStyle.Plain, ExecuteStatisticsQuery),
                new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace)
            };

            _showStatDefinitionsButton = new UIButton();
            _showStatDefinitionsButton.TranslatesAutoresizingMaskIntoConstraints = false;
            _showStatDefinitionsButton.SetTitle("1. Choose statistic definitions", UIControlState.Normal);
            _showStatDefinitionsButton.SetTitleColor(View.TintColor, UIControlState.Normal);

            _showGroupFieldsButton = new UIButton();
            _showGroupFieldsButton.TranslatesAutoresizingMaskIntoConstraints = false;
            _showGroupFieldsButton.SetTitle("2. Choose group fields", UIControlState.Normal);
            _showGroupFieldsButton.SetTitleColor(View.TintColor, UIControlState.Normal);

            _showOrderByFieldsButton = new UIButton();
            _showOrderByFieldsButton.SetTitle("3. Choose 'Order by' fields", UIControlState.Normal);
            _showOrderByFieldsButton.SetTitleColor(View.TintColor, UIControlState.Normal);

            UIStackView buttonContainer = new UIStackView(new[] {_showStatDefinitionsButton, _showGroupFieldsButton, _showOrderByFieldsButton, new UIView()});
            buttonContainer.Axis = UILayoutConstraintAxis.Vertical;
            buttonContainer.TranslatesAutoresizingMaskIntoConstraints = false;
            buttonContainer.Distribution = UIStackViewDistribution.Fill;
            buttonContainer.Alignment = UIStackViewAlignment.Top;

            // Add the views.
            View.AddSubviews(buttonContainer, toolbar);

            // Lay out the views.
            NSLayoutConstraint.ActivateConstraints(new[]
            {
                buttonContainer.TopAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TopAnchor),
                buttonContainer.LeadingAnchor.ConstraintEqualTo(View.LayoutMarginsGuide.LeadingAnchor),
                buttonContainer.TrailingAnchor.ConstraintEqualTo(View.LayoutMarginsGuide.TrailingAnchor),
                buttonContainer.BottomAnchor.ConstraintEqualTo(toolbar.TopAnchor),

                toolbar.BottomAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.BottomAnchor),
                toolbar.LeadingAnchor.ConstraintEqualTo(View.LeadingAnchor),
                toolbar.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor)
            });
        }

        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);

            // Subscribe to events.
            _showStatDefinitionsButton.TouchUpInside += ShowStatDefinitions;
            _showGroupFieldsButton.TouchUpInside += ShowGroupFields;
            _showOrderByFieldsButton.TouchUpInside += ShowOrderByFields;
        }

        public override void ViewDidDisappear(bool animated)
        {
            base.ViewDidDisappear(animated);

            // Unsubscribe from events, per best practice.
            _showStatDefinitionsButton.TouchUpInside -= ShowStatDefinitions;
            _showGroupFieldsButton.TouchUpInside -= ShowGroupFields;
            _showOrderByFieldsButton.TouchUpInside -= ShowOrderByFields;
        }
    }

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

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

    // Class that defines a view model for showing field names and statistic types in a picker.
    public class StatDefinitionModel : UIPickerViewModel
    {
        // Array of field names.
        private readonly string[] _fieldNames;

        // Array of available statistic types.
        private readonly Array _statTypes = Enum.GetValues(typeof(StatisticType));

        // Constructor that takes an array of the available field names.
        public StatDefinitionModel(string[] fieldNames)
        {
            _fieldNames = fieldNames;
        }

        // Property to expose the currently selected definition in the picker.
        public StatisticDefinition SelectedStatDefinition { get; private set; }

        // Return the number of picker components (two sections: field names and statistic types).
        public override nint GetComponentCount(UIPickerView pickerView)
        {
            return 2;
        }

        // Return the number of rows in each of the two sections.
        public override nint GetRowsInComponent(UIPickerView pickerView, nint component)
        {
            // first component is the fields list, second is the statistic types.
            return component == 0 ? _fieldNames.Length : _statTypes.Length;
        }

        // Get the title to display in each picker component.
        public override string GetTitle(UIPickerView pickerView, nint row, nint component)
        {
            // first component is the fields list, second is the statistic types.
            return component == 0 ? _fieldNames[row] : _statTypes.GetValue(row).ToString();
        }

        // Handle the selection event for the picker to create a statistic definition with the values chosen.
        public override void Selected(UIPickerView pickerView, nint row, nint component)
        {
            // Get the field name.
            string onFieldName = _fieldNames[pickerView.SelectedRowInComponent(0)];

            // Get the statistic type.
            StatisticType statType = (StatisticType) _statTypes.GetValue(pickerView.SelectedRowInComponent(1));

            // Create an output field alias by concatenating the field name and statistic type.
            string outAlias = onFieldName + "_" + statType;

            // Create a new statistic definition (available from the SelectedStatDefinition public property).
            SelectedStatDefinition = new StatisticDefinition(onFieldName, statType, outAlias);
        }

        // Return the desired width for each component in the picker.
        public override nfloat GetComponentWidth(UIPickerView pickerView, nint component)
        {
            // first component is the fields list, second is the statistic types.
            return component == 0 ? 160f : 120f;
        }

        // Return the desired height for rows in the picker.
        public override nfloat GetRowHeight(UIPickerView pickerView, nint component)
        {
            return 40f;
        }
    }

    // Class that defines a custom data source for showing statistic definitions.
    public class StatisticDefinitionsDataSource : UITableViewSource
    {
        // List of statistic definitions for the current query.
        private readonly List<StatisticDefinition> _statisticDefinitions;

        // Picker for choosing a field and statistic type.
        private readonly UIPickerView _statPicker;

        // Custom UI to show the statistics picker and associated buttons.
        private ChooseStatisticOverlay _chooseStatOverlay;

        // Text to display for the placeholder row used to add new statistic definitions.
        private const string AddNewStatFieldName = "(Add statistic)";

        // Constructor that takes a list of statistic definitions and a picker for selecting fields and statistic types.
        public StatisticDefinitionsDataSource(List<StatisticDefinition> statDefs, UIPickerView picker)
        {
            // Store the list of statistic definitions and the statistic picker.
            _statisticDefinitions = statDefs;
            _statPicker = picker;
        }

        // Handle supported edits to the data source (inserts and deletes).
        public override void CommitEditingStyle(UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath)
        {
            // Respond to the user's edit request: Insert a new statistic definition, or delete an existing one.
            if (editingStyle == UITableViewCellEditingStyle.Insert)
            {
                // Create an overlay UI that lets the user choose a field and statistic type to add.
                _chooseStatOverlay = new ChooseStatisticOverlay(_statPicker);

                // Handle the OnStatisticDefined event to get the info entered by the user.
                _chooseStatOverlay.OnStatisticDefined += (s, statDef) =>
                {
                    // Verify the selected statistic doesn't exist in the collection (check for an alias with the same value).
                    StatisticDefinition existingItem = _statisticDefinitions.Find(itm => itm.OutputAlias == statDef.OutputAlias);
                    if (existingItem != null)
                    {
                        return;
                    }

                    // Make updates to the table (add the chosen statistic).
                    tableView.BeginUpdates();

                    // Insert a new row at the top of table display.
                    tableView.InsertRows(new[] {NSIndexPath.FromRowSection(0, 0)}, UITableViewRowAnimation.Fade);

                    // Insert the chosen statistic in the underlying collection.
                    _statisticDefinitions.Insert(0, statDef);

                    // Apply table edits.
                    tableView.EndUpdates();
                };

                // Handle when the user chooses to close the dialog.
                _chooseStatOverlay.OnCanceled += (s, e) =>
                {
                    // Remove the item input UI.
                    _chooseStatOverlay.Hide();
                    _chooseStatOverlay = null;
                };

                // Add the picker UI view (will display semi-transparent over the table view).
                tableView.AddSubview(_chooseStatOverlay);

                _chooseStatOverlay.TranslatesAutoresizingMaskIntoConstraints = false;
                _chooseStatOverlay.LeadingAnchor.ConstraintEqualTo(tableView.SafeAreaLayoutGuide.LeadingAnchor).Active =
                    true;
                _chooseStatOverlay.TrailingAnchor.ConstraintEqualTo(tableView.SafeAreaLayoutGuide.TrailingAnchor)
                    .Active = true;
                _chooseStatOverlay.BottomAnchor.ConstraintEqualTo(tableView.SafeAreaLayoutGuide.BottomAnchor).Active = true;
            }
            else if (editingStyle == UITableViewCellEditingStyle.Delete)
            {
                // Remove the selected row from the table and the underlying collection of statistic definitions.
                _statisticDefinitions.RemoveAt(indexPath.Row);
                tableView.DeleteRows(new[] {indexPath}, UITableViewRowAnimation.Fade);
            }
        }

        // Define the (confirmation) text to display when the user chooses to delete a row.
        public override string TitleForDeleteConfirmation(UITableView tableView, NSIndexPath indexPath)
        {
            return "Remove";
        }

        // Allow all rows to be edited.
        public override bool CanEditRow(UITableView tableView, NSIndexPath indexPath)
        {
            return true;
        }

        // Allow all rows to be deleted except the last row, which is a placeholder for creating new statistic definitions.
        public override UITableViewCellEditingStyle EditingStyleForRow(UITableView tableView, NSIndexPath indexPath)
        {
            // Get the index of the last row in the table view.
            nint lastRowIndex = tableView.NumberOfRowsInSection(0) - 1;

            // Set the editing style as delete for all but the final row (insert).
            return indexPath.Row == lastRowIndex ? UITableViewCellEditingStyle.Insert : UITableViewCellEditingStyle.Delete;
        }

        // Prepare the data source for editing.
        public void WillBeginTableEditing(UITableView tableView)
        {
            // See if the table already has a placeholder row for the "Add New" button.
            StatisticDefinition existingItem = _statisticDefinitions.Find(itm => itm.OnFieldName == AddNewStatFieldName);

            // Return if there is already a placeholder row.
            if (existingItem != null)
            {
                return;
            }

            // Begin updating the table.
            tableView.BeginUpdates();

            // Create an index path for the last row in the table.
            NSIndexPath lastRowIndex = NSIndexPath.FromRowSection(tableView.NumberOfRowsInSection(0), 0);

            // Add the insert placeholder row at the end of table display.
            tableView.InsertRows(new[] {lastRowIndex}, UITableViewRowAnimation.Fade);

            // Create a new StatisticDefinition and add it to the underlying data.
            _statisticDefinitions.Add(new StatisticDefinition(AddNewStatFieldName, StatisticType.Count, ""));

            // Apply the table edits.
            tableView.EndUpdates();
        }

        // This is called each time a cell needs to be created in the table.
        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            // Create a new cell with a main and detail label style.
            UITableViewCell cell = new UITableViewCell(UITableViewCellStyle.Subtitle, null);

            // Get the corresponding StatisticDefinition for this row.
            StatisticDefinition definition = _statisticDefinitions[indexPath.Row];

            // Set the cell text with the field name.
            cell.TextLabel.Text = definition.OnFieldName;

            // If this is not the placeholder (insert) row, set the detail text with the statistic type.
            if (definition.OnFieldName != AddNewStatFieldName)
            {
                cell.DetailTextLabel.Text = definition.StatisticType.ToString();
            }

            // Return the new cell.
            return cell;
        }

        // Return the number of rows for the table (count of the statistics definition list).
        public override nint RowsInSection(UITableView tableview, nint section)
        {
            return _statisticDefinitions.Count;
        }
    }

    // Class that defines a custom data source for display group fields.
    public class GroupFieldsDataSource : UITableViewSource
    {
        // Dictionary of available fields for grouping results.
        private readonly Dictionary<string, bool> _potentialGroupFields;

        // Constructor that takes a dictionary of fields.
        public GroupFieldsDataSource(Dictionary<string, bool> fields)
        {
            _potentialGroupFields = fields;
        }

        // Create a view to display the value of each item in the dictionary.
        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            // Create a UITableViewCell with default style.
            UITableViewCell cell = new UITableViewCell(UITableViewCellStyle.Default, null);

            // Get the field name and whether it's set as a group field.
            string fieldName = _potentialGroupFields.ElementAt(indexPath.Row).Key;
            bool isForGrouping = _potentialGroupFields.ElementAt(indexPath.Row).Value;

            // Display the field name in the cell.
            cell.TextLabel.Text = fieldName;

            // Create a UISwitch for selecting the field for grouping.
            UISwitch groupFieldSwitch = new UISwitch
            {
                Frame = new CGRect(cell.Bounds.Width - 60, 7, 50, cell.Bounds.Height),
                // Set the switch control tag with the row position.
                Tag = indexPath.Row,
                // Set the initial switch value to show whether it's been selected for grouping.
                On = isForGrouping
            };

            // Handle the value changed for the switch so the dictionary value can be updated.
            groupFieldSwitch.ValueChanged += GroupBySwitched;

            // Add the UISwitch to the cell's content view.
            cell.ContentView.AddSubview(groupFieldSwitch);

            return cell;
        }

        private void GroupBySwitched(object sender, EventArgs e)
        {
            // Use the control's tag to get the row that was changed.
            nint index = ((UISwitch) sender).Tag;

            // Set or clear the group field according to the UISwitch setting.
            string key = _potentialGroupFields.ElementAt((int) index).Key;
            _potentialGroupFields[key] = ((UISwitch) sender).On;
        }

        // Return the number of rows to display.
        public override nint RowsInSection(UITableView tableview, nint section)
        {
            return _potentialGroupFields.Count;
        }
    }

    // Class that defines a custom data source for displaying fields to order results with.
    public class OrderByFieldsDataSource : UITableViewSource
    {
        // List of order field options.
        private readonly List<OrderFieldOption> _potentialOrderByFields;

        // Constructor that takes a list of order field options to display.
        public OrderByFieldsDataSource(List<OrderFieldOption> fields)
        {
            _potentialOrderByFields = fields;
        }

        // Create a cell to display information for each order field option.
        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            // Default table cell.
            UITableViewCell cell = new UITableViewCell(UITableViewCellStyle.Default, null);

            // Get the field name and whether it's been selected for sorting.
            string fieldName = _potentialOrderByFields.ElementAt(indexPath.Row).OrderInfo.FieldName;
            bool isForSorting = _potentialOrderByFields.ElementAt(indexPath.Row).OrderWith;

            // Show the field name in the cell.
            cell.TextLabel.Text = fieldName;

            // Create a UISwitch for selecting the field for ordering results.
            UISwitch sortFieldSwitch = new UISwitch
            {
                Frame = new CGRect(cell.Bounds.Width - 60, 7, 50, cell.Bounds.Height),
                // Set the control's tag with the row index.
                Tag = indexPath.Row,
                // Set the initial switch value to show if this field will be used for sorting.
                On = isForSorting
            };

            // Handle the value changed event to update the dictionary value for this field.
            sortFieldSwitch.ValueChanged += OrderBySwitched;

            // Add the UISwitch to the cell's content view.
            cell.ContentView.AddSubview(sortFieldSwitch);

            return cell;
        }

        private void OrderBySwitched(object sender, EventArgs e)
        {
            // Use the control's tag to get the row that was changed.
            nint index = ((UISwitch) sender).Tag;

            // Get the corresponding field and update its choice as a sort field.
            OrderFieldOption orderByOption = _potentialOrderByFields.ElementAt((int) index);
            orderByOption.OrderWith = ((UISwitch) sender).On;
        }

        // Return the number of rows to display.
        public override nint RowsInSection(UITableView tableview, nint section)
        {
            return _potentialOrderByFields.Count;
        }
    }

    // Class that defines a custom data source for showing statistic query results.
    public class StatisticQueryResultsDataSource : UITableViewSource
    {
        // Dictionary of group names and statistic results.
        private readonly Dictionary<string, IReadOnlyDictionary<string, object>> _statisticsResults;

        // Constructor that takes a dictionary of group names and statistic results.
        public StatisticQueryResultsDataSource(Dictionary<string, IReadOnlyDictionary<string, object>> results)
        {
            _statisticsResults = results;
        }

        // Create a cell for each item in the results.
        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            // Create a new cell with a main and detail label style.
            UITableViewCell cell = new UITableViewCell(UITableViewCellStyle.Subtitle, null);

            // Get the group name.
            KeyValuePair<string, IReadOnlyDictionary<string, object>> group = _statisticsResults.ElementAt(indexPath.Section);

            // Get the results for this group (dictionary).
            IReadOnlyDictionary<string, object> stats = group.Value;

            // Get the result (field alias and value).
            string field = stats.Keys.ElementAt(indexPath.Row);
            object value = stats.Values.ElementAt(indexPath.Row);

            // Set the main text with the statistic value.
            cell.TextLabel.Text = (value ?? "").ToString();

            // Set the sub text with the field alias name.
            cell.DetailTextLabel.Text = field;

            // Return the new cell.
            return cell;
        }

        // Return the number of sections (groups).
        public override nint NumberOfSections(UITableView tableView)
        {
            return _statisticsResults.Keys.Count;
        }

        // Return the number of rows in the specified section (group).
        public override nint RowsInSection(UITableView tableview, nint section)
        {
            return _statisticsResults[_statisticsResults.Keys.ElementAt((int) section)].Count;
        }

        // Return the header text for the specified section (group).
        public override string TitleForHeader(UITableView tableView, nint section)
        {
            return _statisticsResults.Keys.ElementAt((int) section);
        }
    }

    // View containing "define statistic" controls (picker for fields/stat type, add/cancel buttons).
    public class ChooseStatisticOverlay : UIView
    {
        // Event to provide the statistic definition the user entered when the view closes.
        public event EventHandler<StatisticDefinition> OnStatisticDefined;

        // Event to report that the choice was canceled.
        public event EventHandler OnCanceled;

        // Constructor that takes a picker for defining new statistics.
        public ChooseStatisticOverlay(UIPickerView statPicker)
        {
            this.TranslatesAutoresizingMaskIntoConstraints = false;
            // Toolbar with "Add" and "Done" buttons.
            UIToolbar toolbar = new UIToolbar();
            toolbar.TranslatesAutoresizingMaskIntoConstraints = false;

            statPicker.TranslatesAutoresizingMaskIntoConstraints = false;

            // Add Button (add the new stat and don't dismiss the UI).
            UIBarButtonItem addButton = new UIBarButtonItem("Add", UIBarButtonItemStyle.Plain, (s, e) =>
            {
                // Get the selected StatisticDefinition.
                StatDefinitionModel statPickerModel = (StatDefinitionModel) statPicker.Model;
                StatisticDefinition newStatDefinition = statPickerModel.SelectedStatDefinition;
                if (newStatDefinition != null)
                {
                    // Fire the OnMapInfoEntered event and provide the statistic definition.
                    OnStatisticDefined?.Invoke(this, newStatDefinition);
                }
            });

            // Done Button (dismiss the UI, don't use the selected statistic).
            UIBarButtonItem doneButton = new UIBarButtonItem("Done", UIBarButtonItemStyle.Done,
                (s, e) => { OnCanceled?.Invoke(this, null); });

            // Add the buttons to the toolbar.
            toolbar.Items = new[] {addButton, doneButton};

            statPicker.BackgroundColor = ApplicationTheme.BackgroundColor;

            // Add the controls.
            AddSubviews(toolbar, statPicker);

            toolbar.BottomAnchor.ConstraintEqualTo(BottomAnchor).Active = true;
            toolbar.LeadingAnchor.ConstraintEqualTo(LeadingAnchor).Active = true;
            toolbar.TrailingAnchor.ConstraintEqualTo(TrailingAnchor).Active = true;

            statPicker.TopAnchor.ConstraintEqualTo(TopAnchor).Active = true;
            statPicker.LeadingAnchor.ConstraintEqualTo(LeadingAnchor).Active = true;
            statPicker.TrailingAnchor.ConstraintEqualTo(TrailingAnchor).Active = true;
            statPicker.BottomAnchor.ConstraintEqualTo(toolbar.TopAnchor).Active = true;
        }

        // Animate increasing transparency to completely hide the view, then remove it.
        public void Hide()
        {
            // Action to make the view transparent.
            Action makeTransparentAction = () => Alpha = 0;

            // Action to remove the view.
            Action removeViewAction = RemoveFromSuperview;

            // Time to complete the animation (seconds).
            const double secondsToComplete = 0.75;

            // Animate transparency to zero, then remove the view.
            Animate(secondsToComplete, makeTransparentAction, removeViewAction);
        }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.