Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost.Views/3.3/MapViews/GradientLegend.cs @ 13072

Last change on this file since 13072 was 13072, checked in by gkronber, 8 years ago

#2499: added code from HeuristicLab.BioBoost.Views (from private repository) nothing much has been changed

File size: 3.5 KB
Line 
1using System.Drawing.Drawing2D;
2using SharpMap;
3using SharpMap.Rendering.Decoration;
4using System;
5using System.Drawing;
6
7using System.Globalization;
8
9
10namespace HeuristicLab.BioBoost.Views.MapViews {
11  public class GradientLegend : MapDecoration {
12    public Size Size { get; set; }
13    public double MinValue { get; set; }
14    public double MaxValue { get; set; }
15    public ColorBlend ColorBlend { get; set; }
16    public Point Offset { get; set; }
17    public Font Font { get; set; }
18    public String Unit { get; set; }
19
20    public GradientLegend() {
21      Size = new Size(150, 20);
22      Anchor = MapDecorationAnchor.LeftBottom;
23      MinValue = 0;
24      MaxValue = 1;
25      Offset = new Point(10, 10);
26      // color blend does not match the gradient legend exactly
27      /*
28       ColorBlend  = new ColorBlend {
29      Colors = new[] {
30        Color.FromArgb(0, 0, 127),
31        Color.FromArgb(0, 0, 255),
32        Color.FromArgb(0, 255, 255),
33        Color.FromArgb(0, 255, 0),
34        Color.FromArgb(127, 255, 0),
35        Color.FromArgb(255, 255, 0),
36        Color.FromArgb(255, 127, 0),
37        Color.FromArgb(255, 0, 0),
38        Color.FromArgb(127, 0, 0)},
39      Positions = new[] { 0.0f, 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.75f, 0.875f, 1f }
40       };
41       */
42      // GradientLegend uses ColorBlend from System.Drawing and we need to convert it to a SharpMap colorblend here
43      ColorBlend = new ColorBlend() {
44        Colors = LazyValueLayerGenerator.DefaultColorBlend.Colors,
45        Positions = LazyValueLayerGenerator.DefaultColorBlend.Positions
46      };
47      Font = (Font)SystemFonts.DefaultFont.Clone();
48    }
49
50    protected override Size InternalSize(Graphics g, Map map) {
51      return new Size(Size.Width + Offset.X + 4, Size.Height + Offset.Y + 3);
52    }
53
54    protected override void OnRender(Graphics g, Map map) {
55      var pos = new PointF(g.ClipBounds.Left + Offset.X, g.ClipBounds.Bottom - Offset.Y - Size.Height);
56      var brush = new System.Drawing.Drawing2D.LinearGradientBrush(pos, new PointF(pos.X + Size.Width, pos.Y), Color.Black, Color.Black) {
57        InterpolationColors = ColorBlend
58      };
59      g.FillRectangle(new SolidBrush(Color.FromArgb(127, 255, 255, 255)),
60        pos.X - 1, pos.Y - 1, Size.Width + 3, Size.Height + 2);
61      var minStr = MinValue.ToString(CultureInfo.InvariantCulture);
62      var maxStr = MaxValue.ToString("G4", CultureInfo.InvariantCulture);
63      if (!String.IsNullOrEmpty(Unit)) maxStr += " " + Unit;
64      var minDim = g.MeasureString(minStr, Font);
65      var maxDim = g.MeasureString(maxStr, Font);
66      var textHeight = Math.Min(minDim.Height, maxDim.Height);
67      g.FillRectangle(brush, pos.X, pos.Y + textHeight, Size.Width, Size.Height - textHeight - 2);
68      g.DrawString(minStr, Font, Brushes.Black, pos.X, pos.Y);
69      g.DrawString(maxStr, Font, Brushes.Black, pos.X + Size.Width - maxDim.Width - 2, pos.Y);
70      g.DrawLine(Pens.Black, pos.X, pos.Y, pos.X, pos.Y + Size.Height);
71      g.DrawLine(Pens.Black, pos.X + Size.Width, pos.Y, pos.X + Size.Width, pos.Y + Size.Height);
72
73      g.DrawLine(Pens.Black, pos.X + Size.Width / 4, pos.Y + textHeight, pos.X + Size.Width / 4, pos.Y + Size.Height);
74      g.DrawLine(Pens.Black, pos.X + Size.Width / 2, pos.Y + textHeight / 2, pos.X + Size.Width / 2, pos.Y + Size.Height);
75      g.DrawLine(Pens.Black, pos.X + Size.Width / 4 * 3, pos.Y + textHeight, pos.X + Size.Width / 4 * 3, pos.Y + Size.Height);
76    }
77
78  }
79}
Note: See TracBrowser for help on using the repository browser.