FastReport
199 строк · 7.3 Кб
1using System;
2using System.Drawing;
3using System.Drawing.Drawing2D;
4using System.ComponentModel;
5using FastReport.Utils;
6
7namespace FastReport.Gauge.Linear
8{
9/// <summary>
10/// Represents a linear scale.
11/// </summary>
12#if !DEBUG
13[DesignTimeVisible(false)]
14#endif
15public class LinearScale : GaugeScale
16{
17#region Fields
18
19private float left;
20private float top;
21private float height;
22private float width;
23private int majorTicksNum;
24
25#endregion // Fields
26
27#region Properties
28
29
30
31#endregion // Properties
32
33#region Constructors
34
35/// <summary>
36/// Initializes a new instance of the <see cref="LinearScale"/> class.
37/// </summary>
38/// <param name="parent">The parent gauge object.</param>
39public LinearScale(GaugeObject parent) : base(parent)
40{
41MajorTicks = new ScaleTicks(10, 2, Color.Black);
42MinorTicks = new ScaleTicks(6, 1, Color.Black);
43majorTicksNum = 6;
44}
45
46#endregion // Constructors
47
48#region Private Methods
49
50private void DrawMajorTicksHorz(FRPaintEventArgs e)
51{
52IGraphics g = e.Graphics;
53Pen pen = e.Cache.GetPen(MajorTicks.Color, MajorTicks.Width * e.ScaleX, DashStyle.Solid);
54Brush brush = TextFill.CreateBrush(new RectangleF(Parent.AbsLeft * e.ScaleX, Parent.AbsTop * e.ScaleY,
55Parent.Width * e.ScaleX, Parent.Height * e.ScaleY), e.ScaleX, e.ScaleY);
56float x = left;
57float y1 = top;
58float y2 = top + height;
59float step = width / (majorTicksNum - 1);
60int textStep = (int)((Parent.Maximum - Parent.Minimum) / (majorTicksNum - 1));
61Font font = e.Cache.GetFont(Font.FontFamily, Parent.IsPrinting ? Font.Size : Font.Size * e.ScaleX * 96f / DrawUtils.ScreenDpi, Font.Style);
62string text = Parent.Minimum.ToString();
63float y3 = y1 - 0.4f * Units.Centimeters * e.ScaleY;
64if ((Parent as LinearGauge).Inverted)
65{
66y3 = y2 - g.MeasureString(text, Font).Height + 0.4f * Units.Centimeters * e.ScaleY;
67}
68for (int i = 0; i < majorTicksNum; i++)
69{
70g.DrawLine(pen, x, y1, x, y2);
71SizeF strSize = g.MeasureString(text, Font);
72g.DrawString(text, font, brush, x - strSize.Width / 2 * e.ScaleX / (DrawUtils.ScreenDpi / 96f), y3);
73text = Convert.ToString(textStep * (i + 1) + Parent.Minimum);
74x += step;
75}
76brush.Dispose();
77}
78
79private void DrawMinorTicksHorz(FRPaintEventArgs e)
80{
81IGraphics g = e.Graphics;
82Pen pen = e.Cache.GetPen(MinorTicks.Color, MinorTicks.Width * e.ScaleX, DashStyle.Solid);
83float x = left;
84float y1 = top + height * 0.2f;
85float y2 = top + height - height * 0.2f;
86float step = width / (majorTicksNum - 1) / 4;
87for (int i = 0; i < majorTicksNum - 1; i++)
88{
89x += step;
90for (int j = 0; j < 3; j++)
91{
92g.DrawLine(pen, x, y1, x, y2);
93x += step;
94}
95}
96}
97
98private void DrawMajorTicksVert(FRPaintEventArgs e)
99{
100IGraphics g = e.Graphics;
101Pen pen = e.Cache.GetPen(MajorTicks.Color, MajorTicks.Width * e.ScaleX, DashStyle.Solid);
102Brush brush = TextFill.CreateBrush(new RectangleF(Parent.AbsLeft * e.ScaleX, Parent.AbsTop * e.ScaleY,
103Parent.Width * e.ScaleX, Parent.Height * e.ScaleY), e.ScaleX, e.ScaleY);
104float y = top + height;
105float x1 = left;
106float x2 = left + width;
107float step = height / (majorTicksNum - 1);
108int textStep = (int)((Parent.Maximum - Parent.Minimum) / (majorTicksNum - 1));
109Font font = e.Cache.GetFont(Font.FontFamily, Parent.IsPrinting ? Font.Size : Font.Size * e.ScaleX * 96f / DrawUtils.ScreenDpi, Font.Style);
110string text = Parent.Minimum.ToString();
111for (int i = 0; i < majorTicksNum; i++)
112{
113g.DrawLine(pen, x1, y, x2, y);
114SizeF strSize = g.MeasureString(text, Font);
115float x3 = x1 - strSize.Width * e.ScaleX / (DrawUtils.ScreenDpi / 96f) - 0.04f * Units.Centimeters * e.ScaleX;
116if ((Parent as LinearGauge).Inverted)
117{
118x3 = x2 + 0.04f * Units.Centimeters * e.ScaleX;
119}
120g.DrawString(text, font, brush, x3, y - strSize.Height / 2 * e.ScaleY / (DrawUtils.ScreenDpi / 96f));
121text = Convert.ToString(textStep * (i + 1) + Parent.Minimum);
122y -= step;
123}
124brush.Dispose();
125}
126
127private void DrawMinorTicksVert(FRPaintEventArgs e)
128{
129IGraphics g = e.Graphics;
130Pen pen = e.Cache.GetPen(MinorTicks.Color, MinorTicks.Width * e.ScaleX, DashStyle.Solid);
131float y = top + height;
132float x1 = left + width * 0.2f;
133float x2 = left + width - width * 0.2f;
134float step = height / (majorTicksNum - 1) / 4;
135for (int i = 0; i < majorTicksNum - 1; i++)
136{
137y -= step;
138for (int j = 0; j < 3; j++)
139{
140g.DrawLine(pen, x1, y, x2, y);
141y -= step;
142}
143}
144}
145
146#endregion // Private Methods
147
148#region Public Methods
149
150/// <inheritdoc/>
151public override void Assign(GaugeScale src)
152{
153base.Assign(src);
154
155LinearScale s = src as LinearScale;
156MajorTicks.Assign(s.MajorTicks);
157MinorTicks.Assign(s.MinorTicks);
158}
159
160/// <inheritdoc/>
161public override void Draw(FRPaintEventArgs e)
162{
163base.Draw(e);
164
165if (Parent.Vertical)
166{
167left = (Parent.AbsLeft + 0.7f * Units.Centimeters) * e.ScaleX;
168top = (Parent.AbsTop + 0.5f * Units.Centimeters) * e.ScaleY;
169height = (Parent.Height - 1.0f * Units.Centimeters) * e.ScaleY;
170width = (Parent.Width - 1.4f * Units.Centimeters) * e.ScaleX;
171
172DrawMajorTicksVert(e);
173DrawMinorTicksVert(e);
174}
175else
176{
177left = (Parent.AbsLeft + 0.5f * Units.Centimeters) * e.ScaleX;
178top = (Parent.AbsTop + 0.6f * Units.Centimeters) * e.ScaleY;
179height = (Parent.Height - 1.2f * Units.Centimeters) * e.ScaleY;
180width = (Parent.Width - 1.0f * Units.Centimeters) * e.ScaleX;
181
182DrawMajorTicksHorz(e);
183DrawMinorTicksHorz(e);
184}
185}
186
187/// <inheritdoc/>
188public override void Serialize(FRWriter writer, string prefix, GaugeScale diff)
189{
190base.Serialize(writer, prefix, diff);
191
192LinearScale dc = diff as LinearScale;
193MajorTicks.Serialize(writer, prefix + ".MajorTicks", dc.MajorTicks);
194MinorTicks.Serialize(writer, prefix + ".MinorTicks", dc.MinorTicks);
195}
196
197#endregion // Public Methods
198}
199}
200