Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1265_HeuristicLab.Visualization/HeuristicLab.Visualization/3.3/Primitives/LabeledPrimitive.cs @ 15995

Last change on this file since 15995 was 13716, checked in by bburlacu, 9 years ago

#1265: Added option to select the SmoothingMode in the ChartControl. Introduced a LabeledPrimitive which encapsulates a RectangularPrimitiveBase primitive and supports drawing a text label on top of it.
Introduced a set of useful methods in the PrimitiveUtil class for calculating intersection points between linear and rectangular primitives (useful for connecting shapes together).

File size: 3.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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
22using System;
23using System.Drawing;
24using System.Drawing.Text;
25
26namespace HeuristicLab.Visualization {
27  public sealed class LabeledPrimitive : RectangularPrimitiveBase {
28    public string Label { get; set; }
29    public Font Font { get; set; }
30    public RectangularPrimitiveBase Primitive { get; private set; }
31    public Brush TextBrush { get; set; }
32
33    public LabeledPrimitive(RectangularPrimitiveBase primitive, string label, Font font, Brush textBrush = null) : base(primitive.Chart, primitive.LowerLeft, primitive.UpperRight, primitive.Pen, primitive.Brush) {
34      Label = label;
35      Primitive = primitive;
36      Font = font;
37      TextBrush = textBrush;
38    }
39
40    public override void Draw(Graphics graphics) {
41      base.Draw(graphics);
42      Primitive.Draw(graphics);
43    }
44
45    public override bool ContainsPoint(PointD point) {
46      return Primitive.ContainsPoint(point);
47    }
48
49    public override void PostDraw(Graphics graphics) {
50      base.PostDraw(graphics);
51      graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
52
53      if (string.IsNullOrEmpty(Label))
54        return;
55
56      var labelSize = graphics.MeasureString(Label, Font);
57      var primitiveSize = Chart.TransformWorldToPixel(new SizeD(Primitive.Size.Width, Primitive.Size.Height));
58
59      float scale = Math.Min(primitiveSize.Width / labelSize.Width, primitiveSize.Height / labelSize.Height);
60
61      Font = new Font(Font.FontFamily, Math.Min(12, Font.Size * scale));
62      labelSize = graphics.MeasureString(Label, Font);
63
64      var p1 = Chart.TransformWorldToPixel(LowerLeft);
65      var p2 = Chart.TransformWorldToPixel(UpperRight);
66
67      int width = Math.Abs(p1.X - p2.X);
68      int height = Math.Abs(p1.Y - p2.Y);
69
70      var brush = TextBrush ?? new SolidBrush(Primitive.Pen.Color);
71      graphics.DrawString(Label, Font, brush, p1.X + (width - labelSize.Width) / 2f, p2.Y + (height - labelSize.Height) / 2f);
72    }
73
74    public override void Move(PointD point, Offset delta) {
75      Primitive.Move(point, delta);
76      base.Move(point, delta);
77    }
78
79    public override void Move(Offset delta) {
80      Primitive.Move(delta);
81      base.Move(delta);
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.