Arcade help - labels

196
4
Jump to solution
3 weeks ago
AmberKelly
New Contributor III

Hi All,

 

Apologies if my terminology is incorrect - I'm a novice in Arcade.

I am trying to avoid having multiple label classes by merging my individual arcade label class expressions into one. However I am finding that if one return is met, the following return will not be labelled.

Perhaps the best explanation is an example:

I want to label X when ASSETID is not NULL, additionally i want to label Y when DIAMETER is not NULL AND when width is either NULL or "UNKNOWN". 

 

 

if (IsEmpty($feature.ASSETID) == False){
return "X"}

else if ((IsEmpty($feature.DIAMETER) == False) && ((IsEmpty($feature.WIDTH)) || ($feature.WIDTH == "UNKNOWN")))
    if ($feature.DIAMETER != "UNKNOWN"){
    return "Y"}

 

 

 

I want both labels to appear, but it seems if X is labelled then Y isnt dealt with. I will eventually have a few more labels (eg Z...) I want to also include but am hoping to get this working first.

In researching I have found examples on how to create multiple if else expressions on a single label but haven't found anything on creating multiple labels without any relationship to the previous "returns" output within the one label expression.

Thanks for taking your time to look at this and I hope this makes sense.

 

Cheers

Amber

 

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

Any time you have a return, that ends the expression, and nothing after that will be evaluated. Also, whether you have a return in the first block or not, since the second condition has "else" in front of it, it will only evaluate if the preceding condition is false.

And also, the second condition block has a nested If statement, but only a single closing bracket, so it would probably throw an error if the expression got past the first condition.

If you're trying to build an expression one piece at a time, there are a couple ways to do it. A common approach is to create a text variable and stick more text onto the end of it as needed.

Personally, I like to move values into an array and use Concatenate, but I'll show you both. Here's with text:

var out_text = ''

// label X when ASSETID not null; note using "!" to negate a statement
if ( !IsEmpty($feature.ASSETID) ) {
  out_text += 'X'
}

// label Y when DIAMETER not null and width null / UNKNOWN
if ( !IsEmpty($feature.DIAMETER) && ( IsEmpty($feature.WIDTH) || $feature.WIDTH == 'UNKNOWN' )) {
  out_text += '\nY'  // include a line break to keep separate from X, if both show up
}

return out_text

And with an array:

var out_arr = []

// label X when ASSETID not null; note using "!" to negate a statement
if ( !IsEmpty($feature.ASSETID) ) {
  Push(out_arr, 'X')
}

// label Y when DIAMETER not null and width null / UNKNOWN
if ( !IsEmpty($feature.DIAMETER) && ( IsEmpty($feature.WIDTH) || $feature.WIDTH == 'UNKNOWN' )) {
  Push(out_arr, 'Y')
}

return Concatenate(out_arr, '\n')

The array approach has the benefit of only inserting line breaks when necessary. The text variable option will probably end up inserting too many line breaks in some situations.

- Josh Carlson
Kendall County GIS

View solution in original post

4 Replies
jcarlson
MVP Esteemed Contributor

Any time you have a return, that ends the expression, and nothing after that will be evaluated. Also, whether you have a return in the first block or not, since the second condition has "else" in front of it, it will only evaluate if the preceding condition is false.

And also, the second condition block has a nested If statement, but only a single closing bracket, so it would probably throw an error if the expression got past the first condition.

If you're trying to build an expression one piece at a time, there are a couple ways to do it. A common approach is to create a text variable and stick more text onto the end of it as needed.

Personally, I like to move values into an array and use Concatenate, but I'll show you both. Here's with text:

var out_text = ''

// label X when ASSETID not null; note using "!" to negate a statement
if ( !IsEmpty($feature.ASSETID) ) {
  out_text += 'X'
}

// label Y when DIAMETER not null and width null / UNKNOWN
if ( !IsEmpty($feature.DIAMETER) && ( IsEmpty($feature.WIDTH) || $feature.WIDTH == 'UNKNOWN' )) {
  out_text += '\nY'  // include a line break to keep separate from X, if both show up
}

return out_text

And with an array:

var out_arr = []

// label X when ASSETID not null; note using "!" to negate a statement
if ( !IsEmpty($feature.ASSETID) ) {
  Push(out_arr, 'X')
}

// label Y when DIAMETER not null and width null / UNKNOWN
if ( !IsEmpty($feature.DIAMETER) && ( IsEmpty($feature.WIDTH) || $feature.WIDTH == 'UNKNOWN' )) {
  Push(out_arr, 'Y')
}

return Concatenate(out_arr, '\n')

The array approach has the benefit of only inserting line breaks when necessary. The text variable option will probably end up inserting too many line breaks in some situations.

- Josh Carlson
Kendall County GIS
AmberKelly
New Contributor III

Josh - so much gold in this reply! I've copied it to my notes for future reference.

Thanks this has done just what I was after - thank you for explaining why an array is the preferred option too.

Gotta love the internet! 😄

0 Kudos
ZachBodenner
MVP Regular Contributor

You could try a When statement like 

var AID = $feature.AssetID

var DIA = $feature.diameter

var Wid = $feature.width

When(AID != null, 'X', DIA != null && WID == null, 'Y', DIA != null && WID == 'UNKNOWN', 'Y', '')

 

Admittedly, Arcade labeling can feel really limited and I don't actually know if this will work. 

0 Kudos
AmberKelly
New Contributor III

Thanks for your help with this one Zach. It didnt play ball exactly as I had intended but has led me down an interesting path learning about When statements.

Much thanks!

0 Kudos