Dynamic legend generation through the ADF
I have uploaded my code snippet to generate legend images through the ADF here. The purpose for this grew out of being able to print multiple map resources on the map through the ADF. I had blogged about it before here. I have made some changes to the code snippet after input from some other people trying to do the same thing. The features of the code snippet are:
- Generates images at the required DPI
- Customizable legend title
- Handles group layers
- Handles symbol groups
- Creates a legend image of the desired height and width. If the legend entries overflow the height specified, they will be omitted. If all legend entries are required in the images, it should only involve a small change in code to remove the check for overflow.
Here is an example of a legend generated by the snippet.
Very Good work. I loved work. I have reposted in my blog, so that it will get noticed and useful to others. Hope no issues!
[…] Click here to read the code. Author Vish […]
Has any converted the legend code to VB?
In the for loop
foreach (IGISFunctionality entry in item.Functionalities)
{
if (entry is IMapFunctionality)
mf = entry as IMapFunctionality;
}
the item.Functionalities count is zero, therefore not assigning mf to an IMapFunctionality object, and the whole function returns null.
Why would my map not have an IGISFunctionality entry? Do you know how I would fix this?
Thanks
Hi Brian,
Try initializing your MapResourceItem by calling MapResourceItem.InitializeResource().
Vish
Hello,
I am new to c# and ArcGIS Server, and need to customize Web mapping application to do same thing you did here. I copied this snipplet, however couldn’t make it work. I tried to call it from page_load, but it gives me error on Map map, then I added “using ESRI.ArcGIS.Web.ADF.UI.WebControls”. This solves Map problem but I couldn’t figure out BitMap. What should I do? What should I include more?
Thanks for all the help,
Gokce
Vish,
Very good post. Whar needs to be changed if i need to display only the items in the current map extent in the legend. Can you alos share the implementation code for above during printing .
–Thanan
were you aware of the static method on the toc.renderlegendhtml and the method of the scalebar and northarrow called draw()?
Could help out a bit…
Hello,
I’m also interested in the VB version of the code ….
Thank you…
if doesnt work, replace “trying ..” region with this codes
if need change 0 index replace to your index
#region “Trying to get the legend information”
Dictionary<string, KeyValuePair> legendInfo = new Dictionary<string, KeyValuePair>();
int layerCounter = 0;
IMapResource itemMP = map.MapResourceManagerInstance.GetResource(0);
IMapTocFunctionality mtoc = itemMP.CreateFunctionality(typeof(IMapTocFunctionality), Guid.NewGuid().ToString()) as IMapTocFunctionality;
IMapFunctionality mf = Map1.GetFunctionality(0);
TocDataFrame[] tocDataFrames = mtoc.GetMapContents(mf.Name, WebImageFormat.PNG24, true, false);
foreach (TocDataFrame tocDataFrame in tocDataFrames)
{
foreach (TocLayer tocLayer in tocDataFrame)
{
if (tocLayer.Visible)
{
List<KeyValuePair> legends = GetLegendImages(tocLayer);
if (legends != null && legends.Count > 0)
{
foreach (KeyValuePair entry in legends)
{
legendInfo.Add(layerCounter.ToString(), new KeyValuePair(entry.Key, entry.Value));
layerCounter++;
}
}
}
}
}
#endregion
Does anyone know, how to print the transparent graphics layer? I sent the buffered features to SOE, which has transparencies. But they are printing non-transparent.
Any workaround besides saving it as a shapefile and bring it back to SOE.
Thanks,
Abhi
Hi Vish, i need the code for generating a dynamic legend on the extent. like on a button click a window popsup showing only the legend of the visible layers in the extent. m using ArcGIS server web ADF with C#. would really appreciate your help on the same.
Hi,
I am new in GIS programming. I was lookin for a code take legends out of a map control.
Thank you very much for your code.It was a big help.
but I have a question.I need to update the legend by zoom in/out. Because in each zoom level we have different data and different symbols. so we need to update the legend by zooming .
You extrac the legend from toc of the map. but toc is not a dynamic entity proportion to the map .
so for a dynamic legend we need a another source updated by the map.
I would really appreciate your help / idead.
Thanks again.