Drawing Legend Example.

Sep 3, 2015 at 8:48 AM
Edited Sep 3, 2015 at 8:51 AM

I am trying to use the following codebase to create legends for a web application using sharpmap.


Can anyone help me with a sample code to draw legend to display polygon shapefiles.

With the code in the above link I am only getting a box with background colour in the right hand bottom corner.

I need to know how we can add legend values and colours in the box.

Sep 3, 2015 at 1:29 PM
Sep 3, 2015 at 2:05 PM
Hi FObermaier,

I tried that but I am not sure how to add items to the legend box.

Also the Legend comes only when the Anchor is LetTop in all other positions the box appears empty.

  1. I have one Polygon Layer which has values for example 1- 1000 and I have the maps displayed with colour codes for ranges from 0-100,100-200,...900-1000
  2. I have colour codes for each of these split values.
  3. I want to show colours in the legends.
I am not sure how I can display the legend.

Sep 4, 2015 at 11:18 AM
I try to translate that:
You have a map with one vector layer, that has a theme (GradientTheme?) applied to it.
var factory = new LegendFactory();
var legend = factory.Create(map, null);

If not, you need to show some code.
Sep 8, 2015 at 8:16 AM
Edited Sep 8, 2015 at 8:19 AM
Hi FObermaier,

This is the code I execute
            CustomTheme StateTheme = new CustomTheme(GetDefaultStateStyle);
            layStates.Theme = StateTheme;
            var factory = new LegendFactory();

             ((LegendSettings)factory.LegendSettings).SymbolSize = new Size(50, 50);
             ((LegendSettings)factory.LegendSettings).HeaderFont = new Font("Times New Roman",10);
             ((LegendSettings)factory.LegendSettings).ItemFont = new Font("Times New Roman", 10);
             ((LegendSettings)factory.LegendSettings).Padding = new Size(0,0);
             ((LegendSettings)factory.LegendSettings).Indentation = 7;
             ((LegendSettings)factory.LegendSettings).ForeColor = new SolidBrush(Color.Blue);

             var legend = factory.Create(map, null);
             ((MapDecoration)legend).BackgroundColor = System.Drawing.Color.White;
             ((MapDecoration)legend).BorderMargin = new Size(10, 10);
             ((MapDecoration)legend).Anchor = MapDecorationAnchor.LeftTop;
             ((MapDecoration)legend).BorderMargin = new Size(0, 0);
            legend.Root.Label = "This is a super long legend title";
            map.MaximumZoom = 360;
            map.Center = new Point(0, 0);

I am getting the following Image out put. I need to put the colour range information in the legends can you please help me out with this.


Ideally I would Like to add Legend something like this as shown in the pic below

Sep 8, 2015 at 11:58 AM
Creating legend items for a general purpose CustomTheme is not implemented/supported. That is why you don't see anything.

You could use SharpMap.Rendering.Thematics.CategoryTheme<double> which does what your picture shows.
Sep 9, 2015 at 7:42 AM
Edited Sep 9, 2015 at 7:42 AM

Thanks for pointing out that. Could you please share some sample code for implementing SharpMap.Rendering.Thematics.CategoryTheme<double> ?

For a quick view i tried the gradient it worked fine showing up on the Legends. Thanks for that !!!

Another point was that when the Legend position is shifted the to any other place other than TopLeft the legends disappear .. any Idea whats I must be doing wrong there ?