Select to view content in your preferred language

Serial Chart Calculation

587
11
Jump to solution
05-22-2025 01:29 PM
GaryMorris
Occasional Contributor

I am creating a Dashboard with several different serial charts.  My data has a length field in feet; I am not able to edit the data or add a field.  I need to have a bar chart that displays the length in miles.  Is there a way for me to calculate this within the serial chart?

0 Kudos
1 Solution

Accepted Solutions
JustPete
Regular Contributor

Try this:

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

// Provide the actual Portal Item ID and layer index
var fs = FeatureSetByPortalItem(portal, '<PORTAL_ITEM_ID>', 2, ['PATH_Length', 'Path_Rating_txt'], false);
 
// Build an array of new features
var features = [];

for (var f in fs) {
    var lengthFeet = f["(PATH_Length"];
    var lengthMiles = Round(lengthFeet / 5280, 2)


    Push(features, {
        attributes: {
            length_miles: lengthMiles,
        }
    });
}

// Define the fields (include the new one for miles)
var convertedValues = {
    fields: [
        { name: 'Path_Rating_txt', type: 'esriFieldTypeString'},
        { name: 'PATH_Length', type: 'esriFieldTypeDouble' }
    ],
    geometryType: '',
    features: features
};

return FeatureSet(convertedValues);
;

View solution in original post

11 Replies
JustPete
Regular Contributor

The best way to do this, without having to create a new field in the data, would be to use the Dashboard Arcade function to return a new feature layer in the dashboard, where the units are converted from feet to miles.

Do you have a snapshot of your data? 

0 Kudos
GaryMorris
Occasional Contributor

I can get one, what do you mean by a snapshot?

0 Kudos
JustPete
Regular Contributor

An example of your data - fields etc

0 Kudos
GaryMorris
Occasional Contributor

It is a portal item, I was trying to do a FeatureSetByPortalItem but keep getting the error:  Test execution error: ',' expected.. Verify test data.

I need to be able to acce the "Path_Rating" field and the "PATH_Length" field and I need calculate the "PATH_Length" field so that it is divided by 5280 to convert it to miles.

 

 

Data.png

0 Kudos
JustPete
Regular Contributor

 I see what you're getting at. Your need to create a new array and push the converted values into that array: 

// Connect to portal and fetch feature layer
var portal = Portal("https://arcgis.com/");
var fs = FeatureSetByPortalItem(
    portal,
    "",
    ,
    ["*"],
    false
);

// Build an array of new features
var features = [];

for (var f in fs) {
    var lengthFeet = f["(PATH_Length"];
    var lengthMiles = Round(lengthFeet / 5280, 2)

var otherfieldnames= f["otherfieldnames"];
var otherfieldnames2= f["otherfieldnames2"];


    Push(features, {
        attributes: {
            length_miles: lengthMiles,
            otherfieldnames: otherfieldnames,
            otherfieldnames2, otherfieldnames2
        }
    });
}

// Define the fields (include the new one for miles)
var combinedRegistry = {
    fields: [
        { name: 'otherfieldnames', type: 'esriFieldTypeString'},
        { name: 'length_miles', type: 'esriFieldTypeDouble' }
        { name: 'otherfieldnames2', type: 'esriFieldTypeString'}
    ],
    geometryType: '',
    features: features
};

return FeatureSet(convertedValues);
0 Kudos
GaryMorris
Occasional Contributor

I really appreciate your help.  I have tried to apply this and keep getting "Test execution error:  Invalid variable assignment.  Verify test data.

here is what I have put in:

 

var portal = Portal('https://arcgis.com');
var fs = FeatureSetByPortalItem(portal,'',2, ['PATH_Length','Path_Rating_txt'], false);

for (var f in fs){
var lengthMiles = Round(f['PATH_Length']/5280,2);

var otherfieldnames=f['Path_Rating_txt'];

Push(features, {
  attributes:{lenthMiles,
  otherfieldnames: Path_Rating_txt
  }
});
}

var combinedRegistry={
  fields:[
    {name: 'Path_Rating_txt', type:'esriFieldTypeString'}
    {name: 'lengthMiles', type:'esriFieldTypeDouble'}
  ],
  geometryType:'',
  features:features
  };


return fs(convertedValues);
0 Kudos
JustPete
Regular Contributor

You need to build an array to push the features in, which is var features = []; just after you connect to your portal, and you need to return the converted values (or whatever you will call your fs). See below:

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

// Provide the actual Portal Item ID and layer index
var fs = FeatureSetByPortalItem(portal, '<PORTAL_ITEM_ID>', 2, ['PATH_Length', 'Path_Rating_txt'], false);

// Initialize the output array
var features = [];

for (var f in fs) {
var lengthMiles = Round(f['PATH_Length'] / 5280, 2);
var pathRating = f['Path_Rating_txt'];

Push(features, {
attributes: {
lengthMiles: lengthMiles,
Path_Rating_txt: pathRating
}
});
}

var convertedValues= {
fields: [
{ name: 'Path_Rating_txt', type: 'esriFieldTypeString' },
{ name: 'lengthMiles', type: 'esriFieldTypeDouble' }
],
geometryType: '', // Define geometryType if needed (e.g., 'esriGeometryPoint')
features: features
};

return convertedValues;

 

0 Kudos
GaryMorris
Occasional Contributor

This works when I run it in the data expression builder but when I close it I get "Unable to execute arcade script" when trying to select it to use in my serial layer.

0 Kudos
JustPete
Regular Contributor

Try this:

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

// Provide the actual Portal Item ID and layer index
var fs = FeatureSetByPortalItem(portal, '<PORTAL_ITEM_ID>', 2, ['PATH_Length', 'Path_Rating_txt'], false);
 
// Build an array of new features
var features = [];

for (var f in fs) {
    var lengthFeet = f["(PATH_Length"];
    var lengthMiles = Round(lengthFeet / 5280, 2)


    Push(features, {
        attributes: {
            length_miles: lengthMiles,
        }
    });
}

// Define the fields (include the new one for miles)
var convertedValues = {
    fields: [
        { name: 'Path_Rating_txt', type: 'esriFieldTypeString'},
        { name: 'PATH_Length', type: 'esriFieldTypeDouble' }
    ],
    geometryType: '',
    features: features
};

return FeatureSet(convertedValues);
;