Select to view content in your preferred language

Data Expression Help in Dashboards

1833
6
Jump to solution
04-24-2023 10:47 AM
Labels (3)
JanelleShank
Emerging Contributor

Good Afternoon All, 

I am new to writing data expression to retrieve data for a hosted table in a dashboard and I am having trouble writing the expression to use in a pie chart on the Dashboard. For example, I need to write and expression that bring in the count of the Current Step field and the Project Name field. Ultimately the goal of the expression is to spring the attributes of the "Current Step' field and while providing a count of those attributes and filtering the counts by the Project Name. Below is my current code. I have been successful in get the Current Count to split and displaying the count of each of the choices but I am struggling to get the Project name to factor into the count. Will I need to write and expression to count the current step by the each individual project?

 

I appreciate any help and suggestions to solve this problem. 

 

var portal = Portal('')
var fs = FeatureSetByPortalItem(
    portal,
    '*',
    0,
    ['Current_Step'],
    false
);

// Create empty array for features and feat object
var features = [];
var feat;

var loc = FeatureSetByPortalItem(
    portal,
    'ddd2e78398c14777b49f902b9b289ada',
    0,
    ['Project_Name'],
    false
);


// Split comma separated hazard types and store in dictionary.  
for (var feature in fs) {
    var split_array  =  Split(feature['Current_Step'], "(")
    var count_arr = Count(split_array)
    for(var i = 0; i < count_arr; i++ ){
        feat = {
            'attributes': {
                'split_choices': Trim(split_array[i])
            }
        }
        Push(features, feat);
}}

// Empty dictionary to capture each hazard reported as separate rows.
var choicesDict = {
    'fields': [
        { 'name': 'split_choices', 'type': 'esriFieldTypeString'}],
    'geometryType': '',
    'features': features
};

// Convert dictionary to featureSet.
var fs_dict = FeatureSet(Text(choicesDict));

// Return featureset after grouping by hazard types.
return GroupBy(fs_dict, ['split_choices'],
       [{ name: 'split_count', expression: 'split_choices', statistic: 'COUNT' }]);
 
Thank you, 
Janelle 
0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

Ah OK. So you want something like this:

// load your featureset
var fs = FeaturesetByPortalItem(Portal("https://arcgis.com"), "b0d335151aad48a5883326b9aed69cdd", 0, ["TextField1", "TextField2"], false)

// define the output featureset
var out_dict = {
  fields: [
    {name: "Project_Name", type: "esriFieldTypeString"},
    {name: "Current_Step", type: "esriFieldTypeString"}
  ],
  features: [],
  geometryType: ""
}

// iterate over the input fs
for(var f in fs) {
  // split the Current_Steps field
  var project_name = f.TextField1
  var steps = Split(f.TextField2, ",")
  // iterate over the split values
  for(var s in steps) {
    // create a new feature in the output featureset
    var new_f = {attributes: {Project_Name: project_name, Current_Step: Trim(steps[s])}}
    Push(out_dict.features, new_f)
  }
}
Console(out_dict)
// return the featureset
return Featureset(Text(out_dict))

 

Change the field names in lines 1, 17, 18. I used "TextField1" for the project name and "TextField2" for the current step.

 

You can use that expression to generate a featureset with the project name and the split values.

You can then input that fs into the pie chart widget:

JohannesLindner_0-1682414299604.png

 

 

Add a side or top bar widget. In there, you can add a category selector. Point it to the data expression and use the project name column as group field:

JohannesLindner_1-1682414331784.png

 

Set the category selector to filter the pie chart:

JohannesLindner_2-1682414379510.png

 

 

This is a quick example. The table on the left is my input featureset. The table on the right is the output of the data expression. The pie chart uses the same data expression.

JohannesLindner_3-1682414604976.png

 

When I select a project, each widget gets filtered to show only features that belong to that project:

JohannesLindner_4-1682414722400.png

 


Have a great day!
Johannes

View solution in original post

6 Replies
JohannesLindner
MVP Frequent Contributor

To post code:

JohannesLindner_0-1674589814049.pngJohannesLindner_1-1674589827880.png

 

 

I don't really understand what you want to achieve.

You have one featureset with a field "Current_Step", which apparently contains lists of values (you split by "(", shouldn't that be "," ?)

And you have a featureset with a field "Project_Name". How are these two tables related? Do they have a common field? Do they have spatial overlap?


Have a great day!
Johannes
0 Kudos
JanelleShank
Emerging Contributor

So, the "Current Step" and "Project Name" are from the same table. I'm trying to add both to a data expression to add the pie chart but I need to separate the "Current Step" string.

0 Kudos
JohannesLindner
MVP Frequent Contributor

Ah OK. So you want something like this:

// load your featureset
var fs = FeaturesetByPortalItem(Portal("https://arcgis.com"), "b0d335151aad48a5883326b9aed69cdd", 0, ["TextField1", "TextField2"], false)

// define the output featureset
var out_dict = {
  fields: [
    {name: "Project_Name", type: "esriFieldTypeString"},
    {name: "Current_Step", type: "esriFieldTypeString"}
  ],
  features: [],
  geometryType: ""
}

// iterate over the input fs
for(var f in fs) {
  // split the Current_Steps field
  var project_name = f.TextField1
  var steps = Split(f.TextField2, ",")
  // iterate over the split values
  for(var s in steps) {
    // create a new feature in the output featureset
    var new_f = {attributes: {Project_Name: project_name, Current_Step: Trim(steps[s])}}
    Push(out_dict.features, new_f)
  }
}
Console(out_dict)
// return the featureset
return Featureset(Text(out_dict))

 

Change the field names in lines 1, 17, 18. I used "TextField1" for the project name and "TextField2" for the current step.

 

You can use that expression to generate a featureset with the project name and the split values.

You can then input that fs into the pie chart widget:

JohannesLindner_0-1682414299604.png

 

 

Add a side or top bar widget. In there, you can add a category selector. Point it to the data expression and use the project name column as group field:

JohannesLindner_1-1682414331784.png

 

Set the category selector to filter the pie chart:

JohannesLindner_2-1682414379510.png

 

 

This is a quick example. The table on the left is my input featureset. The table on the right is the output of the data expression. The pie chart uses the same data expression.

JohannesLindner_3-1682414604976.png

 

When I select a project, each widget gets filtered to show only features that belong to that project:

JohannesLindner_4-1682414722400.png

 


Have a great day!
Johannes
JanelleShank
Emerging Contributor

My apologies for the late response. I have been away from the office and in the field. I finally got to try your suggestion! It was exactly what I needed! Thank you very much!

 

Have a great day!

Best,

Janelle

0 Kudos
BNix_TPA
Emerging Contributor

I'm working with the same code as above, but need to also add a date field.  I used this code below and it works fine until I add the additional date field.  .When I add the date field it doesn't return any data. Any ideas?

 

// load your featureset
var fs = FeaturesetByPortalItem(Portal("https://arcgis.com"), "c68e6ea9b6b74c30bc94ef901d34f5f2"0, ["type_of_activity""focus_area""date_time"], false)

// define the output featureset
var out_dict = {
  fields: [
    {name"Type_of_Activity"type"esriFieldTypeString"},
    {name"Focus_Area"type"esriFieldTypeString"},
    {name"Date_Time"type"esriFieldTypeDate"}
  ],
  features: [],
  geometryType""
}

// iterate over the input fs
for(var f in fs) {
  // split the Current_Steps field
  var date_time = f.date_time
  var type_of_activity = f.type_of_activity
  var steps = Split(f.focus_area",")
  // iterate over the split values
  for(var s in steps) {
    // create a new feature in the output featureset
    var new_f = {attributes: {Date_Timedate_timeType_of_Activitytype_of_activity,  Focus_AreaTrim(steps[s])}}
    Push(out_dict.featuresnew_f)
  }
}
Console(out_dict)
// return the featureset
return Featureset(Text(out_dict))
0 Kudos
BNix_TPA
Emerging Contributor

Ended up figuring this out.  When working with date fields, they have to be wrapped with the Number function.

var date_time = Number(f.date_time)