Renderers fromJson Method

813
3
Jump to solution
08-03-2017 02:57 PM
JillianStanford
Occasional Contributor

Hi!

Does anybody have a working example of how to use the fromjson method on the esri/renderers/jsonUtils module?

Using the following code I expect a blue symbology to be applied but the blueRenderer object always ends up 'undefined'.

var featureLayer = new FeatureLayer("https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_Counties/FeatureServer/0");
var rendererConfig = '{"color"[0,112,255,64],"outline": {"color": [0,0,0,255],"width": 1,"type": "esriSLS","style": "esriSLSSolid","marker": null},"type": "esriSFS","style": "esriSFSSolid"}';
var blueRenderer = jsonUtils.fromJson(rendererConfig);
featureLayer.setRenderer(blueRenderer);
map.addLayer(featureLayer);

Full code here - https://codepen.io/js_gsi/pen/prNvXJ.

Maybe my JSON is not well formed? I pulled it from this playground.

Thank you!

Jill

0 Kudos
1 Solution

Accepted Solutions
ThomasSolow
Regular Contributor

The JSON you're feeding it here is a symbol JSON, not a renderer JSON.

In this situation you should probably create a symbol and a simpleRenderer and pass the symbol into the SimpleRenderer.  I wouldn't do this using fromJSON, although you could.

The from/to JSON methods are there so that instances of classes can be passed over a network or stored in a database.  If you're creating the renderer in the browser in your code, it's not necessary to use those methods.

View solution in original post

3 Replies
ThomasSolow
Regular Contributor

The JSON you're feeding it here is a symbol JSON, not a renderer JSON.

In this situation you should probably create a symbol and a simpleRenderer and pass the symbol into the SimpleRenderer.  I wouldn't do this using fromJSON, although you could.

The from/to JSON methods are there so that instances of classes can be passed over a network or stored in a database.  If you're creating the renderer in the browser in your code, it's not necessary to use those methods.

View solution in original post

RobertScheitlin__GISP
MVP Esteemed Contributor

And here is a snippet of what Thomas is talking about:

var featureLayer = new FeatureLayer("https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_Counties/FeatureServer/0");
var rendererConfig = '{"type": "simple","label": "","description": "","symbol": {"color": [0,112,255,64],"outline": {"color": [0,0,0,255],"width": 1,"type": "esriSLS","style": "esriSLSSolid"},"type": "esriSFS","style": "esriSFSSolid"}}';
var blueRenderer = new SimpleRenderer(rendererConfig);
featureLayer.setRenderer(blueRenderer);
map.addLayer(featureLayer);‍‍‍‍‍
JillianStanford
Occasional Contributor

Ack, of course! Sometimes you just need someone to point out the obvious!

I am storing several renderers in a config file and the user will choose one at run time. The fromJSON method is nice because it instantiates a renderer without my knowing the type at design time.

This is what finally worked from me:

var featureLayer = new FeatureLayer("https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_Counties/FeatureServer/0");
var rendererConfig = {"type": "simple","label": "","description": "","symbol": {"color": [0,112,255,64],"outline": {"color": [0,0,0,255],"width": 1,"type": "esriSLS","style": "esriSLSSolid","marker": null},"type": "esriSFS","style": "esriSFSSolid"}};   
var blueRenderer = jsonUtils.fromJson(rendererConfig);
featureLayer.setRenderer(blueRenderer);
map.addLayer(featureLayer);

Many thanks to you both for the quick replies!

Jill

0 Kudos