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));
Solved! Go to Solution.
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))
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?
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.
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...
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))
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?
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.
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!
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.
Ah cool, I've updated my code. Would you be able to mark it as an accepted solution?
Done! Sorry about that!!