Overview
You will learn: how to build an app to apply symbol colors and styles to features based on attribute values.
Applications can display feature layer data with different styles to enhance the visualization. The first step is to select the correct type of Renderer. A SimpleRenderer applies the same symbol to all features, a UniqueValueRenderer applies a different symbol to each unique attribute value, and a ClassBreaksRenderer applies a symbol to a range of numeric values. Renderers are responsible for accessing the data and applying the appropriate symbol to each feature when the layer draws. Labels can also be displayed to show attribute information for each feature, and visual variables and expressions can also be used to create more complex data-driven visualizations. Visit the documentation to learn more about styling layers.
In this tutorial, you will apply different renderers to enhance the visualization of the Trailheads, Trails and Parks and Open Spaces feature layers.
Zoom in on the map below to see the different layer styles.
Steps
Create a starter app
Open the JavaScript Starter App on CodePen.
In CodePen, click Fork and save the pen as
ArcGIS API for JavaScript Tutorials: Style feature layers.
Style trailheads with a hiker image and labels
In the
requirestatement, add a reference to the FeatureLayer module.require([ "esri/Map", "esri/views/MapView", "esri/layers/FeatureLayer" ], function(Map, MapView, FeatureLayer) {At the end of the code in the main
function, create atrailheadsRendererobject and define it as asimplerenderer and set the symbol properties to draw an hiker image that is apicture-marker,18pxin size for each point. Use theurlbelow for the image.var trailheadsRenderer = { type: "simple", symbol: { type: "picture-marker", url: "http://static.arcgis.com/images/Symbols/NPS/npsPictograph_0231b.png", width: "18px", height: "18px" } };To show trail name labels, create a
trailheadsLabelsobject to define the labelingInfo. Set thesymbolto draw the labels white#FFFFFFwith a green#5E8D74halo, place them centered above the feature, and use a simple expression to reference theTRL_NAMEfield (the data to render).var trailheadsLabels = { symbol: { type: "text", color: "#FFFFFF", haloColor: "#5E8D74", haloSize: "2px", font: { size: "12px", family: "Noto Sans", style: "italic", weight: "normal" } }, labelPlacement: "above-center", labelExpressionInfo: { expression: "$feature.TRL_NAME" } };Create a
trailheadsFeatureLayer and set theurl,renderer, andlabelingInfoproperty to the objects created above and add it to the map.var trailheads = new FeatureLayer({ url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0", renderer: trailheadsRenderer, labelingInfo: [trailheadsLabels] }); map.add(trailheads);Run your code to view the hiker images and labels.
Style trail width by elevation gain
To style features with a different symbol based on attribute values, you can use VisualVariables.
At the end of the code in the main
function, create a SimpleRenderer object with VisualVariables defined to make trails with greater elevation change wider than trails with smaller elevation changes. Set the renderer type tosimple, and the symbol properties to#BA55D3(purple),simple-lineandsolid. Next, add a new object to the visualVariables property. This will define a size visual variable. Set the type in the visual variable tosizeto indicate symbol size will vary based on a data value. Then set the field toELEV_GAIN(the field to use to determine line width), and the min and max data values to0and2300respectively. To control the width of the lines, set theminSizeandmaxSizevalues to3pxand7px.var trailsRenderer = { type: "simple", symbol: { color: "#BA55D3", type: "simple-line", style: "solid" }, visualVariables: [ { type: "size", field: "ELEV_GAIN", minDataValue: 0, maxDataValue: 2300, minSize: "3px", maxSize: "7px" } ] };Create a
trailsFeatureLayer and set theurl,renderer, andopacity(0.75) properties, and add it to the map.var trails = new FeatureLayer({ url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails/FeatureServer/0", renderer: trailsRenderer, opacity: 0.75 }); map.add(trails, 0);Run the code to view trails. The renderer will interpolate all of the data values and apply the appropriate width to each trail.
Style a trails layer to show bike-only trails
If you would like to style and draw just a subset of the data in a layer, you can apply a different symbol, set a the definitionExpression (SQL where clause), and then add the layer on top of the existing layer.
At the bottom of the code in the main function, create a
simplerenderer object with a symbol that is asimple-line,short-dot, pink#FF91FF,1pxwide line.var bikeTrailsRenderer = { type: "simple", symbol: { type: "simple-line", style: "short-dot", color: "#FF91FF", width: "1px" } };Create a
bikeTrailsFeatureLayer and set theurl,rendererand the definitionExpression to USE_BIKE = 'YES', and add it to the map.var bikeTrails = new FeatureLayer({ url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails/FeatureServer/0", renderer: bikeTrailsRenderer, definitionExpression: "USE_BIKE = 'YES'" }); map.add(bikeTrails, 1);Run your code to view the bike trails.
Style park areas with unique colors
In the main
function, create a UniqueValueRenderer object to use a different symbol for each type of park area. Define a function that will create asolid,simple-filltype symbol for each park type. Next, define a renderer and set the type tounique-value, field toTYPEand the uniqueValueInfos to the symbol values below.function createFillSymbol(value, color) { return { value: value, symbol: { color: color, type: "simple-fill", style: "solid", outline: { style: "none" } }, label: value }; } var openSpacesRenderer = { type: "unique-value", field: "TYPE", uniqueValueInfos: [ createFillSymbol("Natural Areas", "#9E559C"), createFillSymbol("Regional Open Space", "#A7C636"), createFillSymbol("Local Park", "#149ECE"), createFillSymbol("Regional Recreation Park", "#ED5151") ] };Create an
openspacesFeatureLayer, set the renderer andopacity(0.2) properties, and add it to the map.var openspaces = new FeatureLayer({ url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Parks_and_Open_Space/FeatureServer/0", renderer: openSpacesRenderer, opacity: 0.2 }); map.add(openspaces, 0);Run the code to view the Parks and Open Spaces. This will draw the different types of parks and open spaces with four unique symbols.
Congratulations, you're done!
Your app should look something like this.
Challenge
Visit the renderers and visual variables documentation to learn more about the different types of styles and visualizations you can create for your data.