Dynamically color labels with JSAPI

472
1
02-14-2020 09:14 AM
MatthewBarr1
New Contributor

I'd like to color labels based on the difference between the current time and a datetime field in a CSV layer in a JSAPI application.  Ideally, it would be on a continuous scale (something like recent=green and oldest=red), but if need be, I could divide the scale into a few discrete stops.  I don't see a way to use visual variables with labels, which was my first thought.  My second thought was to create a separate layer just for the labels, but while I could use a visual variable to control the color, I couldn't get the text to reflect a field in the data. 

I then resorted to discrete stops, using several labelClasses with where clauses, but I'm having trouble figuring out how to work with timestamps within those where clauses (how to get the current time, how to convert a field of my csv to date to compare it against, how to then subtract the two and compare the difference to some threshold)

0 Kudos
1 Reply
Noah-Sager
Esri Regular Contributor

I think you're on the right track with the multiple LabelClass and where clauses. This is the approach we use in this sample: Add multiple label classes to a layer

The tricky part will be the date conversions and comparisons. I think you could make use of some native JavaScript date/time functions, e.g. JavaScript getTime() Method 

0 Kudos