This sample demonstrates how to query for statistics in a FeatureLayerView by distance and display the results of the query in a series of charts.

This app displays homicide data from 50 U.S. cities between 2007 and 2017. The data was gathered and analyzed by The Washington Post and is available for download here.

How it works

As the user clicks or drags the pointer across the map, the app queries and highlights all features within one mile of the pointer location using the FeatureLayerView. The charts immediately update with new data as the pointer location changes.

for (const eventName of ["arcgisViewClick", "arcgisViewDrag"]) {
viewElement.addEventListener(eventName, async (event) => {
// Disable navigation by pointer drag and query the layer view.
event.detail.stopPropagation();
try {
const responses = await queryStatsOnDrag(layerView, event.detail);
updateCharts(responses);
} catch (error) {
if (!promiseUtils.isAbortError(error)) {
console.error(error);
}
}
});
}

To query a layer view by distance, you first need to create a query object and set up the desired parameters. The createQuery() method on the FeatureLayer is a convenient way to construct this object with all the current configurations of the layer already defined on the object.

const query = layerView.layer.createQuery();
// converts the screen point to a map point
// the event object comes from the component event's detail
query.geometry = viewElement.toMap(event);
// queries all features within 1 mile of the point
query.distance = 1;
query.units = "miles";

Next, create an array of StatisticDefinition objects, setting a field name and statistic type for each field for which you wish to query statistics. For statistic queries, you can query statistics for a field name or for data returned from a SQL expression. This sample calculates nearly 30 statistics for each pointer location. We’ll only show a few of them in the following snippet.

// clone the query object from the previous snippet
const statsQuery = query.clone();
// date used to calculate the average time a case has been opened
const dataDownloadDate = Date.UTC(2018, 6, 5);
// Create the statistic definitions for querying stats from the layer view
// the `onStatisticField` property can reference a field name or a SQL expression
// `outStatisticFieldName` is the name of the statistic you will reference in the result
// `statisticType` can be sum, avg, min, max, count, stddev
const statDefinitions = [
// Age of crime since it was reported in years
{
onStatisticField: "(" + dataDownloadDate + " - milliseconds) / (1000 * 60 * 60 * 24 * 365.25)",
outStatisticFieldName: "avg_open_time_years",
statisticType: "avg",
},
// total homicides
{
onStatisticField: "1",
outStatisticFieldName: "total",
statisticType: "count",
},
// crime disposition (aka crime status)
//
// Since this is a string field, we can use a similar technique to sum
// the total of each status of the crime
{
onStatisticField: "CASE WHEN disposition = 'Closed by arrest' THEN 1 ELSE 0 END",
outStatisticFieldName: "num_closed_arrest",
statisticType: "sum",
},
{
onStatisticField: "CASE WHEN disposition = 'Open/No arrest' THEN 1 ELSE 0 END",
outStatisticFieldName: "num_open",
statisticType: "sum",
},
{
onStatisticField: "CASE WHEN disposition = 'Closed without arrest' THEN 1 ELSE 0 END",
outStatisticFieldName: "num_closed_no_arrest",
statisticType: "sum",
},
// average victim age
//
// Some victim ages are unknown and indicated with a -99. We'll
// use an expression to treat those unknown ages as 0. This will
// skew the average age slightly downward since we can't exclude those
// values without a where clause. Do use a where clause, we could execute
// a separate query
{
onStatisticField: "CASE WHEN victim_age_years = -99 THEN 0 ELSE victim_age_years END",
outStatisticFieldName: "avg_age",
statisticType: "avg",
},
];

Once the statistic definition objects are created, pass them as an array to the outStatistics property of the Query object, then execute the query.

// add the stat definitions to the statistics query object cloned earlier
statsQuery.outStatistics = statDefinitions;
// execute the query for all features in the layer view
const allStatsResponse = await layerView.queryFeatures(statsQuery);
const allStats = allStatsResponse.features[0].attributes;

To query just a subset of the data, you can optionally set a where clause on the Query object.

// clone the query object from the previous steps
const openStatsQuery = statsQuery.clone();
// limit the results to only the crimes that did not result in an arrest
openStatsQuery.where = "disposition = 'Open/No arrest'";
// execute the query for only unsolved homicides in the layer view
const unsolvedStatsResponse = await layerView.queryFeatures(openStatsQuery);
const unsolvedStats = unsolvedStatsResponse.features[0].attributes;

The result is returned as a single feature in a FeatureSet. The attribute name of each statistic is the name of the outStatisticFieldName set in the StatisticDefinition object. See the image below, which shows what the result of one of these queries looks like when printed to the browser’s console.

flv-query-stats-result

2DFeatureLayerViewstatisticsQuerydistancechartreactiveUtilspromiseUtilsdebouncepromiseviewOnReadyarcgis-bookmarksarcgis-expandarcgis-legendarcgis-maparcgis-zoomarcgisViewClickmap-componentscalcite-panelcalcite-shellcalcite-shell-panelcalcite-components