This project has moved and is read-only. For the latest updates, please go here.

Formatting label decimals...

Topics: SharpMap v2.0
Sep 10, 2007 at 3:32 PM
One of my developers has created a simple application using SharpMap so that we can learn more about its architecture. It has gone well but we are missing one thing. We could like to display labels with 2 decimals of precision. My developer has not found away to do that. The labels display with the number of actual decimals associated with each feature. 1, 2.5, 3.257, etc. Is there a way that we can specific the format for numeric data?

Sep 10, 2007 at 4:50 PM
I'm guessing you are using v1.0, and not v2.0 (since labels aren't really working in v2.0 yet).

There are 2 ways to do this kind of formatting: either format your data in the dataset before you render it, or use a GetLabelMethod delegate and assign it to the LabelLayer's LabelStringDelegate property. In the delegate method, you can return featureDataRow["MyAttribute"].ToString("N2"); or something similar.
Sep 10, 2007 at 5:38 PM
Ah, crap...

I downloaded the source code in the middle of July, and I loaded the project in Branches 2.0, but when I look at the Assemblies property, is says So've I've been running the old build all this time?

So this code doesn't work in 2.0?

SharpMap.Layers.LabelLayer layRxLabel = new SharpMap.Layers.LabelLayer("Rx labels");
layRxLabel.DataSource = myLayer.DataSource;
layRxLabel.Enabled = true;
layRxLabel.LabelColumn = rx.Column.ToString();
layRxLabel.Style = new SharpMap.Styles.LabelStyle();
layRxLabel.Style.ForeColor = Color.Black;
layRxLabel.Style.Font = new Font(FontFamily.GenericSerif, 10);
layRxLabel.MultipartGeometryBehaviour = LabelLayer.MultipartGeometryBehaviourEnum.All;
layRxLabel.Style.HorizontalAlignment = SharpMap.Styles.LabelStyle.HorizontalAlignmentEnum.Center;
layRxLabel.Style.VerticalAlignment = SharpMap.Styles.LabelStyle.VerticalAlignmentEnum.Middle;
layRxLabel.Style.CollisionDetection = true;
layRxLabel.Style.Offset = new PointF(3, 3);
layRxLabel.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
layRxLabel.SRID = 4326;
myMap.Layers.Add(layRxLabel); //Add label layer to map

Thanks. Do you have an anticipated time for label support in 2.0?
Sep 10, 2007 at 7:30 PM
Right - /branches/v2 is just a copy of v1.0 stuff right now, until CodePlex can finally get the SVN bridge up and running centrally (i.e. on the CodePlex servers). The current codebase for v2.0 is

The API will be different, but similar. The above code might not be exact, but it will be very close (actually I don't see much that will change except you need to create the layer with the data source in the constructor now, and GDI+ primatives like PointF, Font and Color are replaced with SharpMap rendering primatives Point2D, StyleFont and StyleColor which are used identically). I'm working on getting a Beta 1 of v2.0 done this week, and labels and raster support will come shortly after that, before the Beta 2 release.