Select to view content in your preferred language

Show count of a field with domains, including domains that have no records as 0.

204
1
09-04-2024 01:19 PM
Labels (1)
AvaPatterson
Occasional Contributor

If the survey team is currently out in the field collecting data, is there a way to display the domains as 0 for a pie chart? legend I was trying to create an arcade expression, but the legend still doesn't want to list the domains that have 0. If not in a pie chart, is there another way to display 0 for domains with no records in ArcGIS Dashboards?

// Portal and item/layer IDs
var p = 'https://testurl';
var itemID = 'itemID'; // Item ID for the Water_Meter_v3 layer
var layerID = 0; // Layer index

// Domain values for 'cust_material_type'
var domainValues = [
    { "code": "Lead", "name": "Lead" },
    { "code": "GRR", "name": "Galvanized Steel" },
    { "code": "PVC", "name": "PVC" },
    { "code": "PE", "name": "PE" },
    { "code": "Copper", "name": "Copper" },
    { "code": "Braided Metal", "name": "Braided Metal" },
    { "code": "Unknown", "name": "Unknown to be verified" },
    { "code": "Non-Lead", "name": "Non-Lead" }
];

// Access the feature layer using FeatureSetByPortalItem and filter by `to_inventory`
var fs = FeatureSetByPortalItem(
    Portal(p),
    itemID,
    layerID,
    ['cust_material_type', 'to_inventory'], // Fields to include
    false
);

// Apply a filter to only include records where `to_inventory` is "Yes" or "TBD"
var filteredFs = Filter(fs, "to_inventory IN ('Yes', 'TBD')");

// Initialize a dictionary to store counts for each domain value
var domainCounts = {};

// Initialize all domain counts to 0
for (var i in domainValues) {
    var code = domainValues[i]["code"];
    domainCounts[code] = 0;
}

// Loop through the filtered features and count occurrences of each material type
for (var f in filteredFs) {
    var materialType = f["cust_material_type"];
    if (materialType != null && HasKey(domainCounts, materialType)) {
        domainCounts[materialType] += 1;
    }
}

// Create data schema
var Dict = {
    'fields': [
        {'name': 'cust_material_type', 'type': 'esriFieldTypeString'},
        {'name': 'description', 'type': 'esriFieldTypeString'},
        {'name': 'count', 'type': 'esriFieldTypeInteger'}
    ],
    'geometryType': '',
    'features': []
};

// Populate the data schema with the counts
for (var i in domainValues) {
    var code = domainValues[i]["code"];
    var name = domainValues[i]["name"];
    var count = domainCounts[code]; // Retrieve count from domainCounts

    // If count is undefined, set it to 0
    if (count == null) {
        count = 0;
    }

    Push(Dict.features, {
        'attributes': {
            'cust_material_type': code,
            'description': name,
            'count': count
        }
    });
}

// Return the featureset
return FeatureSet(Text(Dict));

 

0 Kudos
1 Reply
jcarlson
MVP Esteemed Contributor

It's interesting, the chart "sees" the other values, but when there's nothing to render for the Pie Chart, it won't include it in the legend.

jcarlson_0-1725482628430.png

A serial chart will show the empty values, though:

jcarlson_1-1725482722207.png

I think it's just a default setting on the pie chart, probably nothing you can do about it without something kinda hacky like making an SVG pie chart from scratch.

- Josh Carlson
Kendall County GIS
0 Kudos