GaugeObject.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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
  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. else if (value < minimum)
  76. {
  77. this.value = minimum;
  78. }
  79. else if (value > maximum)
  80. {
  81. this.value = maximum;
  82. }
  83. }
  84. }
  85. /// <summary>
  86. /// Gets or sets scale of gauge.
  87. /// </summary>
  88. [Category("Appearance")]
  89. [TypeConverter(typeof(FastReport.TypeConverters.FRExpandableObjectConverter))]
  90. [Editor("FastReport.TypeEditors.ScaleEditor, FastReport", typeof(UITypeEditor))]
  91. public GaugeScale Scale
  92. {
  93. get { return scale; }
  94. set { scale = value; }
  95. }
  96. /// <summary>
  97. /// Gets or sets pointer of gauge.
  98. /// </summary>
  99. [Category("Appearance")]
  100. [TypeConverter(typeof(FastReport.TypeConverters.FRExpandableObjectConverter))]
  101. [Editor("FastReport.TypeEditors.PointerEditor, FastReport", typeof(UITypeEditor))]
  102. public GaugePointer Pointer
  103. {
  104. get { return pointer; }
  105. set { pointer = value; }
  106. }
  107. /// <summary>
  108. /// Gets or sets gauge label.
  109. /// </summary>
  110. [Category("Appearance")]
  111. [TypeConverter(typeof(FastReport.TypeConverters.FRExpandableObjectConverter))]
  112. [Editor("FastReport.TypeEditors.LabelEditor, FastReport", typeof(UITypeEditor))]
  113. public virtual GaugeLabel Label
  114. {
  115. get { return label; }
  116. set { label = value; }
  117. }
  118. /// <summary>
  119. /// Gets or sets an expression that determines the value of gauge object.
  120. /// </summary>
  121. [Category("Data")]
  122. [Editor("FastReport.TypeEditors.ExpressionEditor, FastReport", typeof(UITypeEditor))]
  123. public string Expression
  124. {
  125. get { return expression; }
  126. set { expression = value; }
  127. }
  128. /// <summary>
  129. /// Gets a value that specifies is gauge vertical or not.
  130. /// </summary>
  131. [Browsable(false)]
  132. public bool Vertical
  133. {
  134. get { return Width < Height; }
  135. }
  136. #endregion // Properties
  137. #region Constructors
  138. /// <summary>
  139. /// Initializes a new instance of the <see cref="GaugeObject"/> class.
  140. /// </summary>
  141. public GaugeObject()
  142. {
  143. minimum = 0;
  144. maximum = 100;
  145. value = 10;
  146. scale = new GaugeScale(this);
  147. pointer = new GaugePointer(this);
  148. label = new GaugeLabel(this);
  149. expression = "";
  150. }
  151. /// <summary>
  152. /// Initializes a new instance of the <see cref="GaugeObject"/> class.
  153. /// </summary>
  154. /// <param name="minimum">Minimum value of gauge.</param>
  155. /// <param name="maximum">Maximum value of gauge.</param>
  156. /// <param name="value">Current value of gauge.</param>
  157. public GaugeObject(double minimum, double maximum, double value)
  158. {
  159. this.minimum = minimum;
  160. this.maximum = maximum;
  161. this.value = value;
  162. scale = new GaugeScale(this);
  163. pointer = new GaugePointer(this);
  164. label = new GaugeLabel(this);
  165. expression = "";
  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>
  173. /// <param name="scale">Scale of gauge.</param>
  174. /// <param name="pointer">Pointer of gauge.</param>
  175. public GaugeObject(double minimum, double maximum, double value, GaugeScale scale, GaugePointer pointer)
  176. {
  177. this.minimum = minimum;
  178. this.maximum = maximum;
  179. this.value = value;
  180. this.scale = scale;
  181. this.pointer = pointer;
  182. label = new GaugeLabel(this);
  183. expression = "";
  184. }
  185. #endregion // Constructors
  186. #region Report Engine
  187. /// <inheritdoc/>
  188. public override string[] GetExpressions()
  189. {
  190. List<string> expressions = new List<string>();
  191. expressions.AddRange(base.GetExpressions());
  192. if (!String.IsNullOrEmpty(Expression))
  193. {
  194. expressions.Add(Expression);
  195. }
  196. return expressions.ToArray();
  197. }
  198. /// <inheritdoc/>
  199. public override void GetData()
  200. {
  201. base.GetData();
  202. if (!String.IsNullOrEmpty(Expression))
  203. {
  204. object val = Report.Calc(Expression);
  205. if (val != null)
  206. {
  207. try
  208. {
  209. Value = Converter.StringToFloat(val.ToString());
  210. }
  211. catch
  212. {
  213. Value = 0.0;
  214. }
  215. }
  216. }
  217. }
  218. #endregion // Report Engine
  219. #region Public Methods
  220. /// <inheritdoc/>
  221. public override void Assign(Base source)
  222. {
  223. base.Assign(source);
  224. GaugeObject src = source as GaugeObject;
  225. Maximum = src.Maximum;
  226. Minimum = src.Minimum;
  227. Value = src.Value;
  228. Expression = src.Expression;
  229. Scale.Assign(src.Scale);
  230. Pointer.Assign(src.Pointer);
  231. Label.Assign(src.Label);
  232. }
  233. /// <summary>
  234. /// Draws the gauge.
  235. /// </summary>
  236. /// <param name="e">Draw event arguments.</param>
  237. public override void Draw(FRPaintEventArgs e)
  238. {
  239. base.Draw(e);
  240. scale.Draw(e);
  241. pointer.Draw(e);
  242. Border.Draw(e, new RectangleF(AbsLeft, AbsTop, Width, Height));
  243. }
  244. /// <inheritdoc/>
  245. public override void Serialize(FRWriter writer)
  246. {
  247. GaugeObject c = writer.DiffObject as GaugeObject;
  248. base.Serialize(writer);
  249. if (Maximum != c.Maximum)
  250. {
  251. writer.WriteDouble("Maximum", Maximum);
  252. }
  253. if (Minimum != c.Minimum)
  254. {
  255. writer.WriteDouble("Minimum", Minimum);
  256. }
  257. if (Value != c.Value)
  258. {
  259. writer.WriteDouble("Value", Value);
  260. }
  261. if (Expression != c.Expression)
  262. {
  263. writer.WriteStr("Expression", Expression);
  264. }
  265. if (Scale != c.Scale)
  266. {
  267. Scale.Serialize(writer, "Scale", c.Scale);
  268. }
  269. if (Pointer != c.Pointer)
  270. {
  271. Pointer.Serialize(writer, "Pointer", c.Pointer);
  272. }
  273. if (Label != c.Label)
  274. {
  275. Label.Serialize(writer, "Label", c.Label);
  276. }
  277. }
  278. /// <summary>
  279. /// Clone Gauge Object
  280. /// </summary>
  281. /// <returns> clone of this object</returns>
  282. public GaugeObject Clone()
  283. {
  284. var clone = Activator.CreateInstance(this.GetType()) as GaugeObject;
  285. clone.Assign(this);
  286. return clone;
  287. }
  288. #endregion // Public Methods
  289. }
  290. }