Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/TextShape.cs @ 1366

Last change on this file since 1366 was 1337, checked in by bspisic, 16 years ago

#520 Implemented font changes of x-axis, legend and title

File size: 3.4 KB
Line 
1using System;
2using System.Drawing;
3
4namespace HeuristicLab.Visualization {
5  public enum AnchorPositionX {
6    Left, Middle, Right
7  }
8
9  public enum AnchorPositionY {
10    Bottom, Middle, Top
11  }
12
13  public class TextShape : IShape {
14    private IShape parent;
15    private Font font;
16    private Brush brush;
17    private Color color;
18    private string text;
19    private double x;
20    private double y;
21
22    private AnchorPositionX anchorPositionX = AnchorPositionX.Left;
23    private AnchorPositionY anchorPositionY = AnchorPositionY.Top;
24
25    public TextShape(string text) : this(0, 0, text, 14) {}
26
27    public TextShape(double x, double y, string text) : this(x, y, text, 8) {}
28
29    public TextShape(double x, double y, string text, int fontSize) {
30      this.x = x;
31      this.y = y;
32      this.text = text;
33      font = new Font("Arial", fontSize);
34
35      Color = Color.Blue;
36    }
37
38    public TextShape(double x, double y, string text, Font font, Color color) {
39      this.x = x;
40      this.y = y;
41      this.text = text;
42      Font = font;
43      Color = color;
44    }
45
46    public string Text {
47      get { return text; }
48      set { text = value; }
49    }
50
51    public double X {
52      get { return x; }
53      set { x = value; }
54    }
55
56    public double Y {
57      get { return y; }
58      set { y = value; }
59    }
60
61    public Color Color {
62      get { return color; }
63      set {
64        color = value;
65        brush = new SolidBrush(color);
66      }
67    }
68
69    public Font Font {
70      get { return font; }
71      set { font = value; }
72    }
73
74    public AnchorPositionX AnchorPositionX {
75      get { return anchorPositionX; }
76      set { anchorPositionX = value; }
77    }
78
79    public AnchorPositionY AnchorPositionY {
80      get { return anchorPositionY; }
81      set { anchorPositionY = value; }
82    }
83
84    #region IShape Members
85
86    public void Draw(Graphics graphics) {
87      int screenX = Transform.ToScreenX(x, Parent.Viewport, Parent.ClippingArea);
88      int screenY = Transform.ToScreenY(y, Parent.Viewport, Parent.ClippingArea);
89
90      SizeF size = graphics.MeasureString(text, font);
91
92      switch (AnchorPositionX) {
93        case AnchorPositionX.Left:
94          break;
95        case AnchorPositionX.Middle:
96          screenX -= (int)(size.Width/2);
97          break;
98        case AnchorPositionX.Right:
99          screenX -= (int)size.Width;
100          break;
101        default:
102          throw new NotSupportedException("Unknown anchor position: " + AnchorPositionX);
103      }
104
105      switch (AnchorPositionY) {
106        case AnchorPositionY.Top:
107          break;
108        case AnchorPositionY.Middle:
109          screenY -= (int)(size.Height/2);
110          break;
111        case AnchorPositionY.Bottom:
112          screenY -= (int)size.Height;
113          break;
114        default:
115          throw new NotSupportedException("Unknown anchor position: " + AnchorPositionX);
116      }
117
118      graphics.DrawString(text, font, brush, screenX, screenY);
119    }
120
121    public RectangleD BoundingBox {
122      get { return RectangleD.Empty; }
123    }
124
125    public RectangleD ClippingArea {
126      get { return Parent.ClippingArea; }
127    }
128
129    public Rectangle Viewport {
130      get { return Parent.Viewport; }
131    }
132
133    public IShape Parent {
134      get { return parent; }
135      set { parent = value; }
136    }
137
138    #endregion
139  }
140}
Note: See TracBrowser for help on using the repository browser.