Layer order in a legend

11-22-2016 10:31 PM
New Contributor III


There is no order by geometry type in a legend created with a legend widget.

How  layers can be ordered? Generally, we need to implement the rule:

- points

- lines

- polygons

bottom - polygons.

Any help will be appreciated.

0 Kudos
4 Replies
Esri Regular Contributor

the property define the layer order on Legend Widget according to the Javascript Help is the LayerInfos

see below.

<Object[]> layerInfosOptionalSpecify a subset of the layers in the map to display in the legend. If not set, all layers in the map will display in the legend. The order of the layers in the legend depends on the layerInfo array. See the layerInfos property description for a description of its object properties.

So during the layer load on the map change the layer order.

0 Kudos
Esri Regular Contributor

this is a sample code

//add the legend
map.on("layers-add-result", function (evt) {
var layerInfoLines = [];, function (layer, index) {
if (layer.layer.geometryType === "esriGeometryPolyline"){
var layerInfoPolygons = [];, function (layer, index) {
if (layer.layer.geometryType === "esriGeometryPolygon"){
var layerInfo = layerInfoPolygons.concat(layerInfoLines);

if (layerInfo.length > 0) {
var legendDijit = new Legend({
map: map,
layerInfos: layerInfo
}, "legendDiv");


and here see a live sample

Edit fiddle - JSFiddle 

New Contributor III

Thank you for response, Panagiotis Papadopoulos.

Unfortunately, we have no geometry type when working with ArcGISDynamicMapServiceLayer.

So, how we obtain that geometryType?

0 Kudos
MVP Esteemed Contributor