Arcade expression to calculate percentage in pie chart?

632
2
03-29-2022 08:40 PM
Hayley
by
Occasional Contributor II

Hi there, 

I am trying to calculate the percentage of ethnic groups out of the entire population with an Arcade expression. This is what I have so far, any pointers on how to get this outputting a percentage rounded to 1 dp? Thanks!

#have connected to my portal and choosing the fields I want
var portal = Portal('https://arcgis.com');
var fs = FeatureSetByPortalItem(
    portal,
    '9b38b9f62ab74cb0aed6649d0f713cfa',
    0,
    [
        'C18_Ethnicity_L1_European',
        'PopCount_2018'
    ],
    false
);

#trying to sum the value of the two fields     
var european = Sum('C18_Ethnicity_L1_European')
var popcount = Sum('PopCount_2018')

#trying to calculate the percentage
var europeanperc = (european/popcount)*100

return FeatureSet(Number(europeanperc))
HL
2 Replies
jcarlson
MVP Esteemed Contributor

For one thing, you can output a percentage using the Text function. When your formatting string includes a "%" character, it will automatically multiply your result by 100. Including a "0" after the decimal in the formatting string ensures that the result is given to one decimal point.

jcarlson_0-1648643840398.png

Second, and more importantly, a FeatureSet has to be created from a dictionary of items that follow the FeatureSet specifications. But I don't know that you need it for this, honestly.

With a Pie Chart widget, you should be able to set the configuration to Fields, choose your fields, and set the statistic to Sum.

jcarlson_1-1648644254977.png

You can then adjust the percentage formatting to your requirements.

jcarlson_2-1648644321029.png

 

In other words, just let the chart itself handle the summing and formatting, you just need to connect it to the data.

Is the issue that there's no "non-european" field in the data?

- Josh Carlson
Kendall County GIS
Hayley
by
Occasional Contributor II

Hi Josh, thanks for your response.

The layer data expression in Dashboards requires you to return a FeatureSet which is why it stumped me.

I would also like to calculate a percentage based on another field so I cannot just use "Sum" (i.e. I'd like to calculate each ethnic group percentage out of the whole population count rather than just the other ethnic groups). 


Hope that makes sense

HL