Select to view content in your preferred language

Use a data expression to create a new field based on values in another field for use in a pie chart

81
5
Jump to solution
Monday
Labels (1)
Bec
by
Regular Contributor

Hi!  I'm trying to build a data expression in an ArcGIS Online dashboard that will take a field called, HOUR_OF_CRASH, which is an integer field and not a time field, and populate a new field called, TOD, which will be a text field for the time of day the hour falls within so that the new field can be used to build a pie chart within the dashboard.  The current data expression is shown below.

var portal = Portal("https://www.arcgis.com");

var fs = FeatureSetByPortalItem('d651aa4112e5408ea9a1cfabecd6485c', 23)
// Create schema for the returned FeatureSet
var fields = [
    { name: "HOUR_OF_CRASH", type: "esriFieldTypeInteger" },
    { name: "TOD", type: "esriFieldTypeString" }
];

var features = [];

for (var f in fs) {
    var hour = f.HOUR_OF_CRASH;
    var tod;

    if (hour >= 0 && hour <= 5){
        tod = "Night";
    } else if (hour >= 6 && hour <= 8){
        tod = "Morning";
    } else if (hour >= 9 && hour <= 15){
        tod = "Day";
    } else if (hour >= 16 && hour <= 23){
        tod = "Evening";
    } else {
        tod = null;
    }

    Push(features, {
        attributes: {
            HOUR_OF_CRASH: hour,
            TOD: tod
        }
    });
}

Unfortunately, this data expression won't function within the dashboard and doesn't give me quite the output I am looking for in the end.  See the below image of the output.

TOD Data Expression Output.png

 My experience with data expression is relatively limited.  Any advice would be greatly appreciated!

 

Bec

0 Kudos
1 Solution

Accepted Solutions
RPGIS
by MVP Regular Contributor
MVP Regular Contributor

Hi @Bec,

So the issue is that you are not returning a feature. When creating a custom data expression you should return a value similar to the one below.

var portal = Portal("https://www.arcgis.com");

var fs = FeatureSetByPortalItem('d651aa4112e5408ea9a1cfabecd6485c', 23)
// Create schema for the returned FeatureSet
var fields = [
    { name: "HOUR_OF_CRASH", type: "esriFieldTypeInteger" },
    { name: "TOD", type: "esriFieldTypeString" }
];

var features = []; 
for (var f in fs) {
    var hour = f.HOUR_OF_CRASH;
    var tod = When( hour >= 0 && hour <= 5, "Night", hour >= 6 && hour <= 8, "Morning", hour >= 9 && hour <= 15, "Day", 16 && hour <= 23, "Evening", Null )
	Push( features, { attributes: { HOUR_OF_CRASH: hour, TOD: tod } })
	}
return FeatureSet( fields, Values, geometry:'' )

View solution in original post

5 Replies
RPGIS
by MVP Regular Contributor
MVP Regular Contributor

Hi @Bec,

So the issue is that you are not returning a feature. When creating a custom data expression you should return a value similar to the one below.

var portal = Portal("https://www.arcgis.com");

var fs = FeatureSetByPortalItem('d651aa4112e5408ea9a1cfabecd6485c', 23)
// Create schema for the returned FeatureSet
var fields = [
    { name: "HOUR_OF_CRASH", type: "esriFieldTypeInteger" },
    { name: "TOD", type: "esriFieldTypeString" }
];

var features = []; 
for (var f in fs) {
    var hour = f.HOUR_OF_CRASH;
    var tod = When( hour >= 0 && hour <= 5, "Night", hour >= 6 && hour <= 8, "Morning", hour >= 9 && hour <= 15, "Day", 16 && hour <= 23, "Evening", Null )
	Push( features, { attributes: { HOUR_OF_CRASH: hour, TOD: tod } })
	}
return FeatureSet( fields, Values, geometry:'' )
KenBuja
MVP Esteemed Contributor

Also note that you can make the When statement more concise

var tod = When(hour >= 0 && hour <= 5, "Night",
               hour <= 8, "Morning",
               hour <= 15, "Day",
               hour <= 23, "Evening",
               Null
);
Bec
by
Regular Contributor

Also, very good input from you as well Ken!  Thank you for sharing your knowledge and experience.

0 Kudos
Bec
by
Regular Contributor

Yes, RPGIS, your input about returning a FeatureSet was valuable in helping me get to my final output.  I made a few more adjustments to the final data expression and what you see below is the working version along with an image of the output.

 

/// Connect to portal layer
var portal = Portal("https://www.arcgis.com");
var fs = FeatureSetByPortalItem(
    portal,
    "d651aa4112e5408ea9a1cfabecd6485c",
    23
);
// Portal layer is a proxy to REST Service layer called CoC Crash Data
// If REST Service layer attributes change may affect data expression

// --- Define Output Schema ---
var fields = [
    { name: "HOUR_OF_CRASH", type: "esriFieldTypeInteger" },
    { name: "TOD", type: "esriFieldTypeString" },
    { name: "SEVERITY_BY_TYPE_CD", type: "esriFieldTypeString" },
    { name: "CRASH_TYPE_CD", type: "esriFieldTypeString" },
    { name: "DAY_IN_WEEK_CD", type: "esriFieldTypeString" },
    { name: "Month", type: "esriFieldTypeDouble" },
    { name: "HIN", type: "esriFieldTypeSmallInteger" },
    { name: "ODPS_SPEED_IND", type: "esriFieldTypeString" },
    { name: "FUNCTIONAL_CLASS_CD", type: "esriFieldTypeString" },
    { name: "ODOT_LANES_NBR", type: "esriFieldTypeDouble" },
    { name: "ODOT_YOUNG_DRIVER_IND", type: "esriFieldTypeString" },
    { name: "ODPS_SENIOR_DRIVER_IND", type: "esriFieldTypeString" },
    { name: "DISTRACTED_DRIVER_IND", type: "esriFieldTypeString" },
    { name: "WEATHER_COND_CD", type: "esriFieldTypeString" },
    { name: "LIGHT_COND_PRIMARY_CD", type: "esriFieldTypeString" },
    { name: "ROAD_COND_PRIMARY_CD", type: "esriFieldTypeString" },
    { name: "ODOT_CRASH_LOCATION_CD", type: "esriFieldTypeString" }
];

var features = [];

// --- Build Output Records ---
for (var f in fs) {

    var hour = f.HOUR_OF_CRASH;

    var tod = When(
        hour >= 0 && hour <= 5, "Night",
        hour >= 6 && hour <= 8, "Morning",
        hour >= 9 && hour <= 15, "Day",
        hour >= 16 && hour <= 23, "Evening",
        null
    );

    Push(features, {
        attributes: {
            HOUR_OF_CRASH: hour,
            TOD: tod,
            SEVERITY_BY_TYPE_CD: f.SEVERITY_BY_TYPE_CD,
            CRASH_TYPE_CD: f.CRASH_TYPE_CD,
            DAY_IN_WEEK_CD: f.DAY_IN_WEEK_CD,
            Month: f.Month,
            HIN: f.HIN,
            ODPS_SPEED_IND: f.ODPS_SPEED_IND,
            FUNCTIONAL_CLASS_CD: f.FUNCTIONAL_CLASS_CD,
            ODOT_LANES_NBR: f.ODOT_LANES_NBR,
            ODOT_YOUNG_DRIVER_IND: f.ODOT_YOUNG_DRIVER_IND,
            ODPS_SENIOR_DRIVER_IND: f.ODPS_SENIOR_DRIVER_IND,
            DISTRACTED_DRIVER_IND: f.DISTRACTED_DRIVER_IND,
            WEATHER_COND_CD: f.WEATHER_COND_CD,
            LIGHT_COND_PRIMARY_CD: f.LIGHT_COND_PRIMARY_CD,
            ROAD_COND_PRIMARY_CD: f.ROAD_COND_PRIMARY_CD,
            ODOT_CRASH_LOCATION_CD: f.ODOT_CRASH_LOCATION_CD
        }
    });
}

// --- Return FeatureSet (non-spatial) ---
return FeatureSet({
    fields: fields,
    features: features
});

 TOD Working Data Expression Output.png

0 Kudos
RPGIS
by MVP Regular Contributor
MVP Regular Contributor

You could go a more programmatic route.

var fs = FeatureSetByPortalItem('d651aa4112e5408ea9a1cfabecd6485c', 23)
// Create schema for the returned FeatureSet
var HRC = "HOUR_OF_CRASH" ; var TODn = "TOD"
var FDict = Dictionary( HRC, "esriFieldTypeInteger", TODn, "esriFieldTypeString" )
var fields = [
    { name: HRC, type: FDict[HRC] },
    { name: TODn, type: FDict[TODn] }
];

var features = [];
for (var f in fs) {
    FDict[HRC] = f.[HRC]
    FDict[TODn] = When( hour >= 0 && hour <= 5, "Night", hour <= 8, "Morning", hour <= 15, "Day", hour <= 23, "Evening", Null )
 	Push( features, { attributes: FDict })
 	}
return FeatureSet( fields:fields, features:features, geometry:'' )
0 Kudos