Hello Esri Community,
I’m new to using Arcade expressions and I’m currently working on designing a chart element for a dashboard. My goal is to group average scores into 5 distinct categories to improve the clarity of the visual representation in my pie chart.
Here’s what I’ve accomplished so far:
As more data is collected, this setup will likely become cluttered and less effective.
What I Need Help With: I want to categorize the average scores into 5 distinct groups so that the pie chart will be more organized and easier to interpret. Here are example category titles:
Is there any way to do this within Arcade? Any advice or suggestions would be appreciated!
Here is my code so far:
//access steel structure results layer
var p = 'https://www.arcgis.com/'
var itemId = 'xyzxyzxyzxyzx'
var fs = FeatureSetByPortalItem(Portal(p), itemId, 0, ['Leg_1_Ftg_Score', 'Leg_2_Ftg_Score', 'Leg_3_Ftg_Score', 'Leg_4_Ftg_Score', 'Mid_Body_Score', 'Upper_Body_Arms_Score', 'score', 'Coating_Cond_Score', 'groundingscore', 'cathodicprotection_score' ], false);
//create empty dictionary
var fsDict = {
'fields': [
{'name': 'Leg_1_Ftg_Score', 'type': 'esriFieldTypeInteger'},
{'name': 'Leg_2_Ftg_Score', 'type': 'esriFieldTypeInteger'},
{'name': 'Leg_3_Ftg_Score', 'type': 'esriFieldTypeInteger'},
{'name': 'Leg_4_Ftg_Score', 'type': 'esriFieldTypeInteger'},
{'name': 'Mid_Body_Score', 'type': 'esriFieldTypeInteger'},
{'name': 'Upper_Body_Arms_Score', 'type': 'esriFieldTypeInteger'},
{'name': 'score', 'type': 'esriFieldTypeInteger'},
{'name': 'Coating_Cond_Score', 'type': 'esriFieldTypeInteger'},
{'name': 'groundingscore', 'type': 'esriFieldTypeInteger'},
{'name': 'cathodicprotection_score', 'type': 'esriFieldTypeInteger'},
{'name': 'averagescore', 'type': 'esriFieldTypeDouble'}
],
'geometryType': '',
'features': []
};
var index = 0;
//loop through features in the featureset to populate dictionary
for (var feature in fs) {
fsDict.features[index++] = {
'attributes': {
'Leg_1_Ftg_Score': feature['Leg_1_Ftg_Score'],
'Leg_2_Ftg_Score': feature['Leg_2_Ftg_Score'],
'Leg_3_Ftg_Score': feature['Leg_3_Ftg_Score'],
'Leg_4_Ftg_Score': feature['Leg_4_Ftg_Score'],
'Mid_Body_Score': feature['Mid_Body_Score'],
'Upper_Body_Arms_Score': feature['Upper_Body_Arms_Score'],
'score': feature['score'],
'Coating_Cond_Score': feature['Coating_Cond_Score'],
'groundingscore': feature['groundingscore'],
'cathodicprotection_score': feature['cathodicprotection_score'],
//calculate average and populate in dictionary
'averagescore': (Number(feature['Leg_1_Ftg_Score']) + Number(feature['Leg_2_Ftg_Score']) + Number(feature['Leg_3_Ftg_Score']) + Number(feature['Leg_4_Ftg_Score']) + Number(feature['Mid_Body_Score']) + Number(feature['Upper_Body_Arms_Score']) + Number(feature['score']) + Number(feature['Coating_Cond_Score']) + Number(feature['groundingscore']) + Number(feature['cathodicprotection_score']))/10
}
}
}
//return feature set
return FeatureSet(Text(fsDict))
Please let me know what additional info may be helpful, if any. TIA!
Solved! Go to Solution.
You can add an average score category field to your new dict and categorize it in your new featureset then use that for your chart instead of the uncategorized averages. I'm on mobile, but something like (in your new fs):
'averageScoreCat': When(averagescore > 4, "Score above 4", averagescore < 4 && averagescore >= 3, "Score between 3 and 3.9", "No average score calculated")
I didn't type out all the category possibilities, you can just keep adding as you need.
Also, just as a side note because I'm curious, but a) what are you intending to do with the index variable? And b) were you having a problem getting the average without the Number function?
You can add an average score category field to your new dict and categorize it in your new featureset then use that for your chart instead of the uncategorized averages. I'm on mobile, but something like (in your new fs):
'averageScoreCat': When(averagescore > 4, "Score above 4", averagescore < 4 && averagescore >= 3, "Score between 3 and 3.9", "No average score calculated")
I didn't type out all the category possibilities, you can just keep adding as you need.
Also, just as a side note because I'm curious, but a) what are you intending to do with the index variable? And b) were you having a problem getting the average without the Number function?
Thank you, Nicole! This was extremely helpful. I was previously trying to use an "IF" statement instead of the "When" statement. I was having trouble defining my dictionary item 'averagescore' as a variable, but found a workaround for the time being using the full average calculation... it's very clunky looking on my end, but it works exactly as desired!
Here is what I ended up using to populate the 'averageScoreCat' field in my feature set:
//populate score category feature using averages
'averageScoreCat': When(
(Number(feature['Leg_1_Ftg_Score']) + Number(feature['Leg_2_Ftg_Score']) + Number(feature['Leg_3_Ftg_Score']) + Number(feature['Leg_4_Ftg_Score']) + Number(feature['Mid_Body_Score']) + Number(feature['Upper_Body_Arms_Score']) + Number(feature['score']) + Number(feature['Coating_Cond_Score']) + Number(feature['groundingscore']) + Number(feature['cathodicprotection_score'])) / 10 > 4, 'Score Above 4',
(Number(feature['Leg_1_Ftg_Score']) + Number(feature['Leg_2_Ftg_Score']) + Number(feature['Leg_3_Ftg_Score']) + Number(feature['Leg_4_Ftg_Score']) + Number(feature['Mid_Body_Score']) + Number(feature['Upper_Body_Arms_Score']) + Number(feature['score']) + Number(feature['Coating_Cond_Score']) + Number(feature['groundingscore']) + Number(feature['cathodicprotection_score'])) / 10 < 4 && (Number(feature['Leg_1_Ftg_Score']) + Number(feature['Leg_2_Ftg_Score']) + Number(feature['Leg_3_Ftg_Score']) + Number(feature['Leg_4_Ftg_Score']) + Number(feature['Mid_Body_Score']) + Number(feature['Upper_Body_Arms_Score']) + Number(feature['score']) + Number(feature['Coating_Cond_Score']) + Number(feature['groundingscore']) + Number(feature['cathodicprotection_score'])) / 10 >= 3, 'Score between 3 and 3.9',
(Number(feature['Leg_1_Ftg_Score']) + Number(feature['Leg_2_Ftg_Score']) + Number(feature['Leg_3_Ftg_Score']) + Number(feature['Leg_4_Ftg_Score']) + Number(feature['Mid_Body_Score']) + Number(feature['Upper_Body_Arms_Score']) + Number(feature['score']) + Number(feature['Coating_Cond_Score']) + Number(feature['groundingscore']) + Number(feature['cathodicprotection_score'])) / 10 < 3 && (Number(feature['Leg_1_Ftg_Score']) + Number(feature['Leg_2_Ftg_Score']) + Number(feature['Leg_3_Ftg_Score']) + Number(feature['Leg_4_Ftg_Score']) + Number(feature['Mid_Body_Score']) + Number(feature['Upper_Body_Arms_Score']) + Number(feature['score']) + Number(feature['Coating_Cond_Score']) + Number(feature['groundingscore']) + Number(feature['cathodicprotection_score'])) / 10 >= 2, 'Score between 2 and 2.9',
(Number(feature['Leg_1_Ftg_Score']) + Number(feature['Leg_2_Ftg_Score']) + Number(feature['Leg_3_Ftg_Score']) + Number(feature['Leg_4_Ftg_Score']) + Number(feature['Mid_Body_Score']) + Number(feature['Upper_Body_Arms_Score']) + Number(feature['score']) + Number(feature['Coating_Cond_Score']) + Number(feature['groundingscore']) + Number(feature['cathodicprotection_score'])) / 10 < 1 && (Number(feature['Leg_1_Ftg_Score']) + Number(feature['Leg_2_Ftg_Score']) + Number(feature['Leg_3_Ftg_Score']) + Number(feature['Leg_4_Ftg_Score']) + Number(feature['Mid_Body_Score']) + Number(feature['Upper_Body_Arms_Score']) + Number(feature['score']) + Number(feature['Coating_Cond_Score']) + Number(feature['groundingscore']) + Number(feature['cathodicprotection_score'])) / 10 >= 1, 'Score between 1 and 1.9',
'No Average Score Calculated')
Also, to answer the questions:
a) I am not sure what the index variable does exactly, but I used this video as the inspiration (timestamp 6:20): https://youtu.be/rowW2PistiQ?si=MtRzalunD6frOLU6&t=382. The variable is referenced again in line 28 "[index++]". I still have a lot of learning to go when writing code, there is a possibility this was an unnecessary step 🙂
b) I was indeed having a problem getting the average without the Number function 😞 The data was pulled from Survey123 and all fields were automatically set up as String instead of Integer for this data. The Number function was the only way the calculation worked without getting an error message.
Thank you again for your help!!
Oh, that completely makes sense about the scores being strings (I just assumed they were already numbers and was confused why you'd need Number).
Thanks for the video! I'll have to experiment (and I've basically never touched Dashboards, so maybe there's something unique about Arcade expressions there, or the difference between writing an expression for a pop-up (what I'm used to doing) vs a chart).