Arrcade - Color Text Conditionally

4832
13
Jump to solution
04-19-2022 04:26 AM
GRmapper
Occasional Contributor

Hello All,

I have an Arcade expression that conditionally colors a piece of text in a layout.  If I remove the CLR bits it works but with a default color of black. I'd like to get it working with the color assignment if possible. Here is the current statement that proves out as working but I don't get the color I just get the actual color statement characters.

I'd appreciate any coding help.

Expression.png

13 Replies
KenBuja
MVP Esteemed Contributor

I just tested this with a Dynamic Text element (a table) and I get some interesting results. First I used your expression and it worked.

color1.png

I then changed the second evaluation to another formatting tag and it worked.

color2.png

But when I added the two tags together, it broke

color3.png

GeoJason
New Contributor III

Hey everyone! Yesterday I worked with @JacquiMartin and @DallasCrow  to come up with a solution for having font color changes based on conditional statements in a layout (more specifically, a map series). When applying a dynamic text it generates some XML tags in the Format Text pane. This is working by creating a duplicate tag that we are passing the formatting tags into without closing. We are closing tags manually with </CLR> at the end in step 5, but this can be dynamic also. Say you want to change the color, set to bold and italic based on conditional arcade, you would just add the conditions in the first-most tag and copy and addf another closing tag with the same conditional statements in the first-most tag but only return closing tags corresponsing to the opening tags. 

This is a work-around and prone to breaking when the font extends outside the bounding box. These are not readily apparent and usually appear after selecting a different page in the map series. Also, pay attention to the spaces and work with the labeling syntax located here.  


1. Create a dynamic text element and select Table Attribute for the source. In the Arcade select the field you want dispayed.

GeoJason_1-1665763659754.png

2. Click on the view source tag above the dynamic text box tag area. Copy the source code and paste a second copy of it above. 

GeoJason_0-1665763510310.png

GeoJason_3-1665763910533.png

You should now have 2 tags when you switch back to tag view. 

3. Click on the first-most tag (the one we pasted in the front, really it just needs to be the first tag not the second). 

4. Edit the Arcade to be something similar to this...

IIF($feature.hexagonID == 401, "<CLR red = '4' green = '56' blue = '106' >", "<CLR red = '0' green = '0' blue = '0' >")

GeoJason_4-1665765127300.png
*Note mind the spaces. Formating has to be exact in the CLR tags.

Click OK and you should see this.

GeoJason_5-1665765268712.pngGeoJason_6-1665765293951.png

5. Add the close tag for the CLR. </CLR> goes directly in the tag section at the end. 

GeoJason_7-1665765380385.png

You should now see this in your map series or layout. 

GeoJason_8-1665765438847.png

For reference here is section 489...

GeoJason_9-1665765535636.png

and here is a screenshot of the entire project...

GeoJason_10-1665765579838.png

Be careful not to double-click on a bounding box after this solution is in place otherwise...

GeoJason_11-1665766179078.png

you will have to delete the broken tags and start over. It only takes a minute or two if you document your code. 

I hope this helps!

 

KieranClark
Occasional Contributor

Hey Jason,

Thank you for this workaround, it does seem to work on the layout page. However, it still breaks when exporting to PDF as either a single page or the whole map series. Did you ever find a more permanent workaround for this?

I'm still trying to figure out what changed between versions as the <CLR> tag used to work perfectly fine in arcade expressions.

 

Edit: Wow, strike all of that - I figured it out.

Thanks to your post, I was able to figure out a way to set up the code so I didn't have to duplicate a tag and then manually add a </CLR> at the end since I figured if I wrapped it all into a variable it would work. The new code is as follows:

var datafield = $feature.CABLE_IS_COMM_AND_ACCEPT;
var comcolor = "<CLR red='84' green='109' blue='63'>" + "Complete" + "</CLR>"
var inccolor = "<CLR red='255' green='121' blue='0'>" + "Incomplete" + "</CLR>"

Iif(datafield == 1, comcolor, inccolor)

 

This code worked in a test dynamic element but when I applied it to our existing ones, it broke again. However, you had made mention of don't double click bounding boxes but that got me thinking if the bounding box itself was the issue. Our bounding boxes for the dynamic text elements had been 1.2 inches wide with text centered since that made everything line up correctly on our layout. What I did instead was to make them all 6 inches long and shift the X position to offset by 2.4 inches to account for the wider box.

This has now allowed the expression to stick while changing pages in the map series and while exporting the series to PDF - all of which had broken before with your workaround of manually adding </CLR> at the end. Additionally, double clicking the bounding box does not break the expression - the full tag text appears when you double click but once you click off the display returns to normal.

Seriously, thank you for these thoughts - you really got me on the right track to solve this issue that GRmapper and I have been slamming our heads against for a while.

GRmapper
Occasional Contributor

Thanks all for the help and continued support getting to the solution. The community continues to help us all do better geo 😀 !!!

0 Kudos