Arcade Expression for Dominant Category

341
1
08-15-2023 08:17 AM
Labels (1)
RobertCox87
New Contributor II

I am symbolizing a crime layer by Predominant Category in ArcGIS Online and I am trying to create an Arcade expression in the Pop-up configuration that displays  dominant field (Property Crime or Personal Crime).  How would I write a script to say "Dominant Type:" followed by the dominant field?

0 Kudos
1 Reply
Michael_Kraus
Occasional Contributor

Hi Robert - I did a similar thing with Arcade (using a sample that I can't find the source of right now - apologies to the author).  I did "Top 5", you could change the first line to = 1 to just get the top one. 

Below is a screenshot of the popup and the code.  The only bug / issue at the moment is that equal ranks / scores are all showing up - I would prefer it to just take the first one in that case.

Screenshots below...

Michael_Kraus_0-1692142283390.png

Example with equal ranks:

Michael_Kraus_1-1692142335763.png

 

Code below....


var numTopValues = 5;

var groups = [

{ value: $feature["P_Afghanistan_Tot"], alias: "Afghanistan"},
{ value: $feature["P_Bangladesh_Tot"], alias: "Bangladesh"},
{ value: $feature["P_Bosnia_Herzegov_Tot"], alias: "Bosnia and Herzegovina"},
{ value: $feature["P_Brazil_Tot"], alias: "Brazil"},
{ value: $feature["P_Cambodia_Tot"], alias: "Cambodia"},
{ value: $feature["P_Canada_Tot"], alias: "Canada"},
{ value: $feature["P_Chile_Tot"], alias: "Chile"},
{ value: $feature["P_China_Tot"], alias: "China"},
{ value: $feature["P_Croatia_Tot"], alias: "Croatia"},
{ value: $feature["P_Egypt_Tot"], alias: "Egypt"},
{ value: $feature["P_England_Tot"], alias: "England"},
{ value: $feature["P_Fiji_Tot"], alias: "Fiji"},
{ value: $feature["P_France_Tot"], alias: "France"},
{ value: $feature["P_Germany_Tot"], alias: "Germany"},
{ value: $feature["P_Greece_Tot"], alias: "Greece"},
{ value: $feature["P_Hong_Kong_SAR_Ch_Tot"], alias: "Hong Kong"},
{ value: $feature["P_India_Tot"], alias: "India"},
{ value: $feature["P_Indonesia_Tot"], alias: "Indonesia"},
{ value: $feature["P_Iran_Tot"], alias: "Iran"},
{ value: $feature["P_Iraq_Tot"], alias: "Iraq"},
{ value: $feature["P_Ireland_Tot"], alias: "Ireland"},
{ value: $feature["P_Italy_Tot"], alias: "Italy"},
{ value: $feature["P_Japan_Tot"], alias: "Japan"},
{ value: $feature["P_Korea_South_Tot"], alias: "South Korea"},
{ value: $feature["P_Lebanon_Tot"], alias: "Lebanon"},
{ value: $feature["P_Malaysia_Tot"], alias: "Malaysia"},
{ value: $feature["P_Malta_Tot"], alias: "Malta"},
{ value: $feature["P_Mauritius_Tot"], alias: "Mauritius"},
{ value: $feature["P_Myanmar_Tot"], alias: "Myanmar"},
{ value: $feature["P_Nepal_Tot"], alias: "Nepal"},
{ value: $feature["P_Netherlands_Tot"], alias: "Netherlands"},
{ value: $feature["P_New_Zealand_Tot"], alias: "New Zealand"},
{ value: $feature["P_Pakistan_Tot"], alias: "Pakistan"},
{ value: $feature["P_PNG_Tot"], alias: "PNG"},
{ value: $feature["P_Philippines_Tot"], alias: "Philippines"},
{ value: $feature["P_Poland_Tot"], alias: "Poland"},
{ value: $feature["P_Scotland_Tot"], alias: "Scotland"},
{ value: $feature["P_Singapore_Tot"], alias: "Singapore"},
{ value: $feature["P_South_Africa_Tot"], alias: "South Africa"},
{ value: $feature["P_Sri_Lanka_Tot"], alias: "Sri Lanka"},
{ value: $feature["P_Taiwan_Tot"], alias: "Taiwan"},
{ value: $feature["P_Thailand_Tot"], alias: "Thailand"},
{ value: $feature["P_North_Macedonia_Tot"], alias: "North Macedonia"},
{ value: $feature["P_Turkey_Tot"], alias: "Turkey"},
{ value: $feature["P_USA_Tot"], alias: "USA"},
{ value: $feature["P_Vietnam_Tot"], alias: "Vietnam"},
{ value: $feature["P_Wales_Tot"], alias: "Wales"},
{ value: $feature["P_Zimbabwe_Tot"], alias: "Zimbabwe"}

// ADD MORE FIELDS AS NECESSARY
];

function getValuesArray(a){
var valuesArray = []
for(var i in a){
valuesArray[i] = a[i].value;
}
return valuesArray;
}

function findAliases(top5a,fulla){
var aliases = [];
var found = "";
for(var i in top5a){
for(var k in fulla){
if(top5a[i] == fulla[k].value && Find(fulla[k].alias, found) == -1){
found += fulla[k].alias;
aliases[Count(aliases)] = {
alias: fulla[k].alias,
value: top5a[i]
};
}
}
}
return aliases;
}

function getTopGroups(groupsArray){

var values = getValuesArray(groupsArray);
var top5Values = IIF(Max(values) > 0, Top(Reverse(Sort(values)),numTopValues), "Insufficient Population");
var top5Aliases = findAliases(top5Values,groupsArray);

if(TypeOf(top5Values) == "String"){
return top5Values;
} else {
var content = "";
for(var i in top5Aliases){
content += (i+1) + ". " + top5Aliases[i].alias + " (" + Text(top5Aliases[i].value, "#,###") + ")";
content += IIF(i < numTopValues-1, TextFormatting.NewLine, "");
}
}

return content;
}

getTopGroups(groups);

0 Kudos