Custom order values in Serial Chart using Arcade.

591
4
03-22-2023 01:20 PM
CodexRemote
New Contributor

Hello!

I'm writing a Arcade expression to input data in a serial chart in Dashboards.

The goal is to replace the string values to numbers so I can order the bars and keep the string as the label.

This function retrieves the feature set using FeatureSetByPortalItem, gets the first feature using First, and assigns the value of the 'ultima_consulta_medica' field to the variable ultima_consulta_medica.

The Replace function is then called on ultima_consulta_medica, replacing the string 'Menos de 1 mês' with the number 1. 

 

var featureSet = FeatureSetByPortalItem(Portal('myPortal'), 'xxxxx' , 1, ['ultima_consulta_medica'], false);
var feature = First(featureSet);
var ultima_consulta_medica = feature['ultima_consulta_medica'];
ultima_consulta_medica = Replace(ultima_consulta_medica, 'Menos de 1 mês', '1');

return ultima_consulta_medica;

 

I was stuck trying to make the expression loop through all the values of the ultima_consulta_medica. I need the end result to replace the string with '1'. 

I also don't know if my approach was the best way to resolve my problem.

Any suggestions?

0 Kudos
4 Replies
jcarlson
MVP Esteemed Contributor

In order to loop through your features, you literally need to use a for loop. In Data Expressions, that usually means creating a dictionary and pushing features into it, one by one.

var fs = FeatureSetByPortalItem(...)

var out_dict = {
    fields: [{name: 'ultima_consulta_medica', type: 'esriFieldTypeString'}],
    geometryType: '',
    features: []
}

for (var f in fs) {
    
    var new_str = Replace(
        f['ultima_consulta_medica'],
        'Menos de 1 mês',
        '1'
    )
    
    Push(
        out_dict['features'],
        {attributes: {ultima_consulta_medica: new_str}
    )
}

return FeatureSet(Text(out_dict))

 

There are more efficient ways to do this sort of thing, though, if the data permits it. Does the string "Menos de 1 mes" appear in a consistent position?

- Josh Carlson
Kendall County GIS
0 Kudos
AnnaScholl
Occasional Contributor

Hi @jcarlson Josh--

What might be more efficient ways? I am dealing with this problem on a feature view from a survey (therefore cannot edit the layer to add, say, a sequence number to sort the bars). An image is below. As you can see, things are out of order.  

LydiaYoungblood_0-1687511732122.png

Thanks for any ideas!

 

 

jcarlson
MVP Esteemed Contributor

With data expressions, you can easily add new "fields" to the output, even if they do not exist in the input. You could add a new field, "sequence_number", and when you use the Push function to add a new feature to the output, include an expression like Decode that can convert your text values to a number.

Then just use that numeric value to sort the chart data!

- Josh Carlson
Kendall County GIS
0 Kudos
AnnaScholl
Occasional Contributor

Hi @jcarlson . Thank you! Do you happen to know if there is an example code in the master? I've had a look but couldn't find anything. This seems like something that should be quite easy, but I am a complete beginner, so I am not really sure how to proceed. https://github.com/Esri/arcade-expressions/tree/master


 

 

 

 

 

0 Kudos