Custom Arcade Chart in Popup

163
2
Jump to solution
2 weeks ago
BlakeMorrison
New Contributor III

I'm trying to create a chart that displays in a popup, from features in another layer using the current layer as a filter. I have the following code returning the appropriate FeatureSet filtered and grouped as I would like. But the format appears to be incorrect. I can't work out what the correct format is to have the chart actually display the data in the featureset.

 

 

var state = $feature.AALRegion;

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

var fields = [
    'OBJECTID',
    'Region',
    'gridcode',
    'Hectares'
];

var fs = FeatureSetByPortalItem(
    portal,
    '7369e87b3b104eae8c18507261184083',     // FeatureService ID that you want to grab fields from.
    0,                                      // Sublayer index, usually 0.
    fields,                                 // Array of fields to grab from the FeatureService.
    False                                   // Include geometry?
);

var final = GroupBy(Filter(fs,"Region = @state"),'gridcode',
      {
        name: 'sumhec',
        expression: 'Hectares',
        statistic: 'SUM'
    },
);
    
return final

 

 

BlakeMorrison_0-1714621206915.png

 

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

You'll have to use an Arcade element instead of a chart. This uses the "media" popupElement in the return. Here's an example with your code that creates a basic bar chart

 

var state = $feature.AALRegion;

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

var fields = [
    'OBJECTID',
    'Region',
    'gridcode',
    'Hectares'
];

var fs = FeatureSetByPortalItem(
    portal,
    '7369e87b3b104eae8c18507261184083',     // FeatureService ID that you want to grab fields from.
    0,                                      // Sublayer index, usually 0.
    fields,                                 // Array of fields to grab from the FeatureService.
    False                                   // Include geometry?
);

var final = GroupBy(Filter(fs,"Region = @state"),'gridcode',
      {
        name: 'sumhec',
        expression: 'Hectares',
        statistic: 'SUM'
    },
);
    
var Values = {};
var barNames = [];

for (var i in final) {
  Values[Text(i.gridcode)] = i.sumhec; //you have to use a Text string for the Value keys. It doesn't work with numbers
  Push(barNames, i.gridcode)
}


return {
  type: "media",
  attributes:Values,
  mediaInfos:[{
    type: "barchart",
    title: `Sum of ${state}`,
    value: {
      fields: barNames
    }
  }]
}

 

 

 

View solution in original post

2 Replies
KenBuja
MVP Esteemed Contributor

You'll have to use an Arcade element instead of a chart. This uses the "media" popupElement in the return. Here's an example with your code that creates a basic bar chart

 

var state = $feature.AALRegion;

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

var fields = [
    'OBJECTID',
    'Region',
    'gridcode',
    'Hectares'
];

var fs = FeatureSetByPortalItem(
    portal,
    '7369e87b3b104eae8c18507261184083',     // FeatureService ID that you want to grab fields from.
    0,                                      // Sublayer index, usually 0.
    fields,                                 // Array of fields to grab from the FeatureService.
    False                                   // Include geometry?
);

var final = GroupBy(Filter(fs,"Region = @state"),'gridcode',
      {
        name: 'sumhec',
        expression: 'Hectares',
        statistic: 'SUM'
    },
);
    
var Values = {};
var barNames = [];

for (var i in final) {
  Values[Text(i.gridcode)] = i.sumhec; //you have to use a Text string for the Value keys. It doesn't work with numbers
  Push(barNames, i.gridcode)
}


return {
  type: "media",
  attributes:Values,
  mediaInfos:[{
    type: "barchart",
    title: `Sum of ${state}`,
    value: {
      fields: barNames
    }
  }]
}

 

 

 

BlakeMorrison
New Contributor III

Thanks mate! That works straight away.

 

Over the weekend I realised what I was doing was more suitable for the Arcade element as opposed to the Chart element (which would need multiple data expressions to do what I wanted). Thank you for your perfect answer.

0 Kudos