Select to view content in your preferred language

Display Average Flight Time in Indicator

276
6
Jump to solution
05-09-2024 09:23 AM
Labels (1)
JustinH
New Contributor III

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
  },
}

 

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

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); 

 

View solution in original post

6 Replies
KenBuja
MVP Esteemed Contributor

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); 

 

JustinH
New Contributor III

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! 

0 Kudos
KenBuja
MVP Esteemed Contributor

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.

0 Kudos
JustinH
New Contributor III

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. 

 

JustinH_0-1715614951895.png

Thanks for your help. 

0 Kudos
KenBuja
MVP Esteemed Contributor

Sorry...I left out a quotation mark on line 15

  'fields': [{ 'name': 'ave_flight_time', 'type': 'esriFieldTypeDouble'}], 
0 Kudos
JustinH
New Contributor III

Thanks so much! It worked! 

0 Kudos