Select to view content in your preferred language

Getting colour of outline or fill relating to FeatureLayer

126
2
Wednesday
Aeseir
by
Occasional Contributor

I want to get the value of the fill and outline for a FeatureLayer.

 

Previously for a FeatureLayer of type polygon you would do

featureLayer.renderer.get("symbol.outline.color") // get outline color
featureLayer.renderer.get("symbol.color") // fill color

As of 4.28, `Accessor.get` is deprecated in favor of using optional chaining().

If you apply the chaining 

featureLayer.renderer?.symbol?.outline?.color // will lose its mind if you have linter on

 

 

The current workaround is to cast the featureLayer to any before using chaining.

If you have no-explicit-any in your linter the above goes mental and you back at square one.

 

What is the proper way to get the values, i must be missing something obivious.

0 Kudos
2 Replies
Sage_Wall
Esri Contributor

I'm surprised this ever worked @Aeseir there isn't a symbol property on the Renderer class.  Since many renderers can have multiple symbols associated with them, but I guess this approach might work if the renderer was a SimpleRenderer and the symbol was a SimpleFillSymbol?  TypeScript would defiantly complain without doing a lot of casting like this but it's really ugly and I wouldn't suggest it, but I didn't get any typescript or linting errors.

Sage_Wall_0-1718313000975.png

 

I would suggest querying the feature service for the specific feature's who's symbol outline you want to grab with queryFeatures(), and then get the outline from the graphic property on the feature.   https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Feature.html#graphic

 

As for linting and optional chaining I don't seem to have an issue when using this eslint config I just get an error saying renderer doesn't have a symbol property.  Maybe it's something in your linting configuration?

 

 

{
    "env": {
        "browser": true,
        "es2021": true
    },
    "extends": [
        "eslint:recommended",
        "plugin:@typescript-eslint/recommended"
    ],
    "overrides": [
    ],
    "parser": "@typescript-eslint/parser",
    "parserOptions": {
        "ecmaVersion": "latest",
        "sourceType": "module"
    },
    "plugins": [
        "@typescript-eslint"
    ],
    "rules": {
    }
}

 

 

 

0 Kudos
Aeseir
by
Occasional Contributor

Thanks @Sage_Wall .

 

yea i did some minor magic with your sdk to make things work. It makes no sense for it to be referencing the inherited parent class.

 

QOL recommendation the ESRI team updates the SDK to return the actual Renderer class being used by the layer. User can determine type using the type property that is inherited from Renderer.

 

i.e.

 

 

 export class FeatureLayer {
// code omitted for brevity
   /**
     * The renderer assigned to the layer.
     *
     * [Read more...](https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-FeatureLayer.html#renderer)
     */
    renderer: SimpleRenderer | UniqueValueRenderer | PieChartRenderer | HeatmapRenderer | ClassBreaksRenderer| DotDensityRenderer; // just grabbed a few
// code omitted for brevity
  }

 

 

Looking at the SDK doesn't look like it would result in breaking changes, it would make it more flexible, and could actually help the ESRI team long term by simplifying some future refactors and feature developments.

 

0 Kudos