Select to view content in your preferred language

Data Expression for Pie Chart in Dashboard Fails

2352
5
Jump to solution
09-07-2021 10:28 AM
Labels (3)
by Anonymous User
Not applicable

I'm creating  a dashboard for road signs.  Each sign has the installation year (4 digit integer).  (The field name is "Installation"). I want to show a pie chart that shows if signs are <10 years old, 11-20 years old, or >20 years old.  

This is my first time trying to do this in a data expression, and I can't get the correct results.  It says all the signs are 11-20 years old.  I also realized the "ageyrs" variable says it's Nan.  So something somewhere isn't calculating right.  Can anyone spot what I need to fix in my code?

 

 

var p = 'https://www.arcgis.com';
var itemId = 'xxx';
var layerId = 0;

var fs = Filter(FeatureSetByPortalItem(Portal(p),itemID,layerID,['Installation']),'Installation > 0');
var ageyrs = Year(Now())-fs
var agechoice = iif(ageyrs>20,'>20 Years Old',(iif(ageyrs<10,'<10 Years Old','11-20 Years Old')))

var Dict = {
    'fields':[
        {'name': 'sign_age', 'type':'esriFieldTypeDouble'},
        {'name': 'agecat', 'type':'esriFieldTypeString'}],
        'geometryType':'',
        'features':
        [{'attributes':
            {'sign_age':ageyrs,
             'agecat':agechoice
        }}]};

return FeatureSet(Text(Dict))

 

@XanderBakker 

0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

I made  a few minor tweaks and got it to work! Thank you so much for you help! I was searching for a quite some time yesterday trying to figure this out.  Below is the updated code.

var p = 'https://www.arcgis.com'; 
var itemId = 'xxx'; 
var layerId = 0; 
var fs = Filter(FeatureSetByPortalItem(Portal(p), itemID, layerID,['Installation']),'Installation > 0'); 

var Dict = {
    'fields':[ 
        {'name': 'sign_age', 'type':'esriFieldTypeDouble'}, 
        {'name': 'agecat', 'type':'esriFieldTypeString'}], 
    'geometryType':'', 
    'features': []};

var index = 0;
for (var f in fs) {
    var insdate = f['Installation'];
    var ageyrs = Year(Now())-insdate;
    var agechoice = iif(ageyrs>20,'>20 Years Old',
                       (iif(ageyrs<10,'<10 Years Old',
                       '11-20 Years Old')));

    Dict.features[index] = {   
            'attributes': {   
                'sign_age': ageyrs,
                'agecat': agechoice
            }}   
    index++; 
};  

return FeatureSet(Text(Dict));

View solution in original post

0 Kudos
5 Replies
XanderBakker
Esri Esteemed Contributor

Hi @Anonymous User ,

One of the issues in your code is that you using "fs" as if it contains the installation date. In this case "fs" is a featureset and you will loop through the features, read out the installation date and and fill the output "Dict" with the values calculated. Have a look at the code below:

var p = 'https://www.arcgis.com'; 
var itemId = 'xxx'; 
var layerId = 0; 
var fs = Filter(FeatureSetByPortalItem(Portal(p), itemID, layerID,['Installation']),'Installation > 0'); 
var ageyrs = Year(Now())-fs;
var agechoice = iif(ageyrs>20,'>20 Years Old',(iif(ageyrs<10,'<10 Years Old','11-20 Years Old')));
var Dict = {
    'fields':[ 
        {'name': 'sign_age', 'type':'esriFieldTypeDouble'}, 
        {'name': 'agecat', 'type':'esriFieldTypeString'}], 
    'geometryType':'', 
    'features': []};

var index = 0;
for (var f in fs) {
    var instalationdate = f['Installation'];
    var ageyrs = DateDiff(Now(), instalationdate, 'years');
    var agechoice = iif(ageyrs>20,'>20 Years Old',
                       (iif(ageyrs<10,'<10 Years Old',
                       '11-20 Years Old')));

    Dict.features[index] = {   
            'attributes': {   
                'sign_age': ageyrs,
                'agecat': agechoice
            }}   
    index++; 
};  

return FeatureSet(Text(Dict));

 

If you just want to return a count per age class you could also create an intermediate dictionary for each class value and update the count. 

by Anonymous User
Not applicable

That definitely helps and makes more sense.  However it still isn't calculating the Sign Age correctly.  Therefore all the signs show up as 11-20yrs old which isn't correct.  Does it matter that the the Installation field is an integer field?

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi @Anonymous User ,

I'm glad it works and as you noticed there is a big difference between an integer and a date. I changed the calculation using DateDiff, but this requires two dates to compare. In your case using an integer your calculation does the job. 

by Anonymous User
Not applicable

Thank you so much for your help!! 

0 Kudos
by Anonymous User
Not applicable

I made  a few minor tweaks and got it to work! Thank you so much for you help! I was searching for a quite some time yesterday trying to figure this out.  Below is the updated code.

var p = 'https://www.arcgis.com'; 
var itemId = 'xxx'; 
var layerId = 0; 
var fs = Filter(FeatureSetByPortalItem(Portal(p), itemID, layerID,['Installation']),'Installation > 0'); 

var Dict = {
    'fields':[ 
        {'name': 'sign_age', 'type':'esriFieldTypeDouble'}, 
        {'name': 'agecat', 'type':'esriFieldTypeString'}], 
    'geometryType':'', 
    'features': []};

var index = 0;
for (var f in fs) {
    var insdate = f['Installation'];
    var ageyrs = Year(Now())-insdate;
    var agechoice = iif(ageyrs>20,'>20 Years Old',
                       (iif(ageyrs<10,'<10 Years Old',
                       '11-20 Years Old')));

    Dict.features[index] = {   
            'attributes': {   
                'sign_age': ageyrs,
                'agecat': agechoice
            }}   
    index++; 
};  

return FeatureSet(Text(Dict));
0 Kudos