GaugeObject.cs 8.9 KB

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