Select to view content in your preferred language

Need conditional formatting to color labels based on value

376
2
07-11-2024 06:41 AM
ECSFloridaECSLimited
New Contributor

I am working with monitoring well sample data and have test results labelled for April and June. But I also want to set a condition to output a red label if the value is 10 or higher. And I don't want the 'NS' labels colored, just the 10 or higher values. Here is what I am looking at.

condform.png

 

0 Kudos
2 Replies
ZachBodenner
MVP Regular Contributor

Do you mind if the date is colored also? Probably the easiest things to do would be to set up two label classes, one for each of the value ranges based on the test result:

ZachBodenner_0-1720706171801.png

 

My example above has two different label classes based on the size of the feature. Then in the label class, use the SQL tab to set the criteria based on the results value, and style accordingly

ZachBodenner_1-1720706229994.png

 

0 Kudos
jcarlson
MVP Esteemed Contributor

Dynamic labels in Pro are pretty limited, but conditionally changing colors is possible.

Since there are "NS" values, are those actually text fields? I'll assume so for this post.

In text, what we're doing is

  1. For each numeric field:
    1. Get the numeric value of the attribute
    2. Compare against a threshold
    3. Output "red" or "black" in a font element tag
  2. Combine everything into a single text string

Since we're doing a series of steps more than once, I'm going to use a custom function.

Here's what the code could look like:

function colorCode(x) {
  // default color is black
  var col = 'red = "0"'
  // check for numeric value
  if (x != 'NS') {
    // convert to number
    x = Number(x)
    // greater than 10, color is red
    if (x >= 10) {
      col = 'red = "255"'
    }
  }
  // return text
  return `<CLR ${col}>${x}</CLR>`
}

return `April: ${colorCode($feature['ArsTestApr'])}
June: ${colorCode($feature['ArsTestJun'])}`

 

Note, I haven't tested this in Pro. In other contexts, a literal line break in a `backtick string` will come through in the output, but I know Pro can be a little different in how it handles Arcade.

Here's me testing this with some made up values:

jcarlson_0-1720707195996.png

jcarlson_1-1720707220097.png

 

- Josh Carlson
Kendall County GIS
0 Kudos