Layer order in a legend

1752
4
11-22-2016 10:31 PM
PitersonPaulgek
New Contributor III

Hi,

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
PanagiotisPapadopoulos
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
PanagiotisPapadopoulos
Esri Regular Contributor

this is a sample code

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

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

.....

and here see a live sample

Edit fiddle - JSFiddle 

PitersonPaulgek
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
KenBuja
MVP Esteemed Contributor