Select to view content in your preferred language

Data expression to calculate time difference for Pie Chart in Arcade

1415
9
Jump to solution
09-17-2021 02:17 PM
by Anonymous User
Not applicable

I'm working on creating a pie chart to display the total time by patrol type.  I have a startTime field and an endTime field in my feature service.  I'm displaying it by PatrolType.  I have gotten close but I think I'm getting messed up by the field types for time/minutes, etc. Or maybe somethign else is wrong. I get no results but no errors.    I'm on a steep learning curve so examples have gotten me most of the way.

Would you use the DateDiff to get the time or is there another way?  I was attempting to see how many minutes each patrol was, and then display the total minutes for each patrol type.  And do I then need to then Sum up the elapsed minutes?

var myPortal = 'https://boulder.maps.arcgis.com/';
var patrol = FeatureSetByPortalItem(Portal(myPortal), '2a61f35432e24069bdd1a9102d02f0ba', 0, ["startTime", "endTime", "PatrolType", "created_date"], false)


var Dict = {
'fields' : [
{'name' : 'Patrol Type', 'type': 'esriFieldTypeString'},
{'name' : 'Time Elapse', 'type': 'esriFieldTypeDate'},
],
geometryType: "",
features: [],
};

[{'attributes': {
'Patrol Type' : 'PatrolType',
'Time Elapse': DateDiff("endTime", "startTime", 'minutes')

}}
];
return FeatureSet(Text(Dict));

0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

Unable to test but should be close:

 

var myPortal = 'https://boulder.maps.arcgis.com/';
var patrol = FeatureSetByPortalItem(Portal(myPortal), '2a61f35432e24069bdd1a9102d02f0ba', 0, ["startTime", "endTime", "PatrolType", "created_date"], false)

var Dict = {
  'fields' : [
  {'name' : 'PatrolType', 'type': 'esriFieldTypeString'},
  {'name' : 'TimeElapse', 'type': 'esriFieldTypeDouble'}],
  geometryType: "",
  features: [],
};

var index = 0

for (var feat in patrol) {
  Dict.features[index] = {
    'attributes': {
      'PatrolType' : feat.PatrolType,
      'TimeElapse': DateDiff(feat.endTime, feat.startTime, 'minutes')
    }}
  index++}

return FeatureSet(Text(Dict))

 

View solution in original post

0 Kudos
9 Replies
DavidPike
MVP Frequent Contributor

dateDiff() returns a number (i.e. the difference in minutes as a number), swap out your TimeElapse field type to esriFieldTypeDouble

also just to check that you are iterating through the feature set to populate the dictionary and just haven't shared that part of the code?

by Anonymous User
Not applicable

Thanks David - I think I had tried the double field type also.

But that is a good point, I am not iterating.  that is my entire code and this is what I'm getting for my result.   

KendellRyan1_0-1632147047274.png

I'm confused though b/c this is what I was using this as an example to follow, but this does not iterate??
https://www.esri.com/arcgis-blog/products/ops-dashboard/announcements/introducing-data-expressions-i...

 

0 Kudos
DavidPike
MVP Frequent Contributor

Unable to test but should be close:

 

var myPortal = 'https://boulder.maps.arcgis.com/';
var patrol = FeatureSetByPortalItem(Portal(myPortal), '2a61f35432e24069bdd1a9102d02f0ba', 0, ["startTime", "endTime", "PatrolType", "created_date"], false)

var Dict = {
  'fields' : [
  {'name' : 'PatrolType', 'type': 'esriFieldTypeString'},
  {'name' : 'TimeElapse', 'type': 'esriFieldTypeDouble'}],
  geometryType: "",
  features: [],
};

var index = 0

for (var feat in patrol) {
  Dict.features[index] = {
    'attributes': {
      'PatrolType' : feat.PatrolType,
      'TimeElapse': DateDiff(feat.endTime, feat.startTime, 'minutes')
    }}
  index++}

return FeatureSet(Text(Dict))

 

0 Kudos
by Anonymous User
Not applicable

Thanks David - minor tweak and that worked great, much appreciated!  I'm sure this is a newbie question, but can you tell my why that ESRI example I put the URL for in above didn't iterate? 

0 Kudos
DavidPike
MVP Frequent Contributor

What did you have to tweak ?(I can edit my response just so it doesn't confuse anyone who might possibly use it in the future)

As to why your code was wrong, you need to iterate through the initial feature set and append the values into a the dictionary you created (this is done by the for loop populating the 'features' [] list)

The only code I can see without the for loop is using SUM on the entire feature set (SUM is a function which can take a feature set and sum field as arguments).  What you tried to do with something like'Patrol Type' : 'PatrolType' just doesn't make sense.

 

0 Kudos
Gerhard
Regular Contributor

Couldn't figure out why my DateDiff wasn't working. Saw this comment and realised my dictionary field type was set to esriFieldTypeInteger when it should have been esriFieldTypeDouble. Thanks @DavidPike!

Best,

Gerhard
0 Kudos
by Anonymous User
Not applicable

Very helpful, I'm going to try to apply that same logic to a different problem I'm trying to solve, where my looping is not successsful.

Oh, all I did was just change "fs" to "Patrol" to match my variable.  


DavidPike
MVP Frequent Contributor

Ah cool, I've updated my code.  Would you be able to mark it as an accepted solution?

0 Kudos
by Anonymous User
Not applicable

Done! Sorry about that!!

0 Kudos