ArcGIS Online

How to summarize aggregate data using Arcade in popups

The November 2022 update of the ArcGIS Online Map Viewer added support for binning as an alternative to clustering and heat maps. Binning aggregates data to predefined cells, effectively representing point data as a gridded polygon layer. This post will demonstrate how you can use Arcade to create dynamic popup elements summarizing attributes from aggregated points in a bin’s popup.

Density of crimes committed in San Diego, aggregated to geohash bins.
Density of crimes committed in San Diego, aggregated to geohash bins.

Aggregate fields

The aggregation panel in the map viewer allows you to define aggregate fields summarizing the features contained within a bin based on a set of statistics. For example, assume you enable binning on a point layer representing cities that has a POPULATION field on the layer. You can create a field named POPULATION_SUM that calculates the sum of the POPULATION field for all features within the bin.

The ArcGIS Online Map Viewer allows you to define aggregate fields for summarizing data in bins. This example shows how you can aggregate the population field of each feature using the "sum" statistic.
The ArcGIS Online Map Viewer allows you to define aggregate fields for summarizing data in bins. This example shows how you can aggregate the population field of each feature using the "sum" statistic.

Previously (in clustering visualizations), you would have had to use Arcade math functions (such as sum) to display aggregate statistics in the popups.

// returns the sum of the POPULATION
// field for all features in the bin
SUM($aggregatedFeatures, "POPULATION")

You should always favor defining bin statistics using aggregate fields instead of using Arcade, since aggregate fields are precomputed and can be used anywhere in the binning configuration, including labels and styles. However, this begs an important question…

Do aggregate fields remove the need for Arcade in binning visualizations?

No! Arcade does much more than calculate values. In fact, you can use it to create dynamic, flexible popup elements, including the following:

We’ll explore these scenarios in the San Diego crimes web map.

Arcade terminology

Before diving into each example, you should be familiar with the following Arcade language concepts. Each of the following are used in the example expressions:

Ordered list of categories

The San Diego crimes dataset classifies crimes under generic types, such as “drugs and alcohol”, “burglary”, and “theft”. Unfortunately, the Map Viewer does not allow you to create new fields that report the count of each of these categories within the cluster.

However, you can use Arcade to dynamically create a table for a binning popup as illustrated below.

The binning popup displays a table summarizing the top 5 crimes committed within each bin. This table can only be dynamically created using Arcade.
The binning popup displays a table summarizing the top 5 crimes committed within each bin. This table can only be dynamically created using Arcade.

Let’s explore the expression used to create this table. The following expression queries the top 5 crime types for each bin in San Diego, and calculates two statistics for each type: the total count and the percentage of those crimes that were reported at night.

Read the comments to follow the logic of the expression if you are unfamiliar with Arcade.

Arcade snippet showing how to create a table (part 1).

Note that this expression returns different popup elements based on a condition. In this case if there are no categories discovered, then the popup will return a message indicating there are no reported crimes in the area. The expression continues below if a bin does contain at least one category…

Arcade snippet showing how to create a table (part 2).

The for loop in the expression iterates through each category and creates a new row in the HTML table describing the crime type, number of crimes, and percent that were reported at night.

The result displays the following table. Note that rows are ordered from the most common to least common. If we had defined aggregate fields in this scenario, then these rows could not be ordered without the help of Arcade.

View the web map

The binning popup displays a table summarizing the top 5 crimes committed within each bin. This table can only be dynamically created using Arcade.
The binning popup displays a table summarizing the top 5 crimes committed within each bin. This table can only be dynamically created using Arcade.

Create charts based on statistics grouped by distinct values from a single field

This example will demonstrate how to create a single media popup element containing two charts. The following charts will be created:

Arcade can be used to create a pie chart that visualizes the proportion of crimes by type. In this case the count of crimes is grouped by the TYPE field.
Arcade can be used to create a pie chart that visualizes the proportion of crimes by type in the popup. In this case the count of crimes is grouped by the TYPE field.
Arcade can be used to create a line chart that visualizes the number of crimes by month in the popup. In this case the count of crimes is grouped by the MONTH field.
Arcade can be used to create a line chart that visualizes the number of crimes by month in the popup. In this case the count of crimes is grouped by the MONTH field.

While the result of the expression may look very different from the first example, the fundamental logic is very similar in both examples. Read the comments in the expression below to understand how the expression creates two charts in the same popup element: a pie chart that visualizes the number of crimes by type, and a line chart that visualizes the number of crimes committed in each month of the year.

Arcade snippet showing how to create a chart (part 1).

The expression must return a dictionary that follows the web map specification for any of the following content elements.

Since we want charts to render in the popup, we must set up a dictionary defining the attributes (or data) used in the chart. The expression continues below…

Arcade snippet showing how to create a chart (part 2).

Conclusion

Aggregate fields remove the need to write Arcade expressions for use in binning popups, styles, and labels for the majority of cases. However, you still need Arcade if you would like to dynamically render summary statistics for categories from a single field, such as a sorted table or chart.

If you’re a web developer, be sure to check out the Summarize binned data using Arcade in the ArcGIS API for JavaScript documentation. This sample demonstrates how to create the same web map outside of Map Viewer using JavaScript.

About the author

Kristian is a Principal Product Engineer at Esri specializing in data visualization. He works on the ArcGIS Maps SDK for JavaScript, ArcGIS Arcade, and Map Viewer in ArcGIS Online. His goal is to help developers be successful, efficient, and confident in building web applications with the JavaScript Maps SDK, especially when it comes to visualizing data. Prior to joining Esri, he worked as a GIS Specialist for an environmental consulting company. He enjoys cartography, GIS analysis, and building GIS applications for genealogy.

Connect:
1 Comment
Oldest
Newest
Inline Feedbacks
View all comments

Next Article

Drawing a Blank? Understanding Drawing Alerts in ArcGIS Pro

Read this article