Select to view content in your preferred language

Dashboard Pie Chart Data Expression using Date Ranges

243
2
Jump to solution
09-05-2024 10:55 AM
Labels (1)
LaineMcGannon
New Contributor

Hi everyone,

I am very new to creating Dashboards and am having trouble writing the correct script for what I need. I need a pie chart to represent the features that have an expiration date (E_Date) within the ranges (categories): the next 30 days, the next 31-60 days, the next 61 to 90 days, and the next 90+ days. I have written a script that accurately pushes out an array of the data. But, when I go to select the data expression layer, it says "Unable to Execute Arcade script" and I cannot move forward with the pie chart. Below is the script I have so far. Any advice is much appreciated. 


var fs = FeatureSetByPortalItem(
Portal('https://gisportal.xyz.com/portal'),
'2917513692574002bfec1932cef87080',
13,
['E_DATE', 'LEASE_STAT'],
false
);

var currentDate = Date();

var within30Days = 0;
var within31to60Days = 0;
var within61to90Days = 0;
var moreThan90Days = 0;

for (var currentFeature in fs) {
if (HasKey(currentFeature, 'E_DATE') && currentFeature.E_DATE != null) {
var expDate = Date(currentFeature.E_DATE);

if (expDate != null && expDate != 'Invalid Date') {

var daysDiff = DateDiff(expDate, currentDate, 'days');

if (daysDiff >= 0 && daysDiff <= 30) {
within30Days += 1;
} else if (daysDiff > 30 && daysDiff <= 60) {
within31to60Days += 1;
} else if (daysDiff > 60 && daysDiff <= 90) {
within61to90Days += 1;
} else if (daysDiff > 90) {
moreThan90Days += 1;
}
}
}
}

var results = [
{ "Category": "Within 30 Days", "Count": within30Days },
{ "Category": "31 to 60 Days", "Count": within31to60Days },
{ "Category": "61 to 90 Days", "Count": within61to90Days },
{ "Category": "More than 90 Days", "Count": moreThan90Days }
];

return results;

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

Something you always have to remember when creating a Data Expression is that it has to return a FeatureSet. You'll have to take the extra steps to do that like this, replacing your last line with this

var Dict = {
    'fields': [
      {'name': 'Category', 'type': 'esriFieldTypeString'},
      {'name': 'Count', 'type': 'esriFieldTypeInteger'}],
    'geometryType': '',   
    'features': []};

for (var i in results){
  Dict.features[i] = {
    'attributes': {
      'Category':results[i].Category,
      'Count': results[i].Count
    }
  }
}

return FeatureSet(Dict)

 

View solution in original post

0 Kudos
2 Replies
KenBuja
MVP Esteemed Contributor

Something you always have to remember when creating a Data Expression is that it has to return a FeatureSet. You'll have to take the extra steps to do that like this, replacing your last line with this

var Dict = {
    'fields': [
      {'name': 'Category', 'type': 'esriFieldTypeString'},
      {'name': 'Count', 'type': 'esriFieldTypeInteger'}],
    'geometryType': '',   
    'features': []};

for (var i in results){
  Dict.features[i] = {
    'attributes': {
      'Category':results[i].Category,
      'Count': results[i].Count
    }
  }
}

return FeatureSet(Dict)

 

0 Kudos
LaineMcGannon
New Contributor

KenBuja, 

First off, thank you so much for your very quick response! 

And, thank you for such a great solution! My feature set functioned properly for the pie chart. I appreciate your help. 

0 Kudos