Hi,
In a similar vein to this post (thinkgeo.com/forums/MapSuite/tabid/143/aft/11161/Default.aspx)
At the moment, our legend data is built up using user supplied data (so we may get lots of entries, and we also may get long entries)
At the moment, if the legend layer has too many entries, then they are simply lost. If the entries are really long, then the text is cut-off.
I guess there are two options to fixing this: word wrapping (but overriding TextStyle doesnt work), or the additional of a scroll bar?
Ideally, we’d like this to be available for both WPF and MVC editions (but we’ll take one or the other if its possible) :)
Can you please suggest something?
Thanks,
Steve
LegendAdornmentLayer with long entries
Hi Steve,
Sorry it looks our Legend related code have some issues.
The code as below is a temporary solution, We will keep enhancemnet the legend functions. This solution can auto warp string which separated by blank(" “) and auto calculate the legend height.
Please let me know whether it works for you.
Regards,
Don
public partial class MainForm : Form
{
private void Form_Load(object sender, EventArgs e)
{
winformsMap1.MapUnit = GeographyUnit.DecimalDegree;
winformsMap1.AdornmentOverlay.Layers.Clear();
MyLegendAdornmentLayer layer = new MyLegendAdornmentLayer();
layer.Width = 150;
layer.Location = AdornmentLocation.LowerLeft;
string a = “This is a test label which shows xxxxxxxxxxxx”;
string b = “yeeeep it’s right”;
MyLegendItem item2 = new MyLegendItem();
item2.TextStyle = new TextStyle(b, new GeoFont(“Arial”, 12), new GeoSolidBrush(GeoColor.StandardColors.Black));
item2.ImageStyle = AreaStyles.CreateSimpleAreaStyle(fixedLayer1Color);
layer.LegendItems.Add(item2);
layer.Height += 28;
MyLegendItem item3 = new MyLegendItem();
item3.TextStyle = new TextStyle(“bbb”, new GeoFont(“Arial”, 12), new GeoSolidBrush(GeoColor.StandardColors.Black));
item3.ImageStyle = AreaStyles.CreateSimpleAreaStyle(fixedLayer2Color);
layer.LegendItems.Add(item3);
winformsMap1.AdornmentOverlay.Layers.Add(layer);
winformsMap1.Refresh();
}
}
class MyLegendAdornmentLayer : LegendAdornmentLayer
{
protected override void DrawCore(GeoCanvas canvas, Collection<SimpleCandidate> labelsInAllLayers)
{
SimulatorDrawItemAndCalculateHeight(canvas);
base.DrawCore(canvas, labelsInAllLayers);
}
private void SimulatorDrawItemAndCalculateHeight(GeoCanvas canvas)
{
GdiPlusGeoCanvas tmpGeoCanvas = new GdiPlusGeoCanvas();
Bitmap bitmap = new Bitmap((int)Width, (int)Height);
tmpGeoCanvas.BeginDrawing(bitmap, canvas.CurrentWorldExtent, canvas.MapUnit);
foreach (LegendItem item in LegendItems)
item.Draw(tmpGeoCanvas, new Collection<SimpleCandidate>(), new LegendDrawingParameters());
tmpGeoCanvas.EndDrawing();
float newHeight = 0;
foreach (LegendItem item in LegendItems)
{
newHeight += item.Height;
}
this.Height = newHeight + 25;
}
}
class MyLegendItem : LegendItem
{
protected override int CustomWarpLength()
{
GdiPlusGeoCanvas textGeoCanvas = new GdiPlusGeoCanvas();
int textWishWidth = (int)textGeoCanvas.MeasureText(TextStyle.TextColumnName, TextStyle.Font).Width;
float textLimitWidth = Width - (ImageWidth + ImageLeftPadding + ImageRightPadding + TextLeftPadding + TextRightPadding);
if (textLimitWidth < textWishWidth)
{
int limitStringLength = (Int32)((textLimitWidth / textWishWidth) * TextStyle.TextColumnName.Length);
if (!TextStyle.TextColumnName.Contains(”\r\n"))
TextStyle.TextColumnName = SplitText(TextStyle.TextColumnName, limitStringLength);
}
return 0;
}
private string SplitText(string textValue, int limitStringLength)
{
string[] values = textValue.Split(’ ');
Collection<string> sections = FilterStrings(values, limitStringLength);
this.Height = (this.Height - TextTopPadding) * sections.Count + TextTopPadding;
return String.Join("\r\n", sections);
}
private Collection<string> FilterStrings(string[] source, int limitStringLength)
{
Collection<string> result = new Collection<string>();
string currentString = “”;
foreach (string value in source)
{
if (value.Length + 1 + currentString.Length < limitStringLength)
{
if (currentString.Length == 0)
{
currentString = value;
}
else
{
currentString = currentString + " " + value;
}
}
else
{
result.Add(currentString);
currentString = “”;
currentString = value;
}
}
if (currentString != “”)
{
result.Add(currentString);
currentString = “”;
}
return result;
}
}
Thanks,
This will suit us for the time being.
Cheers,
Steve
Steve,
I am glad to hear that’s helpful.
Regards,
Don