Arcade Expression for Labels

296
2
Jump to solution
11-27-2023 02:06 PM
JohnMax
New Contributor III

I am trying to get a field value from a feature using Arcade and pass it to a label for a chart in a popup.

I create the expression outside the JavaScript to get the field value:

<script type="text/arcgis-arcade" id="field-name">
  var label = $feature.Field2;
  return {label};
</script>

 Then I reference the script:

var arcadeFields = document.getElementById("field-name").text;

Finally, I use the variable in the label property of the popupTemplate:

popupTemplate: {
title: "Layer1",
fieldInfos: [{
fieldName: "Field18",
label: "Field 18 label"
},
{
fieldName: "Field25",
label: arcadeFields
}
],

It is almost working as intended, only the label for the chart shows the Arcade expression shown below, but not the actual field value.

Untitled.jpg

How do I get the Arcade script to return the field value and not the expression? 

Here's a codepen of it in action if you click on one of the parcels with a value. 

Thanks in advance.

 

0 Kudos
1 Solution

Accepted Solutions
KristianEkenes
Esri Regular Contributor

fieldInfo.label doesn't take Arcade expressions. Arcade can only be defined in 2 places inside popups: expressionInfos, and ExpressionContent elements. To accomplish what you're attempting, you need an ExpressionContent element. This gives you full control over popup elements all within the scope of an Arcade expression. The expression itself needs to return the content type you want...in this case a chart. That chart's labels and values can be calculated using any Arcade function within the expression. 

Here's an example of the expression:

var label = Text($feature.Field2);
var attributes = {
  "Field 18 label": $feature.Field18
};
attributes[label] = $feature.Field25;

var fields = [];

for (var a in attributes){
  Push(fields, a);
}

return {
  type: "media",
  attributes,
  mediaInfos: [{
    type: "piechart",
    title: "Count by type",
    value: {
      fields
    }
  }]
};

And how you would reference it in your app: https://codepen.io/kekenes/pen/rNPqymw?editors=1000

View solution in original post

2 Replies
KristianEkenes
Esri Regular Contributor

fieldInfo.label doesn't take Arcade expressions. Arcade can only be defined in 2 places inside popups: expressionInfos, and ExpressionContent elements. To accomplish what you're attempting, you need an ExpressionContent element. This gives you full control over popup elements all within the scope of an Arcade expression. The expression itself needs to return the content type you want...in this case a chart. That chart's labels and values can be calculated using any Arcade function within the expression. 

Here's an example of the expression:

var label = Text($feature.Field2);
var attributes = {
  "Field 18 label": $feature.Field18
};
attributes[label] = $feature.Field25;

var fields = [];

for (var a in attributes){
  Push(fields, a);
}

return {
  type: "media",
  attributes,
  mediaInfos: [{
    type: "piechart",
    title: "Count by type",
    value: {
      fields
    }
  }]
};

And how you would reference it in your app: https://codepen.io/kekenes/pen/rNPqymw?editors=1000

JohnMax
New Contributor III

Brilliant!  Thanks so much for the help.  I could never have figured this out on my own.

0 Kudos