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!
Solved! Go to Solution.
This seems to work regardless of the number of words:
Var FtName = Replace($feature["NAME"], ' ', TextFormatting.NewLine)
return FtName
R_
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']
}
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), ' ')
);
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.
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"]
}
This seems to work regardless of the number of words:
Var FtName = Replace($feature["NAME"], ' ', TextFormatting.NewLine)
return FtName
R_
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']
}
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), ' ')
);