Create chart to tally same value across multiple fields in the same feature layer

248
1
10-13-2021 02:40 PM
Labels (1)
NikeshPatel
New Contributor

I'm really trying to display a line chart in our Arcgis Dashboard that utilizes an expression counting the same value from multiple fields. 

Submitter    Choice 1    Choice 2    Choice 3
John            1            5           6
Amy             5            2           8
Alex            6            4           5

So, Ideally I would like visualize 5 has the highest count currently as users submit their surveys.

I was trying to refer to the samples (https://arcg.is/38SEWWz), but I'm still left a little stumped).

 

 

Struggling for ideas

0 Kudos
1 Reply
jcarlson
MVP Notable Contributor

Can you share the code you've got so far? If you've already found the Data Expression samples, plugging your data into the right one is not terribly difficult. But yours is a bit different from the samples.

It's hard to say without having access to the data, but I think you'll probably want to:

  1. Use GroupBy three times, once for each choice field
    1. Group by the value in the choice field, returning the count of features in the group as well
  2. Create a list of dicts, each with a "choice value" and "count"
  3. Iterate over each grouped featureset and add the counts to the appropriate item in your list.
  4. Create a FeatureSet using the list of dicts.

Any chance the layer is public? It'd be more helpful to be able to test this against the same data.

var fs = FeatureSetByPortalItem(Portal('your-portal-url'), 'itemid', layerIndex, ["choice1", "choice2", "choice3"], false)

var choices = {
    "1": 0,
    "2": 0,
    // And so on, until there is an item for every possible choice.
}

// Group by choices and get counts
var g1 = GroupBy(fs, {name: 'choice', expression: 'choice1'}, {name: 'c_count', expression: '1', statistic: 'COUNT'})
var g2 = GroupBy(fs, {name: 'choice', expression: 'choice2'}, {name: 'c_count', expression: '1', statistic: 'COUNT'})
var g3 = GroupBy(fs, {name: 'choice', expression: 'choice3'}, {name: 'c_count', expression: '1', statistic: 'COUNT'})

// Iterate over grouped FeatureSets, populate dict
for(var g in g1){
    choices[g["choice"]] += g["c_count"]
}

for(var g in g12){
    choices[g["choice"]] += g["c_count"]
}

for(var g in g3){
    choices[g["choice"]] += g["c_count"]
}

// Create array of features from dict
var feats = []

// Populate feature array from dict
for(var c in choices){
    var feat = {"attributes": {"choice": c}, {"c_count": choices[c]}}
    Push(feats, feat)

// Create FeatureSet from JSON, using array of features
          
var outDict= { 
    'fields': [{'name':'choice', 'type':'esriFieldTypeString'},
               {'name':'c_count', 'type':'esriFieldTypeInteger'}], 
    'geometryType': '', 
    'features': feats
    }; 

return FeatureSet(Text(outDict)); 

 You could probably refine that code with a custom function, too, but that could be a decent starting point for you.

- Josh Carlson
Kendall County GIS
0 Kudos