Select to view content in your preferred language

Onlinemap date difference and color return

532
6
09-12-2022 11:04 AM
CCWeedcontrol
Regular Contributor

I been trying to start this basic Arcade expression, well I thought it was basic. I want to display the difference between two date fields and if the number of days are less then 30 display the number of days in blue, if the number of days are over 30 days display the number of days in red. if there is no date in the "PI_DecisionDate1" then return "In Progress".

Here is what I have started, both fields are date fields

 

 

// Write a script to return a value to show in the pop-up.
// For example, get the average of 4 fields:
// Average($feature.SalesQ1, $feature.SalesQ2, $feature.SalesQ3, $feature.SalesQ4)

var startDate = Date($feature["Received_date"])
var endDate = Date($feature["PI_DecisionDate1"])
var age = DateDiff(endDate, startDate, 'days');
return "response time" + " " +age + " " + "Days";

//Less than
IF(age <= 30){
    return age + "#2986cc"
}
//Greater than
IF (age >= 31){
    return age + "D21C06"
}
// else return
// else{
//  return "IN PROGRESS" "F9905D"   
//}

 

 

 

result with the above, but I don't see the days populated when I click on the features on the map. Also, the formula doesn't see right from what I can tell.

Result Value

Resultresponse time 43 Days
TypeString
0 Kudos
6 Replies
jcarlson
MVP Esteemed Contributor

The biggest problem here is line 8, in which you have a return. This isn't the same as print() in python; once you hit "return", the expression ends.

How is your popup configured, exactly? Having two strings in your return statement isn't right, in line line 20, and your other returns in the conditional statements will just attempt to join the number to the hex code. I don't see how the hex code is actually being used.

If your popup used an Arcade object, you can use something like this:

var startDate = Date($feature["Received_date"])
var endDate = Date($feature["PI_DecisionDate1"])

var age = DateDiff(endDate, startDate, 'days')

var age_str = Iif(
    !IsEmpty(endDate),
    `Response time: ${Text(age)} Days`,
    'IN PROGRESS'
)

var age_color = When(
    age <= 30, "#2986cc",
    age > 30), "#d21c06",
    "#f9905d"
)

return { 
    type : 'text', 
    text : `<font color=${age_color}>${age_str}`
}

 

- Josh Carlson
Kendall County GIS
0 Kudos
CCWeedcontrol
Regular Contributor

All I have configured on my popup is {expression/expr1}. Not sure how to set it up, I've been looking for some video or blogs but I haven't' found one that explains how to configure the pop with the arcade expression. I was trying to originally wanted to display this in a Dashboard, to get an average of all, but couldn't figure out how so I thought I would try this on the map.

 

I was able to get the popup to work with the following

 

 

var startDate = Date($feature["Received_date"])
var endDate = Date($feature["PI_DecisionDate1"])
var age = DateDiff(endDate, startDate, 'days');
return "response time" + " " +age + " " + "Days";

 

 

 

But just a simple response time "10" days.

 

 

With yours

 

 

var startDate = Date($feature["Received_date"])
var endDate = Date($feature["PI_DecisionDate1"])

var age = DateDiff(endDate, startDate, 'days')

var age_str = Iif(
    !IsEmpty(endDate),
    `Response time: ${Text(age)} Days`,
    'IN PROGRESS'
)

var age_color = When(
    age <= 30, "#2986cc",
    age > 30, "#d21c06",
    "#f9905d"
)

return { 
    type : 'text', 
    text : `<font color=${age_color}>${age_str}`
}

 

 

 

I get the following when I hit the Test button.

Result Value

Result
text<font color=#2986cc>Response time: 22 Days
typetext
TypeDictionary

 

Map popup display.

 

[object Object]

0 Kudos
jcarlson
MVP Esteemed Contributor

I suppose I should have asked: is this in the Map Viewer or the Map Viewer Classic?

- Josh Carlson
Kendall County GIS
0 Kudos
CCWeedcontrol
Regular Contributor

In Map View Classic.

0 Kudos
jcarlson
MVP Esteemed Contributor

Ah, okay. There's no way to do this with a single expression, then. You'll need one expression for the text, and a separate expression for the hex code.

Then set the popup to a custom text format display, switch the display to "view HTML source", and enter the text:

<font color={expression/expr1}>{expression/expr0}</font>

That should do it.

Any particular reason to use the classic map viewer?

- Josh Carlson
Kendall County GIS
0 Kudos
CCWeedcontrol
Regular Contributor

I made some changes to my arcade expressions and was able to get to work like I wanted to with the color but the issue I am having now is that if the "days" are "0", what I have as return"IN PROGRESS" doesn't work/popup. how can I get to return ""IN PROGRESS" if the there is zero days?

 

Expression 1

var startDate = Date($feature["Received_date"])
var endDate = Date($feature["PI_DecisionDate1"])
var age = DateDiff(endDate, startDate, 'days');
// return "Response time:" + " " + age + "days";

if (age >= 0){
return "Response time:" + " " + age + "days";
}

else if (age <= 0){
return "IN PROGRESS";
}

 

Expression 2

var days = Round(DateDiff($feature["PI_DecisionDate1"], $feature["Received_date"], 'days'),0);

var result = ""
if (days <= 31){
//Less than
result = "#0a75ad";
//Greater
} else if (days >= 31){
result = "#fe015a";

}
return result;

 

HTML

<p style="color:{expression/expr2};"><font size="4">{expression/expr1}</font></p>‍

0 Kudos