Hello ,
I’m using LegendAdornmentLayer and LegendItem for add legend to printerlayer on my map .
If width of legendItem’s text is larger than width of LegendAdornmentLayer then it grows out of LegendAdornmentLayer .
This is my code :
LegendAdornmentLayer.LegendItems.Add(new LegendItem()
{
ImageStyle = new AreaStyle(new GeoSolidBrush(GeoColor.FromArgb(170, GeoColor.StandardColors.Green))),
TextStyle = new TextStyle(“ddddddddddddddddddddddddddddddddddddddddddddddddd”, new GeoFont(“Arial”, 8), new GeoSolidBrush(GeoColor.SimpleColors.Black))
});
My question is how can I resize legend to fit the longest legend item ?
Regards
Resize legend to fit the longest legend item
Hi Maryam,
Here I have a sample about how to adjust your text follow legend size.
thinkgeo.com/forums/MapSuite/tabid/143/aft/11606/Default.aspx
Could you have a look at it and let me know whether that’s helpful?
Regards,
Don
Hello,
I am also facing similar scenario.
Your code in mentioned topic is working well but in my scenario user can adjust font family and font size of legend items. I think it should be a way that let us calculate width of legend based on longest item.
Any idea?
Regards
Hi Shaahin,
This code can calculate width of longest text label, you can set width of LegendAdornmentLayer after that.
GdiPlusGeoCanvas textGeoCanvas = new GdiPlusGeoCanvas();
int textWishWidth = (int)textGeoCanvas.MeasureText(TextStyle.TextColumnName, TextStyle.Font).Width;
Regards,
Don
Hi Don,
your recent response works fine when i use AdornmentOverlay and add LegendAdornmentLayer to it , but i work on Printing and have to use LegendPrinterLayer because i need to SetPosition for it .
can you help me on this new problem ?
thanks .
LegendAdornmentLayer.LegendItems.Add(new LegendItem()
{
ImageStyle = new AreaStyle(new GeoSolidBrush(GeoColor.FromArgb(170, GeoColor.StandardColors.Green))),
TextStyle = new TextStyle(legendText, new GeoFont("Arial", dynamicValue), new GeoSolidBrush(GeoColor.SimpleColors.Black))
});
var legendPrinterLayer = new LegendPrinterLayer(LegendAdornmentLayer)
{
BackgroundMask = AreaStyles.CreateSimpleAreaStyle(new GeoColor(255, 230, 230, 230),
GeoColor.SimpleColors.Black, 1),
Width = CalculateLegendWidth()
};
legendPrinterLayer.SetPosition(2, 1, -2.9, 3.9, PrintingUnit.Inch);
var printerInteractiveOverlay = (PrinterInteractiveOverlay)Map1.InteractiveOverlays["PrintPreviewOverlay"];
printerInteractiveOverlay.PrinterLayers.Add("LegendPrinterLayer", legendPrinterLayer);
=============================
calculate width based on your response
float CalculateLegendWidth()
{
var textGeoCanvas = new GdiPlusGeoCanvas();
var result = (float)0;
var currentValue = (float)0;
LegendAdornmentLayer.LegendItems.ToList().ForEach(c =>
{
currentValue = textGeoCanvas.MeasureText(c.TextStyle.TextColumnName, c.TextStyle.Font).Width;
if (currentValue > result)
result = currentValue;
});
return result;
}
Hi Maryam,
You can simply use this method
legendPrinterLayer.SetPosition(width,
height, centerPointX, centerPointY, PrintingUnit.Inch) to reach your purpose, The
parameter ‘width’ is relative to the legend layer width, so if layer’s width
is longer than the longest item, then you have to set the width to the longest
legenditem length(PrintingUnit is Inch) plus an offset like 0.6.
Here I attached a sample in case you can refer and if the issue persists, please feel free to let us know.
Regards,
Johnny
LegendTest.zip (9.89 KB)
Thanks for creating a sample for me. your response works fine.
Hi Maryam,
Good to hear it works.
Any questions, don’t hesitate to let us know.
Thanks,
Johnny