Query a feature table for statistics, grouping and sorting by different fields.
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 drop down menus and "Add" button. Select a table row and click "Remove statistic" 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
- Create a
ServiceFeatureTable
using the URL of a feature service and load the table. - Get the feature tables field names list with
featureTable.getFields()
. - Create
StatisticDefinition
s specifying the field to compute statistics on and theStatisticType
to compute. - Create
StatisticsQueryParameters
passing in the list of statistic definitions. - To have the results grouped by fields, add the field names to the query parameters'
groupByFieldNames
collection. - To have the results ordered by fields, create
OrderBy
s, specifying the field name andSortOrder
. Pass theseOrderBy
s to the parameters'orderByFields
collection. - To execute the query, call
featureTable.queryStatisticsAsync(queryParameters)
. - Get the
StatisticsQueryResult
. From this, you can get an iterator ofStatisticRecord
s to loop through and display.
About the data
This sample uses a Diabetes, Obesity, and Inactivity by US County feature layer hosted on ArcGIS Online.
Relevant API
- Field
- OrderBy
- QueryParameters
- ServiceFeatureTable
- StatisticDefinition
- StatisticRecord
- StatisticsQueryParameters
- StatisticsQueryResult
- StatisticType
Tags
correlation, data, fields, filter, group, sort, statistics, table
Sample Code
/* Copyright 2018 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.
*
*/
package com.esri.arcgisruntime.sample.statisticalquerygroupandsort;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
/**
* Expandable list view which displays grouped results from a LinkedHashMap.
*/
class ExpandableListViewAdapter extends BaseExpandableListAdapter {
private final Context context;
private final List<String> mGroupList;
private final LinkedHashMap<String, List<String>> mStatList;
public ExpandableListViewAdapter(Context context, LinkedHashMap<String, List<String>> statList) {
this.context = context;
mGroupList = new ArrayList<>(statList.keySet());
mStatList = statList;
}
@Override
public Object getChild(int groupListPosition, int statListPosition) {
return mStatList.get(mGroupList.get(groupListPosition)).get(statListPosition);
}
@Override
public long getChildId(int groupPosition, int statListPosition) {
return statListPosition;
}
@Override
public View getChildView(int groupListPosition, final int statListPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
final String expandedListText = (String) getChild(groupListPosition, statListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(android.R.layout.simple_expandable_list_item_1, null);
}
TextView expandedListTextView = convertView.findViewById(android.R.id.text1);
expandedListTextView.setText(expandedListText);
return convertView;
}
@Override
public int getChildrenCount(int listPosition) {
return mStatList.get(mGroupList.get(listPosition)).size();
}
@Override
public Object getGroup(int listPosition) {
return mGroupList.get(listPosition);
}
@Override
public int getGroupCount() {
return mGroupList.size();
}
@Override
public long getGroupId(int listPosition) {
return listPosition;
}
@Override
public View getGroupView(int listPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(android.R.layout.simple_expandable_list_item_1, null);
}
TextView listTitleTextView = convertView.findViewById(android.R.id.text1);
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
}