▾
U.S. counties styled with a UniqueValueRenderer showing the predominant political party affiliation of registered voters.
What is Arcade?
Sometimes you need to render an attribute that doesn’t exist in your layer. Arcade
See the Arcade - expression language guide for more details about how to use Arcade in other parts of the ArcGIS Maps SDK for JavaScript
How Arcade works
Arcade expressions are referenced as strings in the valueExpression property of ClassBreaksRenderer, UniqueValueRenderer, DotDensityRenderer or any visual variable: color, size, opacity, and rotation. When defined, it is always used instead of referencing a field/normalizationField.
When used in a ClassBreaksRenderer, DotDensityRenderer, or any of the visual variables, the value expression must evaluate to a number. Expressions may evaluate to either strings or numbers in UniqueValueRenderer.
Examples
Predominance with UniqueValueRenderer
In this example, Arcade is used to create a predominance map showing the most common political party affiliation in each U.S. county. The underlying layer has three fields that identify the number of republicans, democrats, and independent/non-party voters in each county. Since the service does not contain a field indicating the predominant party, we can write an Arcade expression to identify that for each feature.
The Arcade expression is referenced in the valueExpression property of a UniqueValueRenderer.
36 collapsed lines
<!doctype html><html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<title>Arcade - political affiliation predominance</title>
<!-- Load the ArcGIS Maps SDK for JavaScript from CDN --> <script type="module" src="https://js.arcgis.com/5.2/"></script>
<style> html, body { color-scheme: dark; height: 100%; margin: 0; } </style> </head>
<body> <arcgis-map center="-95, 38" zoom="4"> <arcgis-zoom slot="top-left"></arcgis-zoom> <arcgis-expand slot="top-right"> <arcgis-legend id="political-affiliation-legend"></arcgis-legend> </arcgis-expand> </arcgis-map>
<script type="module"> const [Map, FeatureLayer] = await $arcgis.import(["@arcgis/core/Map.js", "@arcgis/core/layers/FeatureLayer.js"]);
const viewElement = document.querySelector("arcgis-map"); const legendElement = document.getElementById("political-affiliation-legend");
const renderer = { type: "unique-value", valueExpression: ` // store field values in variables with // meaningful names. Each is the total count // of votes for the respective party
var republican = $feature.MP06025a_B; var democrat = $feature.MP06024a_B; var independent = $feature.MP06026a_B; var parties = [republican, democrat, independent];
// Match the maximum value with the label // of the respective field and return it for // use in a UniqueValueRenderer
return Decode( Max(parties), republican, 'republican', democrat, 'democrat', independent, 'independent', 'n/a' ); `,108 collapsed lines
valueExpressionTitle: "Counties by dominant party among registered voters",
uniqueValueInfos: [ { value: "democrat", symbol: createSymbol("#00c3ff"), label: "Democrat", }, { value: "republican", symbol: createSymbol("#ff002e"), label: "Republican", }, { value: "independent", symbol: createSymbol("#faff00"), label: "Independent/other party", }, ],
};
const layer = new FeatureLayer({ portalItem: { id: "8444e275037549c1acab02d2626daaee", }, renderer, popupTemplate: { title: "{COUNTY}, {STATE}", content: [ { type: "fields", fieldInfos: [ { fieldName: "MP06024a_B", label: "Democrat", fieldFormat: { type: "number", minimumFractionDigits: 0, maximumFractionDigits: 0, useGrouping: "always", }, }, { fieldName: "MP06025a_B", label: "Republican", fieldFormat: { type: "number", minimumFractionDigits: 0, maximumFractionDigits: 0, useGrouping: "always", }, }, { fieldName: "MP06026a_B", label: "Independent/Other", fieldFormat: { type: "number", minimumFractionDigits: 0, maximumFractionDigits: 0, useGrouping: "always", }, }, { fieldName: "POP18UP_CY", label: "Adult Population", fieldFormat: { type: "number", minimumFractionDigits: 0, maximumFractionDigits: 0, useGrouping: "always", }, }, ], }, ], }, });
const map = new Map({ basemap: "dark-gray-vector", layers: [layer], });
legendElement.layerInfos = [ { layer: layer, title: "Dominant political party by number of registered voters for each U.S. County", }, ]; viewElement.map = map;
// Creates a SimpleFillSymbol based on an input color
function createSymbol(color) { return { type: "simple-fill", // autocasts as new SimpleFillSymbol() color: color, outline: { width: 0.2, color: [0, 0, 0, 0.1], }, }; } </script> </body></html>Then assign unique symbols for each expected return value in the renderer.
61 collapsed lines
<!doctype html><html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<title>Arcade - political affiliation predominance</title>
<!-- Load the ArcGIS Maps SDK for JavaScript from CDN --> <script type="module" src="https://js.arcgis.com/5.2/"></script>
<style> html, body { color-scheme: dark; height: 100%; margin: 0; } </style> </head>
<body> <arcgis-map center="-95, 38" zoom="4"> <arcgis-zoom slot="top-left"></arcgis-zoom> <arcgis-expand slot="top-right"> <arcgis-legend id="political-affiliation-legend"></arcgis-legend> </arcgis-expand> </arcgis-map>
<script type="module"> const [Map, FeatureLayer] = await $arcgis.import(["@arcgis/core/Map.js", "@arcgis/core/layers/FeatureLayer.js"]);
const viewElement = document.querySelector("arcgis-map"); const legendElement = document.getElementById("political-affiliation-legend");
const renderer = { type: "unique-value", valueExpression: ` // store field values in variables with // meaningful names. Each is the total count // of votes for the respective party
var republican = $feature.MP06025a_B; var democrat = $feature.MP06024a_B; var independent = $feature.MP06026a_B; var parties = [republican, democrat, independent];
// Match the maximum value with the label // of the respective field and return it for // use in a UniqueValueRenderer
return Decode( Max(parties), republican, 'republican', democrat, 'democrat', independent, 'independent', 'n/a' ); `,
valueExpressionTitle: "Counties by dominant party among registered voters",
uniqueValueInfos: [ { value: "democrat", symbol: createSymbol("#00c3ff"), label: "Democrat", }, { value: "republican", symbol: createSymbol("#ff002e"), label: "Republican", }, { value: "independent", symbol: createSymbol("#faff00"), label: "Independent/other party", }, ],88 collapsed lines
};
const layer = new FeatureLayer({ portalItem: { id: "8444e275037549c1acab02d2626daaee", }, renderer, popupTemplate: { title: "{COUNTY}, {STATE}", content: [ { type: "fields", fieldInfos: [ { fieldName: "MP06024a_B", label: "Democrat", fieldFormat: { type: "number", minimumFractionDigits: 0, maximumFractionDigits: 0, useGrouping: "always", }, }, { fieldName: "MP06025a_B", label: "Republican", fieldFormat: { type: "number", minimumFractionDigits: 0, maximumFractionDigits: 0, useGrouping: "always", }, }, { fieldName: "MP06026a_B", label: "Independent/Other", fieldFormat: { type: "number", minimumFractionDigits: 0, maximumFractionDigits: 0, useGrouping: "always", }, }, { fieldName: "POP18UP_CY", label: "Adult Population", fieldFormat: { type: "number", minimumFractionDigits: 0, maximumFractionDigits: 0, useGrouping: "always", }, }, ], }, ], }, });
const map = new Map({ basemap: "dark-gray-vector", layers: [layer], });
legendElement.layerInfos = [ { layer: layer, title: "Dominant political party by number of registered voters for each U.S. County", }, ]; viewElement.map = map;
// Creates a SimpleFillSymbol based on an input color
function createSymbol(color) { return { type: "simple-fill", // autocasts as new SimpleFillSymbol() color: color, outline: { width: 0.2, color: [0, 0, 0, 0.1], }, }; } </script> </body></html>Arcade-driven opacity
Arcade can be used anywhere a data value is referenced in a renderer, including visual variables. The following snippet references an Arcade expression in an opacity visual variable to calculate the percentage of voters comprising the predominant party in each county. The opacity stops map the expected data values to opacity values.
Opaque counties indicate areas where the predominant political party comprises the vast majority of voters. Transparent counties show areas where there is a more diverse mix of individuals belonging to each political party.
122 collapsed lines
<!doctype html><html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /> <title>Arcade-driven visualizations | Sample | ArcGIS Maps SDK for JavaScript</title>
<!-- Load the ArcGIS Maps SDK for JavaScript from CDN --> <script type="module" src="https://js.arcgis.com/5.2/"></script>
<style> html, body { height: 100%; margin: 0; } </style> </head>
<body class="calcite-mode-dark"> <arcgis-map center="-95, 38" zoom="4"> <arcgis-zoom slot="top-left"></arcgis-zoom> <arcgis-legend slot="bottom-left"></arcgis-legend> <arcgis-popup slot="popup"></arcgis-popup> </arcgis-map>
<!-- ARCADE SCRIPTS -->
<!-- PREDOMINANT POLITICAL PARTY -->
<!-- Arcade expression that determines the predominant political party in each feature. First, feature values are assigned to meaningful variable names and placed in an array. Then the maximum count is determined with the Max() function. That count is then compared with each variable using Decode() Once a match is found, a string for the corresponding party is returned implicitly.
Decode() and Max() are functions built-in to Arcade Max() returns the max of the input values. Decode() returns the string corresponding to the value matching the input --> <script type="text/plain" id="winning-party"> // store field values in variables with // meaningful names. Each is the total count // of votes for the respective party
var republican = $feature.MP06025a_B; var democrat = $feature.MP06024a_B; var independent = $feature.MP06026a_B; var parties = [republican, democrat, independent];
// Match the maximum value with the label // of the respective field and return it for // use in a UniqueValueRenderer
return Decode( Max(parties), republican, 'republican', democrat, 'democrat', independent, 'independent', 'n/a' ); </script> <!-- STRENGTH OF WIN -->
<!-- Arcade expression determining the strength of the predominant party in each feature based on the proportion of that party to all others. A simple percentage is calculated and returned for the max value. --> <script type="text/plain" id="strength"> var republican = $feature.MP06025a_B; var democrat = $feature.MP06024a_B; var independent = $feature.MP06026a_B; var parties = [republican, democrat, independent]; var total = Sum(parties); var max = Max(parties);
return (max / total) * 100; </script> <script type="module"> const [Map, FeatureLayer] = await $arcgis.import(["@arcgis/core/Map.js", "@arcgis/core/layers/FeatureLayer.js"]);
// Get Arcade expression returning the winning party from the // winning-party script referenced above. const winnerArcade = document.getElementById("winning-party").text;
// Add the expression to a unique value renderer and create a fill // symbol for each possible value.
const renderer = { type: "unique-value", // autocasts as new UniqueValueRenderer() valueExpression: winnerArcade, valueExpressionTitle: "Counties by dominant party among registered voters", uniqueValueInfos: [ { value: "democrat", symbol: createSymbol("#00c3ff"), label: "Democrat", }, { value: "republican", symbol: createSymbol("#ff002e"), label: "Republican", }, { value: "independent", symbol: createSymbol("#faff00"), label: "Independent/other party", }, ], };
// Add an Arcade expression to an opacity visual variable. Counties where // the predominant party is nearly equal with the others (33%) are very // transparent. Counties where the dominant party makes up at least 44% // of all people registered to a party are fully opaque.
// Get Arcade expression returning the strength of the win from the // `strength` script referenced above. const strengthArcade = document.getElementById("strength").text;
renderer.visualVariables = [ { type: "opacity", valueExpression: strengthArcade, valueExpressionTitle: "Share of registered voters comprising the dominant party", stops: [ { value: 33, opacity: 0.05, label: "< 33%" }, { value: 44, opacity: 1.0, label: "> 44%" }, ], }, ];84 collapsed lines
// Create FeatureLayer instance with popupTemplate
const layer = new FeatureLayer({ portalItem: { // autocasts as new PortalItem() id: "8444e275037549c1acab02d2626daaee", }, title: "Dominant political party by number of registered voters for each U.S. County", outFields: ["MP06025a_B", "MP06024a_B", "MP06026a_B", "COUNTY", "STATE", "POP18UP_CY"], renderer: renderer, popupTemplate: { // autocasts as new PopupTemplate() title: "{COUNTY}, {STATE}", content: [ { type: "fields", fieldInfos: [ { fieldName: "MP06024a_B", label: "Democrat", fieldFormat: { type: "number", usegrouping: "always", minimumFractionDigits: 0, maximumFractionDigits: 0, }, }, { fieldName: "MP06025a_B", label: "Republican", fieldFormat: { type: "number", usegrouping: "always", minimumFractionDigits: 0, maximumFractionDigits: 0, }, }, { fieldName: "MP06026a_B", label: "Independent/Other", fieldFormat: { type: "number", usegrouping: "always", minimumFractionDigits: 0, maximumFractionDigits: 0, }, }, { fieldName: "POP18UP_CY", label: "Adult Population", fieldFormat: { type: "number", usegrouping: "always", minimumFractionDigits: 0, maximumFractionDigits: 0, }, }, ], }, ], }, });
document.querySelector("arcgis-map").map = new Map({ basemap: "dark-gray-vector", layers: [layer], });
// Creates a SimpleFillSymbol based on an input color
function createSymbol(color) { return { type: "simple-fill", // autocasts as new SimpleFillSymbol() color: color, outline: { width: 0.2, color: [0, 0, 0, 0.1], }, }; } </script> </body></html>See the Arcade - expression language guide for more details about how to use Arcade in other parts of the ArcGIS Maps SDK for JavaScript