When you call a layer of a map service published on ArcGIS Sever, you need to render it based on a specific field, but the service you publish does not have that field, and you can use that method.
/******************************************************************************** **************************FeatureLaye r*********************************** *************************************************************************** *****///New FeaturelayerFeaturelayer.queryfeatures (). Then (function(results) {varfeatures =Results.features; for(varFeatureinchfeatures) {features[feature].attributes["Nico"] =feature; } varPopuptemplate ={title:' {NAME} ', Content: [{type:"Fields", Fieldinfos: [{fieldName:"Name"}, {fieldName:"FID"},{fieldName:"XZDM"},{fieldName:"Nico" }] }] }; varFields =[{name:"XZDM", alias:"XZDM", type:"OID",},{name:"FID", alias:"FID", type:"Double",},{name:"Nico", alias:"Nico", type:"Integer", }]; //See the Sample snippet for the source and fields propertiesConst LAYER =NewFeaturelayer ({source:features, fields:fields, Objectidfield:"XZDM",//field name of the Object IDsGeometrytype: "Polygon", }); varRenderer ={type:"Simple",//autocasts as New Simplerenderer ()Symbol: {type: "Simple-fill"},//autocasts as New Simplefillsymbol ()visualvariables: [{type:"Color", field:"Nico", stops: [{value:0, color: {r:125, g:255, b:103, a:0.3}}, {value:One, color: {r:255, g:25, b:123, a:0.6} }] }] }; Layer.renderer=renderer; Map.add (layer);});
Here are two types of classification rendering methods
First Kind
/******************************************************************************** **************************SimpleRende rer*********************************** ************************************************************************* *******///generate random color, equal spacing classification based on maximum minimum value and number of classificationsvarMinclassvalue = 0;varMaxclassvalue = 10;varClassnum = 4;vardis = Math.Round ((maxclassvalue-minclassvalue)/classnum);//Building stops ArraysvarStops =NewArray (); for(varI=minclassvalue; i<maxclassvalue; i + =dis) { //building a Color object varcolor =NewObject (); COLOR.R= parseint (Math.random () *100); COLOR.G= parseint (Math.random () *100); Color.b= parseint (Math.random () *100); COLOR.A= Math.Round (Math.random () *10)/10;//build with value and color Property objects varA =NewObject (); A.value=i; A.color=color; Stops.push (a);}//SimplerenderervarCitiesRenderer01 ={type:"Simple",//autocasts as New Simplerenderer ()Symbol: {type: "Simple-fill"},//autocasts as New Simplefillsymbol ()visualvariables: [{type:"Color", field:"Nico", Stops:stops,}]};
The second Kind
/******************************************************************************** **************************ClassBreaks renderer*********************************** ******************************************************************** ************///generate random color, equal spacing classification based on maximum minimum value and number of classificationsvarMinclassvalue = 0;varMaxclassvalue = 10;varClassnum = 4;vardis = Math.Round ((maxclassvalue-minclassvalue)/classnum);//Building Classbreakinfos ArraysvarClassbreakinfos =NewArray (); for(varI=minclassvalue; i<maxclassvalue; i + =dis) { //building a Color object varcolor =NewObject (); COLOR.R= parseint (Math.random () *100); COLOR.G= parseint (Math.random () *100); Color.b= parseint (Math.random () *100); COLOR.A= Math.Round (Math.random () *10)/10;//to build a symbol object that contains the type and color properties varSymbol =NewObject (); Symbol.type= "Simple-fill", Symbol.color=color; //build an Obj object that contains MinValue, MaxValue, symbol, label varobj =NewObject (); Obj.minvalue=I, Obj.maxvalue= i +Dis, Obj.symbol=symbol; Obj.label= i + "~" + i +dis; Classbreakinfos.push (obj);}//ClassbreaksrenderervarCitiesRenderer01 ={type:"Class-breaks",//autocasts as New Classbreaksrenderer ()Field: "Nico", Classbreakinfos:classbreakinfos,};
ArcGIS Web apijavascript API4.6 generates Featurelayer and renders in the client browser