FastReport
322 строки · 9.2 Кб
1using System;
2using System.ComponentModel;
3using System.Collections.Generic;
4using FastReport.Utils;
5using System.Drawing;
6using System.Drawing.Design;
7
8namespace FastReport.Gauge
9{
10/// <summary>
11/// Represents a gauge object.
12/// </summary>
13public partial class GaugeObject : ReportComponentBase
14{
15#region Fields
16
17private double maximum;
18private double minimum;
19private double value;
20private GaugeScale scale;
21private GaugePointer pointer;
22private GaugeLabel label;
23private string expression;
24
25#endregion // Fields
26
27#region Properties
28
29/// <summary>
30/// Gets or sets the minimal value of gauge.
31/// </summary>
32[Category("Layout")]
33public double Minimum
34{
35get { return minimum; }
36set
37{
38if (value < maximum)
39{
40minimum = value;
41if (this.value < minimum)
42{
43this.value = minimum;
44}
45}
46}
47}
48
49/// <summary>
50/// Gets or sets the maximal value of gauge.
51/// </summary>
52[Category("Layout")]
53public double Maximum
54{
55get { return maximum; }
56set
57{
58if (value > minimum)
59{
60maximum = value;
61if (this.value > maximum)
62{
63this.value = maximum;
64}
65}
66}
67}
68
69/// <summary>
70/// Gets or sets the current value of gauge.
71/// </summary>
72[Category("Layout")]
73public double Value
74{
75get { return value; }
76set
77{
78if ((value >= minimum) && (value <= maximum))
79{
80this.value = value;
81}
82else if (value < minimum)
83{
84this.value = minimum;
85}
86else if (value > maximum)
87{
88this.value = maximum;
89}
90}
91}
92
93/// <summary>
94/// Gets or sets scale of gauge.
95/// </summary>
96[Category("Appearance")]
97[TypeConverter(typeof(FastReport.TypeConverters.FRExpandableObjectConverter))]
98[Editor("FastReport.TypeEditors.ScaleEditor, FastReport", typeof(UITypeEditor))]
99public GaugeScale Scale
100{
101get { return scale; }
102set { scale = value; }
103}
104
105/// <summary>
106/// Gets or sets pointer of gauge.
107/// </summary>
108[Category("Appearance")]
109[TypeConverter(typeof(FastReport.TypeConverters.FRExpandableObjectConverter))]
110[Editor("FastReport.TypeEditors.PointerEditor, FastReport", typeof(UITypeEditor))]
111public GaugePointer Pointer
112{
113get { return pointer; }
114set { pointer = value; }
115}
116
117/// <summary>
118/// Gets or sets gauge label.
119/// </summary>
120[Category("Appearance")]
121[TypeConverter(typeof(FastReport.TypeConverters.FRExpandableObjectConverter))]
122[Editor("FastReport.TypeEditors.LabelEditor, FastReport", typeof(UITypeEditor))]
123public virtual GaugeLabel Label
124{
125get { return label; }
126set { label = value; }
127}
128
129/// <summary>
130/// Gets or sets an expression that determines the value of gauge object.
131/// </summary>
132[Category("Data")]
133[Editor("FastReport.TypeEditors.ExpressionEditor, FastReport", typeof(UITypeEditor))]
134public string Expression
135{
136get { return expression; }
137set { expression = value; }
138}
139
140/// <summary>
141/// Gets a value that specifies is gauge vertical or not.
142/// </summary>
143[Browsable(false)]
144public bool Vertical
145{
146get { return Width < Height; }
147}
148
149#endregion // Properties
150
151#region Constructors
152
153/// <summary>
154/// Initializes a new instance of the <see cref="GaugeObject"/> class.
155/// </summary>
156public GaugeObject()
157{
158minimum = 0;
159maximum = 100;
160value = 10;
161scale = new GaugeScale(this);
162pointer = new GaugePointer(this);
163label = new GaugeLabel(this);
164expression = "";
165}
166
167/// <summary>
168/// Initializes a new instance of the <see cref="GaugeObject"/> class.
169/// </summary>
170/// <param name="minimum">Minimum value of gauge.</param>
171/// <param name="maximum">Maximum value of gauge.</param>
172/// <param name="value">Current value of gauge.</param>
173public GaugeObject(double minimum, double maximum, double value)
174{
175this.minimum = minimum;
176this.maximum = maximum;
177this.value = value;
178scale = new GaugeScale(this);
179pointer = new GaugePointer(this);
180label = new GaugeLabel(this);
181expression = "";
182}
183
184/// <summary>
185/// Initializes a new instance of the <see cref="GaugeObject"/> class.
186/// </summary>
187/// <param name="minimum">Minimum value of gauge.</param>
188/// <param name="maximum">Maximum value of gauge.</param>
189/// <param name="value">Current value of gauge.</param>
190/// <param name="scale">Scale of gauge.</param>
191/// <param name="pointer">Pointer of gauge.</param>
192public GaugeObject(double minimum, double maximum, double value, GaugeScale scale, GaugePointer pointer)
193{
194this.minimum = minimum;
195this.maximum = maximum;
196this.value = value;
197this.scale = scale;
198this.pointer = pointer;
199label = new GaugeLabel(this);
200expression = "";
201}
202
203#endregion // Constructors
204
205#region Report Engine
206
207/// <inheritdoc/>
208public override string[] GetExpressions()
209{
210List<string> expressions = new List<string>();
211expressions.AddRange(base.GetExpressions());
212
213if (!String.IsNullOrEmpty(Expression))
214{
215expressions.Add(Expression);
216}
217return expressions.ToArray();
218}
219
220/// <inheritdoc/>
221public override void GetData()
222{
223base.GetData();
224
225if (!String.IsNullOrEmpty(Expression))
226{
227object val = Report.Calc(Expression);
228if (val != null)
229{
230try
231{
232Value = Converter.StringToFloat(val.ToString());
233}
234catch
235{
236Value = 0.0;
237}
238}
239}
240}
241
242#endregion // Report Engine
243
244#region Public Methods
245
246/// <inheritdoc/>
247public override void Assign(Base source)
248{
249base.Assign(source);
250
251GaugeObject src = source as GaugeObject;
252Maximum = src.Maximum;
253Minimum = src.Minimum;
254Value = src.Value;
255Expression = src.Expression;
256Scale.Assign(src.Scale);
257Pointer.Assign(src.Pointer);
258Label.Assign(src.Label);
259}
260
261/// <summary>
262/// Draws the gauge.
263/// </summary>
264/// <param name="e">Draw event arguments.</param>
265public override void Draw(FRPaintEventArgs e)
266{
267base.Draw(e);
268scale.Draw(e);
269pointer.Draw(e);
270Border.Draw(e, new RectangleF(AbsLeft, AbsTop, Width, Height));
271}
272
273/// <inheritdoc/>
274public override void Serialize(FRWriter writer)
275{
276GaugeObject c = writer.DiffObject as GaugeObject;
277base.Serialize(writer);
278
279if (Maximum != c.Maximum)
280{
281writer.WriteDouble("Maximum", Maximum);
282}
283if (Minimum != c.Minimum)
284{
285writer.WriteDouble("Minimum", Minimum);
286}
287if (Value != c.Value)
288{
289writer.WriteDouble("Value", Value);
290}
291if (Expression != c.Expression)
292{
293writer.WriteStr("Expression", Expression);
294}
295if (Scale != c.Scale)
296{
297Scale.Serialize(writer, "Scale", c.Scale);
298}
299if (Pointer != c.Pointer)
300{
301Pointer.Serialize(writer, "Pointer", c.Pointer);
302}
303if (Label != c.Label)
304{
305Label.Serialize(writer, "Label", c.Label);
306}
307}
308
309/// <summary>
310/// Clone Gauge Object
311/// </summary>
312/// <returns> clone of this object</returns>
313public GaugeObject Clone()
314{
315var clone = Activator.CreateInstance(this.GetType()) as GaugeObject;
316clone.Assign(this);
317return clone;
318}
319
320#endregion // Public Methods
321}
322}
323