Hi Everyone-
We are trying to display the average flight time for our drone program inside of an indicator. We have two fields, startTime and endTime. We could easily make this calculation in a new field but when we get the data pushed to AGOL via a third party, all of the data is overwritten. Since the fields stay the same, if we are able to calculate this in an indicator that would be helpful.
I get the script to work but only when I use a Feature value type. When I choose a statistic value type the indicator widget hides my date fields.
In summary, I need to calculate the difference in time for each row and then average them all in the indicator.
I sort of went off the suggestions here but they focus on using this in a list instead of an indicator. However, it is important to note that the article says we could do the same thing in an indicator!
Here is the code I have:
(Start Time and End Time are Both Date Field Types mm/dd/yyy, h:mm)
var startDate = Date($datapoint.START_TIME)
var endDate = Date($datapoint.END_TIME)
var flightTime= DateDiff(endDate, startDate, 'minutes')/60
if (!isEmpty($datapoint.START_TIME) && !isEmpty($datapoint.END_TIME))
return flightTime
return {
//textColor:'',
//backgroundColor:'',
//topText: '',
//topTextColor: '',
//topTextOutlineColor: '',
//topTextMaxSize: 'medium',
middleText:'',
middleTextColor: '',
middleTextOutlineColor: '',
middleTextMaxSize: 'large',
//bottomText: '',
//bottomTextColor: '',
//bottomTextOutlineColor: '',
//bottomTextMaxSize: 'medium',
//iconName:'',
//iconAlign:'left',
//iconColor:'',
//iconOutlineColor:'',
//noValue:false,
attributes: {
flightTime:flightTime
},
}
Solved! Go to Solution.
If you want to get the average time for all the flights, then you'll want to create a FeatureSet to be used in the indicator
var p = Portal("https://arcgis.com");
var fs = FeatureSetByPortalItem(p, 'itemId', 0);
var cumulative_flight_time = 0;
var counter = 0;
for (var f in fs) {
var startDate = Date(f.START_TIME)
var endDate = Date(f.END_TIME)
var flightTime = DateDiff(endDate, startDate, 'minutes')/60
cumulative_flight_time += flightTime;
counter++;
}
var theDict = {
'fields': [{ 'name': 'ave_flight_time', 'type': esriFieldTypeDouble'}],
'geometryType': '',
'features': [
{
'attributes': {
'ave_flight_time': cumulative_flight_time / counter
}
}
]
};
return FeatureSet(theDict);
If you want to get the average time for all the flights, then you'll want to create a FeatureSet to be used in the indicator
var p = Portal("https://arcgis.com");
var fs = FeatureSetByPortalItem(p, 'itemId', 0);
var cumulative_flight_time = 0;
var counter = 0;
for (var f in fs) {
var startDate = Date(f.START_TIME)
var endDate = Date(f.END_TIME)
var flightTime = DateDiff(endDate, startDate, 'minutes')/60
cumulative_flight_time += flightTime;
counter++;
}
var theDict = {
'fields': [{ 'name': 'ave_flight_time', 'type': esriFieldTypeDouble'}],
'geometryType': '',
'features': [
{
'attributes': {
'ave_flight_time': cumulative_flight_time / counter
}
}
]
};
return FeatureSet(theDict);
This is great, thanks! I am assuming I would call on this feature set in the template "middleText"? I understand what your code is doing (extremely helpful). I guess I am trying to figure out what to do with it next!
Very appreciated!
No, this will be the data source of the indicator. In the Settings window, click the Change button for the Layer and create a new data expression.
Awesome @KenBuja! I have added the script you supplied above, I am very grateful. I am getting an error in the second part that I cannot figure out.
I have added our portal and the Feature id, those are the only changes I have made.
Thanks for your help.
Sorry...I left out a quotation mark on line 15
'fields': [{ 'name': 'ave_flight_time', 'type': 'esriFieldTypeDouble'}],
Thanks so much! It worked!