RadialGauge.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Drawing.Design;
  5. using System.Drawing.Drawing2D;
  6. using FastReport.Utils;
  7. namespace FastReport.Gauge.Radial
  8. {
  9. #region Enums
  10. /// <summary>
  11. /// Radial Gauge types
  12. /// </summary>
  13. [Flags]
  14. public enum RadialGaugeType
  15. {
  16. /// <summary>
  17. /// Full sized gauge
  18. /// </summary>
  19. Circle = 1,
  20. /// <summary>
  21. /// Half of the radial gauge
  22. /// </summary>
  23. Semicircle = 2,
  24. /// <summary>
  25. /// Quarter of the radial gauge
  26. /// </summary>
  27. Quadrant = 4
  28. }
  29. /// <summary>
  30. /// Radial Gauge position types
  31. /// </summary>
  32. [Flags]
  33. [TypeConverter(typeof(FastReport.TypeConverters.FlagConverter))]
  34. public enum RadialGaugePosition
  35. {
  36. /// <summary>
  37. /// None
  38. /// </summary>
  39. None = 0,
  40. /// <summary>
  41. /// Top
  42. /// </summary>
  43. Top = 1,
  44. /// <summary>
  45. /// Bottom
  46. /// </summary>
  47. Bottom = 2,
  48. /// <summary>
  49. /// Left
  50. /// </summary>
  51. Left = 4,
  52. /// <summary>
  53. /// Right
  54. /// </summary>
  55. Right = 8
  56. }
  57. #endregion // Enums
  58. /// <summary>
  59. /// Represents a linear gauge.
  60. /// </summary>
  61. public partial class RadialGauge : GaugeObject
  62. {
  63. private const double RAD = Math.PI / 180.0;
  64. private PointF center;
  65. private RadialGaugeType type;
  66. private RadialGaugePosition position;
  67. private float semicircleOffsetRatio;
  68. #region Properties
  69. /// <inheritdoc/>
  70. public override float Width
  71. {
  72. get { return base.Width; }
  73. set
  74. {
  75. base.Width = value;
  76. if (base.Height != base.Width)
  77. {
  78. base.Height = Width;
  79. }
  80. }
  81. }
  82. /// <inheritdoc/>
  83. public override float Height
  84. {
  85. get { return base.Height; }
  86. set
  87. {
  88. base.Height = value;
  89. if (base.Width != base.Height)
  90. {
  91. base.Width = Height;
  92. }
  93. }
  94. }
  95. /// <summary>
  96. /// Returns centr of the gauge
  97. /// </summary>
  98. [Browsable(false)]
  99. public PointF Center
  100. {
  101. get { return center; }
  102. set { center = value; }
  103. }
  104. /// <summary>
  105. /// The number of radians in one degree
  106. /// </summary>
  107. public static double Radians
  108. {
  109. get { return RAD; }
  110. }
  111. /// <summary>
  112. /// Gets or sets the Radial Gauge type
  113. /// </summary>
  114. [Browsable(true)]
  115. [Category("Appearance")]
  116. public RadialGaugeType Type
  117. {
  118. get { return type; }
  119. set
  120. {
  121. if (value == RadialGaugeType.Circle)
  122. {
  123. position = RadialGaugePosition.None;
  124. type = value;
  125. }
  126. if (value == RadialGaugeType.Semicircle &&
  127. !(Position == RadialGaugePosition.Bottom ||
  128. Position == RadialGaugePosition.Left ||
  129. Position == RadialGaugePosition.Right ||
  130. Position == RadialGaugePosition.Top))
  131. {
  132. position = RadialGaugePosition.Top;
  133. type = value;
  134. }
  135. else if (value == RadialGaugeType.Quadrant &&
  136. !(
  137. ((Position & RadialGaugePosition.Left) != 0 && (Position & RadialGaugePosition.Top) != 0 &&
  138. (Position & RadialGaugePosition.Right) == 0 && (Position & RadialGaugePosition.Bottom) == 0) ||
  139. ((Position & RadialGaugePosition.Right) != 0 && (Position & RadialGaugePosition.Top) != 0 &&
  140. (Position & RadialGaugePosition.Left) == 0 && (Position & RadialGaugePosition.Bottom) == 0) ||
  141. ((Position & RadialGaugePosition.Left) != 0 && (Position & RadialGaugePosition.Bottom) != 0 &&
  142. (Position & RadialGaugePosition.Right) == 0 && (Position & RadialGaugePosition.Top) == 0) ||
  143. ((Position & RadialGaugePosition.Right) != 0 && (Position & RadialGaugePosition.Bottom) != 0 &&
  144. (Position & RadialGaugePosition.Left) == 0 && (Position & RadialGaugePosition.Top) == 0)
  145. ))
  146. {
  147. position = RadialGaugePosition.Top | RadialGaugePosition.Left;
  148. type = value;
  149. }
  150. }
  151. }
  152. /// <summary>
  153. /// Gats or sets the Radial Gauge position. Doesn't work for Full Radial Gauge.
  154. /// </summary>
  155. [Category("Appearance")]
  156. [Editor("FastReport.TypeEditors.FlagsEditor, FastReport", typeof(UITypeEditor))]
  157. public RadialGaugePosition Position
  158. {
  159. get { return position; }
  160. set
  161. {
  162. if (Type == RadialGaugeType.Semicircle &&
  163. (value == RadialGaugePosition.Bottom ||
  164. value == RadialGaugePosition.Left ||
  165. value == RadialGaugePosition.Right ||
  166. value == RadialGaugePosition.Top))
  167. position = value;
  168. else if (Type == RadialGaugeType.Quadrant &&
  169. (
  170. ((value & RadialGaugePosition.Left) != 0 && (value & RadialGaugePosition.Top) != 0 &&
  171. (value & RadialGaugePosition.Right) == 0 && (value & RadialGaugePosition.Bottom) == 0) ||
  172. ((value & RadialGaugePosition.Right) != 0 && (value & RadialGaugePosition.Top) != 0 &&
  173. (value & RadialGaugePosition.Left) == 0 && (value & RadialGaugePosition.Bottom) == 0) ||
  174. ((value & RadialGaugePosition.Left) != 0 && (value & RadialGaugePosition.Bottom) != 0 &&
  175. (value & RadialGaugePosition.Right) == 0 && (value & RadialGaugePosition.Top) == 0) ||
  176. ((value & RadialGaugePosition.Right) != 0 && (value & RadialGaugePosition.Bottom) != 0 &&
  177. (value & RadialGaugePosition.Left) == 0 && (value & RadialGaugePosition.Top) == 0)
  178. ))
  179. position = value;
  180. else if (Type == RadialGaugeType.Circle)
  181. position = 0;
  182. }
  183. }
  184. /// <summary>
  185. /// Gets or sets the semicircles offset
  186. /// </summary>
  187. [Category("Appearance")]
  188. public float SemicircleOffsetRatio
  189. {
  190. get { return semicircleOffsetRatio; }
  191. set { semicircleOffsetRatio = value; }
  192. }
  193. #endregion // Properties
  194. #region Constructors
  195. /// <summary>
  196. /// Initializes a new instance of the <see cref="RadialGauge"/> class.
  197. /// </summary>
  198. public RadialGauge() : base()
  199. {
  200. InitializeComponent();
  201. Scale = new RadialScale(this);
  202. Pointer = new RadialPointer(this, Scale as RadialScale);
  203. Label = new RadialLabel(this);
  204. Height = 4.0f * Units.Centimeters;
  205. Width = 4.0f * Units.Centimeters;
  206. semicircleOffsetRatio = type == RadialGaugeType.Semicircle &&
  207. (position == RadialGaugePosition.Left || position == RadialGaugePosition.Right) ? 1.5f : 1;
  208. Type = RadialGaugeType.Circle;
  209. Border.Lines = BorderLines.None;
  210. }
  211. #endregion // Constructor
  212. #region Public Methods
  213. /// <inheritdoc/>
  214. public override void Assign(Base source)
  215. {
  216. base.Assign(source);
  217. RadialGauge src = source as RadialGauge;
  218. Type = src.Type;
  219. Position = src.Position;
  220. }
  221. /// <inheritdoc/>
  222. public override void Draw(FRPaintEventArgs e)
  223. {
  224. IGraphics g = e.Graphics;
  225. float x = (AbsLeft + Border.Width / 2) * e.ScaleX;
  226. float y = (AbsTop + Border.Width / 2) * e.ScaleY;
  227. float dx = (Width - Border.Width) * e.ScaleX - 1;
  228. float dy = (Height - Border.Width) * e.ScaleY - 1;
  229. float x1 = x + dx;
  230. float y1 = y + dy;
  231. Pen pen = e.Cache.GetPen(Border.Color, Border.Width * e.ScaleX, Border.DashStyle);
  232. Brush brush;
  233. if (Fill is SolidFill)
  234. brush = e.Cache.GetBrush((Fill as SolidFill).Color);
  235. else
  236. brush = Fill.CreateBrush(new RectangleF(x, y, dx, dy), e.ScaleX, e.ScaleY);
  237. center = new PointF(x + dx / 2, y + dy / 2);
  238. if (type == RadialGaugeType.Circle)
  239. {
  240. g.FillAndDrawEllipse(pen, brush, x, y, dx, dy);
  241. }
  242. else if (type == RadialGaugeType.Semicircle)
  243. {
  244. float semiOffset = (Width / 16f / 2f + 2f) * semicircleOffsetRatio * e.ScaleY;
  245. PointF[] points = new PointF[4];
  246. if (position == RadialGaugePosition.Top)
  247. {
  248. g.FillPie(brush, x, y, dx, dy, -180, 180);
  249. g.DrawArc(pen, x, y, dx, dy, -180, 180);
  250. PointF startPoint = RadialUtils.RotateVector(new PointF[] { new PointF(x + dx / 2, y), center }, -90 * RAD, center)[0];
  251. points[0] = new PointF(startPoint.X, startPoint.Y - 1 * e.ScaleY);
  252. points[1] = new PointF(startPoint.X, startPoint.Y + semiOffset);
  253. points[2] = new PointF(startPoint.X + dx, startPoint.Y + semiOffset);
  254. points[3] = new PointF(startPoint.X + dx, startPoint.Y - 1 * e.ScaleY);
  255. }
  256. else if (position == RadialGaugePosition.Bottom)
  257. {
  258. g.FillPie(brush, x, y, dx, dy, 0, 180);
  259. g.DrawArc(pen, x, y, dx, dy, 0, 180);
  260. PointF startPoint = RadialUtils.RotateVector(new PointF[] { new PointF(x + dx / 2, y), center }, 90 * RAD, center)[0];
  261. points[0] = new PointF(startPoint.X, startPoint.Y + 1 * e.ScaleY);
  262. points[1] = new PointF(startPoint.X, startPoint.Y - semiOffset);
  263. points[2] = new PointF(startPoint.X - dx, startPoint.Y - semiOffset);
  264. points[3] = new PointF(startPoint.X - dx, startPoint.Y + 1 * e.ScaleY);
  265. }
  266. else if (position == RadialGaugePosition.Left)
  267. {
  268. g.FillPie(brush, x, y, dx, dy, 90, 180);
  269. g.DrawArc(pen, x, y, dx, dy, 90, 180);
  270. PointF startPoint = RadialUtils.RotateVector(new PointF[] { new PointF(x + dx / 2, y), center }, 180 * RAD, center)[0];
  271. points[0] = new PointF(startPoint.X - 1 * e.ScaleX, startPoint.Y);
  272. points[1] = new PointF(startPoint.X + semiOffset, startPoint.Y);
  273. points[2] = new PointF(startPoint.X + semiOffset, startPoint.Y - dy);
  274. points[3] = new PointF(startPoint.X - 1 * e.ScaleX, startPoint.Y - dy);
  275. }
  276. else if (position == RadialGaugePosition.Right)
  277. {
  278. g.FillPie(brush, x, y, dx, dy, -90, 180);
  279. g.DrawArc(pen, x, y, dx, dy, -90, 180);
  280. PointF startPoint = RadialUtils.RotateVector(new PointF[] { new PointF(x + dx / 2, y), center }, -180 * RAD, center)[0];
  281. points[0] = new PointF(startPoint.X + 1 * e.ScaleX, startPoint.Y);
  282. points[1] = new PointF(startPoint.X - semiOffset, startPoint.Y);
  283. points[2] = new PointF(startPoint.X - semiOffset, startPoint.Y - dy);
  284. points[3] = new PointF(startPoint.X + 1 * e.ScaleX, startPoint.Y - dy);
  285. }
  286. if (position != RadialGaugePosition.None)
  287. {
  288. GraphicsPath path = new GraphicsPath();
  289. path.AddLines(points);
  290. g.FillAndDrawPath(pen, brush, path);
  291. }
  292. }
  293. else if (type == RadialGaugeType.Quadrant)
  294. {
  295. float semiOffset = (Width / 16f / 2f + 2f) * semicircleOffsetRatio * e.ScaleY;
  296. if (RadialUtils.IsTop(this) && RadialUtils.IsLeft(this))
  297. {
  298. g.FillPie(brush, x, y, dx, dy, -180, 90);
  299. g.DrawArc(pen, x, y, dx, dy, -180, 90);
  300. PointF startPoint = RadialUtils.RotateVector(new PointF[] { new PointF(x + dx / 2, y), center }, -90 * RAD, center)[0];
  301. PointF[] points = new PointF[5];
  302. points[0] = new PointF(startPoint.X, startPoint.Y - 1 * e.ScaleY);
  303. points[1] = new PointF(startPoint.X, startPoint.Y + semiOffset);
  304. points[2] = new PointF(startPoint.X + dx / 2 + semiOffset, startPoint.Y + semiOffset);
  305. points[3] = new PointF(startPoint.X + dx / 2 + semiOffset, y);
  306. points[4] = new PointF(startPoint.X + dx / 2 - 1 * e.ScaleX, y);
  307. GraphicsPath path = new GraphicsPath();
  308. path.AddLines(points);
  309. g.FillAndDrawPath(pen, brush, path);
  310. }
  311. else if (RadialUtils.IsBottom(this) && RadialUtils.IsLeft(this))
  312. {
  313. g.FillPie(brush, x, y, dx, dy, -270, 90);
  314. g.DrawArc(pen, x, y, dx, dy, -270, 90);
  315. PointF startPoint = RadialUtils.RotateVector(new PointF[] { new PointF(x + dx / 2, y), center }, -90 * RAD, center)[0];
  316. PointF[] points = new PointF[5];
  317. points[0] = new PointF(startPoint.X, startPoint.Y + 1 * e.ScaleY);
  318. points[1] = new PointF(startPoint.X, startPoint.Y - semiOffset);
  319. points[2] = new PointF(startPoint.X + dx / 2 + semiOffset, startPoint.Y - semiOffset);
  320. points[3] = new PointF(startPoint.X + dx / 2 + semiOffset, y + dy);
  321. points[4] = new PointF(x + dx / 2 - 1 * e.ScaleX, y + dy);
  322. GraphicsPath path = new GraphicsPath();
  323. path.AddLines(points);
  324. g.FillAndDrawPath(pen, brush, path);
  325. }
  326. else if (RadialUtils.IsTop(this) && RadialUtils.IsRight(this))
  327. {
  328. g.FillPie(brush, x, y, dx, dy, -90, 90);
  329. g.DrawArc(pen, x, y, dx, dy, -90, 90);
  330. PointF startPoint = RadialUtils.RotateVector(new PointF[] { new PointF(x + dx / 2, y), center }, 90 * RAD, center)[0];
  331. PointF[] points = new PointF[5];
  332. points[0] = new PointF(startPoint.X, startPoint.Y - 1 * e.ScaleY);
  333. points[1] = new PointF(startPoint.X, startPoint.Y + semiOffset);
  334. points[2] = new PointF(startPoint.X - dx / 2 - semiOffset, startPoint.Y + semiOffset);
  335. points[3] = new PointF(x + dx / 2 - semiOffset, y);
  336. points[4] = new PointF(x + dx / 2 + 1 * e.ScaleX, y);
  337. GraphicsPath path = new GraphicsPath();
  338. path.AddLines(points);
  339. g.FillAndDrawPath(pen, brush, path);
  340. }
  341. else if (RadialUtils.IsBottom(this) && RadialUtils.IsRight(this))
  342. {
  343. g.FillPie(brush, x, y, dx, dy, 0, 90);
  344. g.DrawArc(pen, x, y, dx, dy, 0, 90);
  345. PointF startPoint = RadialUtils.RotateVector(new PointF[] { new PointF(x + dx / 2, y), center }, 90 * RAD, center)[0];
  346. PointF[] points = new PointF[5];
  347. points[0] = new PointF(startPoint.X, startPoint.Y + 1 * e.ScaleY);
  348. points[1] = new PointF(startPoint.X, startPoint.Y - semiOffset);
  349. points[2] = new PointF(x + dx / 2 - semiOffset, startPoint.Y - semiOffset);
  350. points[3] = new PointF(x + dx / 2 - semiOffset, y + dy);
  351. points[4] = new PointF(x + dx / 2 + 1 * e.ScaleX, y + dy);
  352. GraphicsPath path = new GraphicsPath();
  353. path.AddLines(points);
  354. g.FillAndDrawPath(pen, brush, path);
  355. }
  356. }
  357. Scale.Draw(e);
  358. Pointer.Draw(e);
  359. Label.Draw(e);
  360. DrawMarkers(e);
  361. if (!(Fill is SolidFill))
  362. brush.Dispose();
  363. if (Report != null && Report.SmoothGraphics)
  364. {
  365. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  366. g.SmoothingMode = SmoothingMode.AntiAlias;
  367. }
  368. }
  369. /// <inheritdoc/>
  370. public override void Serialize(FRWriter writer)
  371. {
  372. RadialGauge c = writer.DiffObject as RadialGauge;
  373. base.Serialize(writer);
  374. if (Type != c.Type)
  375. {
  376. writer.WriteValue("Type", Type);
  377. }
  378. if (Position != c.Position)
  379. {
  380. writer.WriteValue("Position", Position);
  381. }
  382. }
  383. #endregion // Public Methods
  384. }
  385. }