How to add legend to custom print layout with print dijit params

2780
3
02-22-2016 02:47 PM
DouglasGuess
Occasional Contributor

I have a custom widget that allows the user to enter a query string and select a field to symbolize the selection set by unique value.  I want to pass the unique field values/symbolization to the legend of a custom print layout template.  The attached image represents an example of a selected field, unique values and their symbolization (This is what I'd like the legend to look like).  This is what I would like to pass to a Legend element I have in my custom print layout template.  I can pass text elements with no problem, just can't figure out how to pass a legend item.  I'm using the print dijit and set the "templates" param to a variable that contains info pertaining to that specific layout.  I see there is a "legendLayers": [], param within the layoutOptions; however, I can't seem to get this to work.  As I mentioned, I'd like the legend to look like the attached image.  Does anyone know if this is possible?  Any help/suggestions would be greatly appreciated.

Thanks,

Doug

0 Kudos
3 Replies
YueWu1
by Esri Regular Contributor
Esri Regular Contributor

Hi Douglas,

The LegendLayer in Print Template is normally used for specific feature layer that you already define with the renderer. For instance, in your scenario, when user select the dropdown to define the renderer. You should give that featurelayer an id, then attach the renderer to that layer.

Then add the layer to map.

Later, you need to define a new legend as LegendLayer, and follow this doc to define the legendLayer id match with the featurelayer that you previous assign with the renderer.

LegendLayer | API Reference | ArcGIS API for JavaScript:

var stateLegend = new LegendLayer(); 
stateLegend.layerId = "FL";

Then add this line into layoutOptions:

"legendLayer": stateLegend 

For a similar sample about using legendLayer, you can take a look about this sample: developer-support/ModifyPrintLegend_preCallback.html at master · Esri/developer-support · GitHub

Hope this can help!

DouglasGuess
Occasional Contributor

Yue, thanks for the reply.  I've looked at the API reference and the sample you provided; however, my rendering is not on an entire feature layer.  I'm creating a selection set and rendering the results as a graphics layer.  Is there a way to set the legend layer to a graphics layer instead of a feature layer?

0 Kudos
YueWu1
by Esri Regular Contributor
Esri Regular Contributor

Hi Doug,

I do check our API documentation and previous thread on Geonet,

Unfortunately, the legend dijit only supports tiled, dynamic, kml and feature layers.

Legend | API Reference | ArcGIS API for JavaScript

However, the workaround is create a feature layer from a feature collection and you'll be able to use that with the legend dijit. According to the old thread: Legend for Graphics Layer

Try this:  http://jsfiddle.net/6R3T8

0 Kudos