Select to view content in your preferred language

Arcade - Some Labels Won't Show

348
5
Jump to solution
08-20-2024 09:27 AM
Labels (3)
FaithBest
Occasional Contributor

FaithBest_0-1724170945016.png

Var FtName = $feature["NAME"]
var FtSplit = Split(FtName, ' ', -1)
Var Counter = Count(FtSplit)
var FirstLine = FtSplit[0] + ' ' + FtSplit[1]
Var SecondLine = Replace(FtName, FirstLine, '')
Var SecondLine2 = FtSplit[2] + ' ' + FtSplit[3]
Var ThirdLine = Replace(SecondLine, SecondLine2, '')

If (Counter > 3){
    If(Counter > 4){
        Var output2 = FirstLine + TextFormatting.NewLine + SecondLine2 + TextFormatting.NewLine + ThirdLine
        Return output2
    }else{
        Var output1 = FirstLine + TextFormatting.NewLine + SecondLine
        Return output1 
    }
}else{
    Return $feature["NAME"]
}

Hi All,

I probably have something wrong with my Arcade expression I just can't figure out what. 

When I use this code, all labels with less than 3 words disappear and will not show. I am trying to create an expression that can handle varied number of word labels and create a stacked view for the labels so when they are published to Portal and viewed in Map Viewer they look a little nicer. 

If anyone has any suggestions or sees the problem, help would be appreciated!

0 Kudos
3 Solutions

Accepted Solutions
RhettZufelt
MVP Notable Contributor

This seems to work regardless of the number of words:

 

Var FtName = Replace($feature["NAME"], ' ', TextFormatting.NewLine)
return FtName

 

R_

View solution in original post

jcarlson
MVP Esteemed Contributor

There's a lot of stuff going on that I don't think is necessary. When you're putting your line items back together, you can just use Concatenate and Slice to get the array values you want.

And we can actually get rid of the counter, too! Slice will return empty strings, so we can just check the length of those strings.

var FtName = $feature["NAME"];
var FtSplit = Split(FtName, " ", -1);

var FirstLine = Concatenate(Slice(FtSplit, 0, 2), " ");
var SecondLine = Concatenate(Slice(FtSplit, 2, 4), " ");
var ThirdLine = Concatenate(Slice(FtSplit, 4), " ");

if (Count(ThirdLine) > 0) {
  return `${FirstLine}\n${SecondLine}\n${ThirdLine}`
} else if (Count(SecondLine) > 0) {
  return `${FirstLine}\n${SecondLine}`
} else {
  return $feature['NAME']
}

 

 

 

jcarlson_1-1724175691902.png

jcarlson_2-1724175709170.png

jcarlson_3-1724175732127.png

 

- Josh Carlson
Kendall County GIS

View solution in original post

KenBuja
MVP Esteemed Contributor

The reason you're not getting any results for a name less than four words is that you're using specific index values in the split array that wouldn't exist in lines 4 and 6 for shorter words. This should work regardless of the number of words in the name. The third line will contain anything more than four words.

 

 

Var FtName = $feature['Name'];
var FtSplit = Split(FtName, ' ')
Var Counter = Count(FtSplit)
When (Counter < 4, FtName,
      Counter == 4, Concatenate(Slice(Ftsplit,0, 2), ' ') + TextFormatting.NewLine + Concatenate(Slice(FtSplit, 2, 4), ' '),
      Concatenate(Slice(Ftsplit,0, 2), ' ') + TextFormatting.NewLine + Concatenate(Slice(FtSplit, 2, 4), ' ')  + TextFormatting.NewLine + Concatenate(Slice(FtSplit, 4), ' ')
);

View solution in original post

5 Replies
KenBuja
MVP Esteemed Contributor

When asking a question about code, you should insert the code using the "Insert/edit code sample" button instead of posting an image of the code. It makes it easier to duplicate it in our testing environments.

FaithBest
Occasional Contributor

Oh Thank you @KenBuja 

Var FtName = $feature["NAME"]
var FtSplit = Split(FtName, ' ', -1)
Var Counter = Count(FtSplit)
var FirstLine = FtSplit[0] + ' ' + FtSplit[1]
Var SecondLine = Replace(FtName, FirstLine, '')
Var SecondLine2 = FtSplit[2] + ' ' + FtSplit[3]
Var ThirdLine = Replace(SecondLine, SecondLine2, '')

If (Counter > 3){
    If(Counter > 4){
        Var output2 = FirstLine + TextFormatting.NewLine + SecondLine2 + TextFormatting.NewLine + ThirdLine
        Return output2
    }else{
        Var output1 = FirstLine + TextFormatting.NewLine + SecondLine
        Return output1 
    }
}else{
    Return $feature["NAME"]
}

 

0 Kudos
RhettZufelt
MVP Notable Contributor

This seems to work regardless of the number of words:

 

Var FtName = Replace($feature["NAME"], ' ', TextFormatting.NewLine)
return FtName

 

R_

jcarlson
MVP Esteemed Contributor

There's a lot of stuff going on that I don't think is necessary. When you're putting your line items back together, you can just use Concatenate and Slice to get the array values you want.

And we can actually get rid of the counter, too! Slice will return empty strings, so we can just check the length of those strings.

var FtName = $feature["NAME"];
var FtSplit = Split(FtName, " ", -1);

var FirstLine = Concatenate(Slice(FtSplit, 0, 2), " ");
var SecondLine = Concatenate(Slice(FtSplit, 2, 4), " ");
var ThirdLine = Concatenate(Slice(FtSplit, 4), " ");

if (Count(ThirdLine) > 0) {
  return `${FirstLine}\n${SecondLine}\n${ThirdLine}`
} else if (Count(SecondLine) > 0) {
  return `${FirstLine}\n${SecondLine}`
} else {
  return $feature['NAME']
}

 

 

 

jcarlson_1-1724175691902.png

jcarlson_2-1724175709170.png

jcarlson_3-1724175732127.png

 

- Josh Carlson
Kendall County GIS
KenBuja
MVP Esteemed Contributor

The reason you're not getting any results for a name less than four words is that you're using specific index values in the split array that wouldn't exist in lines 4 and 6 for shorter words. This should work regardless of the number of words in the name. The third line will contain anything more than four words.

 

 

Var FtName = $feature['Name'];
var FtSplit = Split(FtName, ' ')
Var Counter = Count(FtSplit)
When (Counter < 4, FtName,
      Counter == 4, Concatenate(Slice(Ftsplit,0, 2), ' ') + TextFormatting.NewLine + Concatenate(Slice(FtSplit, 2, 4), ' '),
      Concatenate(Slice(Ftsplit,0, 2), ' ') + TextFormatting.NewLine + Concatenate(Slice(FtSplit, 2, 4), ' ')  + TextFormatting.NewLine + Concatenate(Slice(FtSplit, 4), ' ')
);