Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2499: added license header and removed unused usings

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22
23using System.Drawing.Drawing2D;
24using SharpMap;
25using SharpMap.Rendering.Decoration;
26using System;
27using System.Drawing;
28
29using System.Globalization;
30
31
32namespace HeuristicLab.BioBoost.Views.MapViews {
33  public class GradientLegend : MapDecoration {
34    public Size Size { get; set; }
35    public double MinValue { get; set; }
36    public double MaxValue { get; set; }
37    public ColorBlend ColorBlend { get; set; }
38    public Point Offset { get; set; }
39    public Font Font { get; set; }
40    public String Unit { get; set; }
41
42    public GradientLegend() {
43      Size = new Size(150, 20);
44      Anchor = MapDecorationAnchor.LeftBottom;
45      MinValue = 0;
46      MaxValue = 1;
47      Offset = new Point(10, 10);
48      // color blend does not match the gradient legend exactly
49      /*
50       ColorBlend  = new ColorBlend {
51      Colors = new[] {
52        Color.FromArgb(0, 0, 127),
53        Color.FromArgb(0, 0, 255),
54        Color.FromArgb(0, 255, 255),
55        Color.FromArgb(0, 255, 0),
56        Color.FromArgb(127, 255, 0),
57        Color.FromArgb(255, 255, 0),
58        Color.FromArgb(255, 127, 0),
59        Color.FromArgb(255, 0, 0),
60        Color.FromArgb(127, 0, 0)},
61      Positions = new[] { 0.0f, 0.125f, 0.25f, 0.375f, 0.5f, 0.625f, 0.75f, 0.875f, 1f }
62       };
63       */
64      // GradientLegend uses ColorBlend from System.Drawing and we need to convert it to a SharpMap colorblend here
65      ColorBlend = new ColorBlend() {
66        Colors = LazyValueLayerGenerator.DefaultColorBlend.Colors,
67        Positions = LazyValueLayerGenerator.DefaultColorBlend.Positions
68      };
69      Font = (Font)SystemFonts.DefaultFont.Clone();
70    }
71
72    protected override Size InternalSize(Graphics g, Map map) {
73      return new Size(Size.Width + Offset.X + 4, Size.Height + Offset.Y + 3);
74    }
75
76    protected override void OnRender(Graphics g, Map map) {
77      var pos = new PointF(g.ClipBounds.Left + Offset.X, g.ClipBounds.Bottom - Offset.Y - Size.Height);
78      var brush = new System.Drawing.Drawing2D.LinearGradientBrush(pos, new PointF(pos.X + Size.Width, pos.Y), Color.Black, Color.Black) {
79        InterpolationColors = ColorBlend
80      };
81      g.FillRectangle(new SolidBrush(Color.FromArgb(127, 255, 255, 255)),
82        pos.X - 1, pos.Y - 1, Size.Width + 3, Size.Height + 2);
83      var minStr = MinValue.ToString(CultureInfo.InvariantCulture);
84      var maxStr = MaxValue.ToString("G4", CultureInfo.InvariantCulture);
85      if (!String.IsNullOrEmpty(Unit)) maxStr += " " + Unit;
86      var minDim = g.MeasureString(minStr, Font);
87      var maxDim = g.MeasureString(maxStr, Font);
88      var textHeight = Math.Min(minDim.Height, maxDim.Height);
89      g.FillRectangle(brush, pos.X, pos.Y + textHeight, Size.Width, Size.Height - textHeight - 2);
90      g.DrawString(minStr, Font, Brushes.Black, pos.X, pos.Y);
91      g.DrawString(maxStr, Font, Brushes.Black, pos.X + Size.Width - maxDim.Width - 2, pos.Y);
92      g.DrawLine(Pens.Black, pos.X, pos.Y, pos.X, pos.Y + Size.Height);
93      g.DrawLine(Pens.Black, pos.X + Size.Width, pos.Y, pos.X + Size.Width, pos.Y + Size.Height);
94
95      g.DrawLine(Pens.Black, pos.X + Size.Width / 4, pos.Y + textHeight, pos.X + Size.Width / 4, pos.Y + Size.Height);
96      g.DrawLine(Pens.Black, pos.X + Size.Width / 2, pos.Y + textHeight / 2, pos.X + Size.Width / 2, pos.Y + Size.Height);
97      g.DrawLine(Pens.Black, pos.X + Size.Width / 4 * 3, pos.Y + textHeight, pos.X + Size.Width / 4 * 3, pos.Y + Size.Height);
98    }
99
100  }
101}
Note: See TracBrowser for help on using the repository browser.