Select to view content in your preferred language

Printing a legend for a dynamically rendered dynamic layer

7681
10
01-26-2014 05:40 PM
MalcolmWilliamson
Emerging Contributor
I'm afraid I'm attempting another impossible task. I'm using class break and unique value renderers to allow the user to choose which attribute of a dynamic layer is used as the basis for their map. This works great in the browser, and also makes nice print maps, but I can't get the legend info into the resulting PDF map. Is there any way to pass the resulting legend to the Print dijit when using the esri.request approach? Any other options?

Thanks very much.
0 Kudos
10 Replies
RobertWinterbottom
Deactivated User
You can try using the PrintTask.  You can find info on it here: https://developers.arcgis.com/javascript/jsapi/printtask-amd.html.

It takes a URL and a PrintParameters object as its params.  Inside the PrintParameters object you can pass in a PrintTemplate object which can use all the default values and then you can pass inside the layoutOptions, an array of legend layers.

The legend Layers need to be legend layer objects and not an array of layer ids, something like here: https://developers.arcgis.com/javascript/jsapi/legendlayer-amd.html.

You will need to create a new legend layer object, give it a layerId and sublayer ids and then push that into the array of legend layers.

Here's a small snippet from one of my apps:
if (optionalLayer.visible) {
    legendLayer = new esri.tasks.LegendLayer();
    legendLayer.layerId = optionalLayer.id;
    legendLayer.subLayerIds = optionalLayer.visibleLayers;
    options.legendLayers.push(legendLayer);
}


If you pass in the legend layer info correctly the print task should be able to pull in custom symbology for your layer from the renderer, you should actually be able to see it in your outgoing request JSON.
AmosWachanga
Deactivated User

I had the same problem only to realize when using Portrait the legend is present. Meaning my legend had so many items that it did not show. I'm not sure why Malcolm Williamson‌'s is not working. Or maybe this post is long due and a solution was found?

0 Kudos
MalcolmWilliamson
Emerging Contributor
Following your advice, I've got my layer id and sublayer ids pushed into an array that is passed to the legendLayers property. If you look at tail end of the JSON being posted (below), you'll see that legendOptions includes those. However, I'm still getting no legend at all on the PDF maps. Thoughts? I can't locate an example app which includes a legend and a print option to compare.

Format PDF
Layout_Template A3 Landscape
Web_Map_as_JSON {"mapOptions":{"showAttribution":true,"extent":{"xmin":-10884089.275607755,"ymin":4039507.1494186064,"xmax":-9710016.521147506,"ymax":4407627.877639997,"spatialReference":{"wkid":102100}},"spatialReference":{"wkid":102100},"scale":2311162.217155},"operationalLayers":[{"id":"layer0","title":"layer0","opacity":1,"minScale":591657527.591555,"maxScale":2256.994353,"type":"OpenStreetMap","url":"http://a.tile.openstreetmap.org"},{"id":"Districts","title":"Districts","opacity":0.7,"minScale":0,"maxScale":0,"url":"http://165.29.23.149/arcgis/rest/services/DistrictReports_FY22/MapServer","visibleLayers":[32],"layers":[{"id":32,"layerDefinition":{"id":32,"source":{"type":"mapLayer","mapLayerId":32},"drawingInfo":{"renderer":{"type":"classBreaks","field":"Two_or_More_Races_Total_Pie","minValue":0,"classBreakInfos":[{"classMaxValue":161,"label":"0 - 161","description":"","symbol":{"color":[255,255,204,255],"outline":{"color":[0,0,0,255],"width":0.75,"type":"esriSLS","style":"esriSLSSolid"},"type":"esriSFS","style":"esriSFSSolid"}},{"classMaxValue":322,"label":"162 - 322","description":"","symbol":{"color":[218,196,131,255],"outline":{"color":[0,0,0,255],"width":0.75,"type":"esriSLS","style":"esriSLSSolid"},"type":"esriSFS","style":"esriSFSSolid"}},{"classMaxValue":483,"label":"323 - 483","description":"","symbol":{"color":[181,127,72,255],"outline":{"color":[0,0,0,255],"width":0.75,"type":"esriSLS","style":"esriSLSSolid"},"type":"esriSFS","style":"esriSFSSolid"}},{"classMaxValue":644,"label":"484 - 644","description":"","symbol":{"color":[144,58,29,255],"outline":{"color":[0,0,0,255],"width":0.75,"type":"esriSLS","style":"esriSLSSolid"},"type":"esriSFS","style":"esriSFSSolid"}},{"classMaxValue":805,"label":"645 - 805","description":"","symbol":{"color":[107,0,0,255],"outline":{"color":[0,0,0,255],"width":0.75,"type":"esriSLS","style":"esriSLSSolid"},"type":"esriSFS","style":"esriSFSSolid"}}],"classificationMethod":"esriClassifyEqualInterval"}},"minScale":0,"maxScale":0}}]},{"id":"map_graphics","minScale":0,"maxScale":0,"featureCollection":{"layers":[]}}],"exportOptions":{"outputSize":[800,1100],"dpi":96},"layoutOptions":{"titleText":"Two or More Races Total","authorText":"Made by: Arkansas Department of Education","copyrightText":"<copyright info here>","scaleBarOptions":{"metricUnit":"Kilometers","metricLabel":"km","nonMetricUnit":"Miles","nonMetricLabel":"mi"},"legendOptions":{"operationalLayers":[{"id":"Districts","subLayerIds":[32]}]}}}
f json


I appreciate your help!
0 Kudos
RobertWinterbottom
Deactivated User
hmmm, everything in the request looks ok and it even looks like all the rendering info is being passed in.

Would you be able to put up a sample in JSFiddle if its not too much that I could look it. 

I was looking for some code samples on my mac and I realized on one of the apps, we have a custom image service layer and that we had to do a sort of trick to get a legend to appear where we put a dummy layer in a map service with a mock legend to represent the image service layer.  All other apps I have built and are using print are using default symbology except for one which is showing full page map only with no legend, I know we have one or two at work built by some of my colleagues that have print functionality with custom renderers so I will check on them when I get in tomorrow to see if they are showing legends or if I can tweak the one I have to add a legend to it.

I don't think this is impossible, if I recall correctly we have done it at our office in one of our apps but I will have to get back to you on that, been in school all day so my mind is not on GIS at the moment, maybe someone from Esri in the meantime can comment on this to maybe point out something I'm forgetting or point us to a sample.
0 Kudos
MalcolmWilliamson
Emerging Contributor
Ok, my first attempt at migrating an entire app to JSFiddle. This looks pretty ugly when crammed into the Results window, but it seems to work (other than the problem with the legend in the "printed" PDF!):
   http://jsfiddle.net/d5b65/
Normally, one of 62 different layers in the service is passed through the URL (...?layer=32), but I hard-coded this in the fiddle.

I'm pretty new to the javascript API, so I'm sure you'll see all kinds of atrocious code in there - don't worry, you won't offend me! If you can help me overcome this, I'll be very grateful.
0 Kudos
AleydisG__Pere
Regular Contributor
I think the problem here are the default print templates.
The legend is placed in the bottom of the page leaving very small room for the legend info. If a layer has too many symbols it will simply not be displayed in the legend. To check this, load just one or two layers in your app rendered with a simple renderer. You should see the layers info in the bottom left side of the page.
The solution is simple. Edit the default print templates and place the legend elment somewhere else leaving more space for the layer info. And also check that "Shrink contents to fit frame" option to make the layer info fit the legend element (you'll find it in the Fitting strategy section inside the legend properties,  here).
Hope this helps.
MalcolmWilliamson
Emerging Contributor
I think the problem here are the default print templates.
The legend is placed in the bottom of the page leaving very small room for the legend info. If a layer has too many symbols it will simply not be displayed in the legend. To check this, load just one or two layers in your app rendered with a simple renderer. You should see the layers info in the bottom left side of the page.
The solution is simple. Edit the default print templates and place the legend elment somewhere else leaving more space for the layer info. And also check that "Shrink contents to fit frame" option to make the layer info fit the legend element (you'll find it in the Fitting strategy section inside the legend properties,  here).
Hope this helps.


Thanks, I don't believe that this is the problem - at most my legend contains one layer with five classifications, and some of my maps are a single point layer rendered with a simple renderer. The "Shrink contents to fit frame" option is indeed selected, and the minimum font size is set to 4 pt. Any further thoughts?
0 Kudos
MalcolmWilliamson
Emerging Contributor
Robert, have you had a chance to look for a similar example?

Thanks,
    -Malcolm

hmmm, everything in the request looks ok and it even looks like all the rendering info is being passed in.

Would you be able to put up a sample in JSFiddle if its not too much that I could look it. 

I was looking for some code samples on my mac and I realized on one of the apps, we have a custom image service layer and that we had to do a sort of trick to get a legend to appear where we put a dummy layer in a map service with a mock legend to represent the image service layer.  All other apps I have built and are using print are using default symbology except for one which is showing full page map only with no legend, I know we have one or two at work built by some of my colleagues that have print functionality with custom renderers so I will check on them when I get in tomorrow to see if they are showing legends or if I can tweak the one I have to add a legend to it.

I don't think this is impossible, if I recall correctly we have done it at our office in one of our apps but I will have to get back to you on that, been in school all day so my mind is not on GIS at the moment, maybe someone from Esri in the meantime can comment on this to maybe point out something I'm forgetting or point us to a sample.
0 Kudos
AlexeiB
Deactivated User

Has anyone found a solution to the problem? I am facing something similar and would really appreciate any suggestions to get this to work.

Thanks,

Alexei

0 Kudos