Any help appreciated. I am trying to write an expression that will create a label based on presence or absence of data across a number of fields.
I want the label to include the following:
There are 9 or more items.
eg for the Home location with 2 cars, 4 bikes, and 1 boat the label would look like:
However, if no bikes or boat, then the label would be as below with no extra lines:
The data fields are setup similar to this
Using Arcade I was able to build a multi-line label by using:
Concatenate([
$feature.LOCATION,
'Car: ' + $feature.CAR_count,
'Bike: ' + $feature.BIKE_count,
'Boat: ' + $featureBOAT_count],
TextFormatting.NewLine)
This works fine and outputs a count for all 3 types even when the count is zero. I need to find a way to only add a line when the count is greater than zero. This will simplify the map by reducing the entries in the labels when only 1 or 2 of the 9 (or more) data fields are present.
The solution doesn't have to be using Arcade, could be VBscript, JScript, or Python if you have an easy solution.
Many thanks, Pete
Solved! Go to Solution.
Simple enough! We can use push to add entries to the array only when the condition is met.
var label_array = [$feature.LOCATION]
var counts = {
'Car': $feature.CAR_count,
'Bike': $feature.BIKE_count,
'Boat': $feature.BOAT_count
}
for (var c in counts){
if(counts[c]>0){
Push(
label_array,
`\t${c}: ${counts[c]}`
)
}
}
return Concatenate(
label_array,
'\n'
)
And here's me testing that with some sample values:
Simple enough! We can use push to add entries to the array only when the condition is met.
var label_array = [$feature.LOCATION]
var counts = {
'Car': $feature.CAR_count,
'Bike': $feature.BIKE_count,
'Boat': $feature.BOAT_count
}
for (var c in counts){
if(counts[c]>0){
Push(
label_array,
`\t${c}: ${counts[c]}`
)
}
}
return Concatenate(
label_array,
'\n'
)
And here's me testing that with some sample values:
Hi Josh,
You are a legend. Thank you.
Your solution worked first time and is simple and elegant. You have helped make a very messy map much cleaner and easier to read.
Can you suggest any good resources for learning more about arcade expressions like this?
Many thanks again.
Pete
You're very welcome!
I'd suggest a few things: