Arcade (or other) expression to create multi-line label with variable entries

1692
3
Jump to solution
09-26-2022 03:27 AM
Labels (1)
peterverwey_ses
New Contributor II

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:

  • Location Name - from field
  • IF ItemCount != 0 THEN add a new line with a text label and then the ItemCount value ELSE go to next item 

There are 9 or more items. 

eg for the Home location with 2 cars, 4 bikes, and 1 boat the label would look like:

  • Home
  • Car: 2
  • Bike: 4
  • Boat: 1

However, if no bikes or boat, then the label would be as below with no extra lines:

  • Home
  • Car: 2

The data fields are setup similar to this

  • OID / LOCATION / CAR_count / BIKE_count / BOAT_count

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

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

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:

jcarlson_1-1664196544510.png

 

 

- Josh Carlson
Kendall County GIS

View solution in original post

3 Replies
jcarlson
MVP Esteemed Contributor

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:

jcarlson_1-1664196544510.png

 

 

- Josh Carlson
Kendall County GIS
peterverwey_ses
New Contributor II

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

0 Kudos
jcarlson
MVP Esteemed Contributor

You're very welcome!

I'd suggest a few things:

  1. Checking out the Arcade GitHub repo: https://github.com/Esri/arcade-expressions/
  2. Just messing around on the Arcade Playground: https://developers.arcgis.com/arcade/playground/
  3. Making a layer / map / dashboard that's specifically for just messing around with Arcade, and see what you can make with it.
- Josh Carlson
Kendall County GIS