JTedrick-esristaff

Labels for Multiple Choice Questions Using Arcade

Blog Post created by JTedrick-esristaff Employee on Oct 10, 2018

When you have a multiple choice question in a survey, have you wondered why the data in a popup looks different than in the form?

 

When Survey123 publishes a single choice question, by default it takes advantage of geodatabase domains to write the default label into the feature layer as well – this allows the label to appear automatically in popups in the Map Viewer and other applications in your ArcGIS Organization. 

 

With multiple choice questions, this is more difficult.  Survey123 stores multiple choice questions as comma-separated values in a text field.  That means there isn’t a built-in way to view labels for these questions. 

 

However, Arcade can be used to take the values stored and substitute appropriate labels.  Arcade is a cross-platform expression language for ArcGIS, and is currently supported in web map pop-ups and ArcGIS Pro.  Previously, Carmel Connolly showed how to use Arcade with ‘other’ choices in questions.  For multiple choice label substitution, a template attribute expression is available at the Esri’s Arcade Expressions GitHub repository; you will need to enter in: 

  • The name of multiple-choice question’s field 
  • The list of values and labels to be substituted 

 

// Replace the field name here 
var fieldName = "like_fruits";
 
// Replace the choices here 
var choices = {}; 
choices['apple'] = 'Apple   '; 
choices['banana'] = 'Banana   '; 
choices['grapes'] = 'Grapes   '; 
choices['kiwi'] = 'Kiwi   '; 
choices['orange'] = 'Orange   '; 
choices['peach'] = 'Peach   '; 
choices['strawberry'] = 'Strawberry   '; 

function formatLabels(inText) { 
    return Concatenate("āœ“", inText); 
} 

function formatMulti(inField, d) { 
    var values = Split(inField, ','); 
    var labels = []; 
    for (var i in values){ 
        labels[i] = formatLabels(d[values[i]]); 
    } 
    var outText = Concatenate(labels, TextFormatting.NewLine); 
    return outText; 
} 

formatMulti($feature[fieldName], choices);


You can see this expression in this webmap.

Outcomes