Select to view content in your preferred language

Creating a Serial Chart in Dashboard using Arcade 'count' on multiple fields

482
3
Jump to solution
08-06-2024 04:34 AM
ttw_marine
Emerging Contributor

I want to create a Serial Chart for a Dashboard where each column corresponds to a field in a single feature class. I think this needs to be done with Arcade. I've been trying to write something but not very familiar with Arcade. Does anyone know what the code might look like?

There are 4 fields, I would like each to be a bar on the chart.

Each field's entries are equal to either "YES" or "NO".

For each bar & field, I would like a count of all the "YES" entries and all the "NO" entries.

I would like this represented as a stacked bar for each field.

I have included an image giving a very rough example. Any ideas very welcome, thanks!

1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

Yes, unfortunately a chart's "fields" option only works for numeric fields. Summing multiple fields is easy enough in a single GroupBy expression. But getting the yes and no sums to stack, that's another thing.

jcarlson_0-1722953724844.png

You still don't need to do a loop, but GroupBy only gets us 95% of the way there. Taken those totals and building our own FeatureSet does the trick.

var fs = FeatureSetByPortalItem(
  Portal('your portal url'),
  'itemid of service',
  0, // layer index
  ['A', 'B', 'C', 'D'],
  false
)

var g = First(GroupBy(
    fs,
    {name: 'result', expression: '1'},
    [
        {name: 'AY', expression: "CASE WHEN A = 'Y' THEN 1 ELSE 0 END", statistic: 'SUM'},
        {name: 'AN', expression: "CASE WHEN A = 'N' THEN 1 ELSE 0 END", statistic: 'SUM'},
        {name: 'BY', expression: "CASE WHEN B = 'Y' THEN 1 ELSE 0 END", statistic: 'SUM'},
        {name: 'BN', expression: "CASE WHEN B = 'N' THEN 1 ELSE 0 END", statistic: 'SUM'},
        {name: 'CY', expression: "CASE WHEN C = 'Y' THEN 1 ELSE 0 END", statistic: 'SUM'},
        {name: 'CN', expression: "CASE WHEN C = 'N' THEN 1 ELSE 0 END", statistic: 'SUM'},
        {name: 'DY', expression: "CASE WHEN D = 'Y' THEN 1 ELSE 0 END", statistic: 'SUM'},
        {name: 'DN', expression: "CASE WHEN D = 'N' THEN 1 ELSE 0 END", statistic: 'SUM'},
    ]
))

return FeatureSet(Text({
    fields: [
        {name: 'letter', type: 'esriFieldTypeString'},
        {name: 'response', type: 'esriFieldTypeString'},
        {name: 'count', type: 'esriFieldTypeInteger'},
    ],
    geometryType: '',
    features: [
        {attributes: {letter: 'A', response: 'Y', count: g['AY']}},
        {attributes: {letter: 'A', response: 'N', count: g['AN']}},
        {attributes: {letter: 'B', response: 'Y', count: g['BY']}},
        {attributes: {letter: 'B', response: 'N', count: g['BN']}},
        {attributes: {letter: 'C', response: 'Y', count: g['CY']}},
        {attributes: {letter: 'C', response: 'N', count: g['CN']}},
        {attributes: {letter: 'D', response: 'Y', count: g['DY']}},
        {attributes: {letter: 'D', response: 'N', count: g['DN']}},
    ]
}))

jcarlson_1-1722954766457.png

 

- Josh Carlson
Kendall County GIS

View solution in original post

0 Kudos
3 Replies
jcarlson
MVP Esteemed Contributor

Yes, unfortunately a chart's "fields" option only works for numeric fields. Summing multiple fields is easy enough in a single GroupBy expression. But getting the yes and no sums to stack, that's another thing.

jcarlson_0-1722953724844.png

You still don't need to do a loop, but GroupBy only gets us 95% of the way there. Taken those totals and building our own FeatureSet does the trick.

var fs = FeatureSetByPortalItem(
  Portal('your portal url'),
  'itemid of service',
  0, // layer index
  ['A', 'B', 'C', 'D'],
  false
)

var g = First(GroupBy(
    fs,
    {name: 'result', expression: '1'},
    [
        {name: 'AY', expression: "CASE WHEN A = 'Y' THEN 1 ELSE 0 END", statistic: 'SUM'},
        {name: 'AN', expression: "CASE WHEN A = 'N' THEN 1 ELSE 0 END", statistic: 'SUM'},
        {name: 'BY', expression: "CASE WHEN B = 'Y' THEN 1 ELSE 0 END", statistic: 'SUM'},
        {name: 'BN', expression: "CASE WHEN B = 'N' THEN 1 ELSE 0 END", statistic: 'SUM'},
        {name: 'CY', expression: "CASE WHEN C = 'Y' THEN 1 ELSE 0 END", statistic: 'SUM'},
        {name: 'CN', expression: "CASE WHEN C = 'N' THEN 1 ELSE 0 END", statistic: 'SUM'},
        {name: 'DY', expression: "CASE WHEN D = 'Y' THEN 1 ELSE 0 END", statistic: 'SUM'},
        {name: 'DN', expression: "CASE WHEN D = 'N' THEN 1 ELSE 0 END", statistic: 'SUM'},
    ]
))

return FeatureSet(Text({
    fields: [
        {name: 'letter', type: 'esriFieldTypeString'},
        {name: 'response', type: 'esriFieldTypeString'},
        {name: 'count', type: 'esriFieldTypeInteger'},
    ],
    geometryType: '',
    features: [
        {attributes: {letter: 'A', response: 'Y', count: g['AY']}},
        {attributes: {letter: 'A', response: 'N', count: g['AN']}},
        {attributes: {letter: 'B', response: 'Y', count: g['BY']}},
        {attributes: {letter: 'B', response: 'N', count: g['BN']}},
        {attributes: {letter: 'C', response: 'Y', count: g['CY']}},
        {attributes: {letter: 'C', response: 'N', count: g['CN']}},
        {attributes: {letter: 'D', response: 'Y', count: g['DY']}},
        {attributes: {letter: 'D', response: 'N', count: g['DN']}},
    ]
}))

jcarlson_1-1722954766457.png

 

- Josh Carlson
Kendall County GIS
0 Kudos
ttw_marine
Emerging Contributor

Thank you Josh this looks great! Really appreciate your help with this.

I've put in this code and substituted the actual layer & field names, but when I run the script I get the following error: "Test execution error: Invalid variable assignment.. Verify test data.". Not sure if I've inputted anything in wrong, or perhaps its an issue with the 'portal' URL?

ttw_marine_1-1723562531206.png

 

 

0 Kudos
ttw_marine
Emerging Contributor

Just tried it again this morning and it's working! Exactly what I was after, thanks Josh!

0 Kudos