Skip to content

Instantly share code, notes, and snippets.

@apfister
Created February 15, 2022 21:12
Show Gist options
  • Save apfister/ed798570d239d440edc9aff3e95dd6f1 to your computer and use it in GitHub Desktop.
Save apfister/ed798570d239d440edc9aff3e95dd6f1 to your computer and use it in GitHub Desktop.
// item id of the first layer you want to use
var itemId1 = '67959bfbb58e400d80a2dc1be3542338';
// item id of the second layer you want to use
var itemId2 = '22a65ba4870a41a69ed0d8d843480d3c';
var layerId = 0;
var portalUrl = 'https://www.arcgis.com';
var fieldList = ['time_period', 'obs_value'];
// get a FeatureSet Item for the first layer
var fs1 = FeatureSetByPortalItem(Portal(portalUrl), itemId1, layerId, fieldList);
// get a FeatureSet Item for the second layer
var fs2 = FeatureSetByPortalItem(Portal(portalUrl), itemId2, layerId, fieldList);
// group the first FeatureSet by the `time_period` field and average the values
var fs1Grouped = GroupBy(fs1, ['time_period'], [
{ name: 'B15_ObsValueAvg', expression: 'obs_value', statistic: 'avg' }
]);
// group the second FeatureSet by the `time_period` field and average the values
var fs2Grouped = GroupBy(fs2, ['time_period'], [
{ name: 'B18_ObsValueAvg', expression: 'obs_value', statistic: 'avg' }
]);
// create an empty FeatureSet that we will populate and return to use in the chart
var outputFs = {
'geometryType': '',
'features': [],
'fields': [
{ name: 'TIME_PERIOD', type: 'esriFieldTypeInteger'},
{ name: 'OBS_VALUE_B15', type: 'esriFieldTypeDouble' },
{ name: 'OBS_VALUE_B18', type: 'esriFieldTypeDouble' }
]
};
// an empty array to hold the combination of the grouped features from the first and second layer
var features = [];
// loop through each feature in the first FeatureSet and add to the `features` array
for (var item in fs1grouped) {
var rounded = Round(item.B15_ObsValueAvg, 2);
Push(features, {
attributes: {
OBS_VALUE_B15: rounded,
TIME_PERIOD: item.time_period
}
});
}
// helper function to get the existing feature from the `features` array
function getFeature(inFeatures, lookupField, lookupValue) {
for (var f in inFeatures) {
if (inFeatures[f]['attributes'][lookupField] == lookupValue) {
return inFeatures[f];
}
}
return null;
}
// loop through each feature in the second FeatureSet
// get the existing feature based on its `time_period` value
// add in the value from the second FeatureSet
for (var item in fs2grouped) {
var timePeriod = item.time_period;
var rounded = Round(item.B18_ObsValueAvg, 2)
var f = getFeature(features, 'TIME_PERIOD', timePeriod);
f['attributes']['OBS_VALUE_B18'] = rounded;
}
// assign our empty FeatureSet's `features` property with what we just built
outputFs['features'] = features;
// return a FeatureSet object to use in the Chart configuration
return FeatureSet(Text(outputFs));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment