SimpleScale.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using FastReport.Utils;
  6. namespace FastReport.Gauge.Simple
  7. {
  8. /// <summary>
  9. /// Represents a simple scale.
  10. /// </summary>
  11. #if !DEBUG
  12. [DesignTimeVisible(false)]
  13. #endif
  14. public class SimpleScale : GaugeScale
  15. {
  16. #region Fields
  17. private float left;
  18. private float top;
  19. private float height;
  20. private float width;
  21. private int majorTicksNum;
  22. private SimpleSubScale firstSubScale;
  23. private SimpleSubScale secondSubScale;
  24. private float pointerWidthOffset;
  25. private float pointerHeightOffset;
  26. #endregion // Fields
  27. #region Properties
  28. /// <summary>
  29. /// Gets or sets the first subscale (top or left).
  30. /// </summary>
  31. [Browsable(true)]
  32. public virtual SimpleSubScale FirstSubScale
  33. {
  34. get { return firstSubScale; }
  35. set { firstSubScale = value; }
  36. }
  37. /// <summary>
  38. /// Gets or sets the second subscale (right or bottom).
  39. /// </summary>
  40. [Browsable(true)]
  41. public virtual SimpleSubScale SecondSubScale
  42. {
  43. get { return secondSubScale; }
  44. set { secondSubScale = value; }
  45. }
  46. #endregion // Properties
  47. #region Constructors
  48. /// <summary>
  49. /// Initializes a new instance of the <see cref="SimpleScale"/> class.
  50. /// </summary>
  51. /// <param name="parent">The parent gauge object.</param>
  52. public SimpleScale(GaugeObject parent) : base(parent)
  53. {
  54. MajorTicks = new ScaleTicks(10, 2, Color.Black);
  55. MinorTicks = new ScaleTicks(6, 1, Color.Black);
  56. majorTicksNum = 6;
  57. firstSubScale = new SimpleSubScale();
  58. secondSubScale = new SimpleSubScale();
  59. }
  60. #endregion // Constructors
  61. #region Private Methods
  62. private void DrawMajorTicksHorz(FRPaintEventArgs e)
  63. {
  64. IGraphics g = e.Graphics;
  65. Pen pen = e.Cache.GetPen(MajorTicks.Color, MajorTicks.Width * e.ScaleX, DashStyle.Solid);
  66. Brush brush = TextFill.CreateBrush(new RectangleF(Parent.AbsLeft, Parent.AbsTop, Parent.Width, Parent.Height), e.ScaleX, e.ScaleY);
  67. pointerHeightOffset = (Parent.Pointer as SimplePointer).Height / 2 + Parent.Pointer.BorderWidth * 2 * e.ScaleY;
  68. float x = left;
  69. float y1 = top;
  70. float y2 = top + height / 2 - pointerHeightOffset;
  71. float y3 = top + height / 2 + pointerHeightOffset;
  72. float y4 = top + height;
  73. float step = width / (majorTicksNum - 1);
  74. int textStep = (int)((Parent.Maximum - Parent.Minimum) / (majorTicksNum - 1));
  75. Font font = e.Cache.GetFont(Font.FontFamily, Parent.IsPrinting ? Font.Size : Font.Size * e.ScaleX * 96f / DrawUtils.ScreenDpi, Font.Style);
  76. string text = Parent.Minimum.ToString();
  77. if (firstSubScale.Enabled)
  78. {
  79. for (int i = 0; i < majorTicksNum; i++)
  80. {
  81. g.DrawLine(pen, x, y1, x, y2);
  82. if (firstSubScale.ShowCaption)
  83. {
  84. SizeF strSize = g.MeasureString(text, Font);
  85. g.DrawString(text, font, brush, x - strSize.Width / 2 * e.ScaleX / (DrawUtils.ScreenDpi / 96f), y1 - 0.4f * Units.Centimeters * e.ScaleY);
  86. text = Convert.ToString(textStep * (i + 1) + Parent.Minimum);
  87. }
  88. x += step;
  89. }
  90. }
  91. x = left;
  92. text = Parent.Minimum.ToString();
  93. if (secondSubScale.Enabled)
  94. {
  95. for (int i = 0; i < majorTicksNum; i++)
  96. {
  97. g.DrawLine(pen, x, y3, x, y4);
  98. if (secondSubScale.ShowCaption)
  99. {
  100. SizeF strSize = g.MeasureString(text, Font);
  101. g.DrawString(text, font, brush, x - strSize.Width / 2 * e.ScaleX / (DrawUtils.ScreenDpi / 96f), y4 + 0.08f * Units.Centimeters * e.ScaleY);
  102. text = Convert.ToString(textStep * (i + 1) + Parent.Minimum);
  103. }
  104. x += step;
  105. }
  106. }
  107. brush.Dispose();
  108. }
  109. private void DrawMinorTicksHorz(FRPaintEventArgs e)
  110. {
  111. IGraphics g = e.Graphics;
  112. Pen pen = e.Cache.GetPen(MinorTicks.Color, MinorTicks.Width * e.ScaleX, DashStyle.Solid);
  113. pointerHeightOffset = (Parent.Pointer as SimplePointer).Height / 2 + Parent.Pointer.BorderWidth * 2 * e.ScaleY;
  114. float x = left;
  115. float y1 = top + height * 0.15f;
  116. float y2 = top + height / 2 - pointerHeightOffset;
  117. float y3 = top + height / 2 + pointerHeightOffset;
  118. float y4 = top + height - height * 0.15f;
  119. float step = width / (majorTicksNum - 1) / 4;
  120. if (firstSubScale.Enabled)
  121. {
  122. for (int i = 0; i < majorTicksNum - 1; i++)
  123. {
  124. x += step;
  125. for (int j = 0; j < 3; j++)
  126. {
  127. g.DrawLine(pen, x, y1, x, y2);
  128. x += step;
  129. }
  130. }
  131. }
  132. x = left;
  133. if (secondSubScale.Enabled)
  134. {
  135. for (int i = 0; i < majorTicksNum - 1; i++)
  136. {
  137. x += step;
  138. for (int j = 0; j < 3; j++)
  139. {
  140. g.DrawLine(pen, x, y3, x, y4);
  141. x += step;
  142. }
  143. }
  144. }
  145. }
  146. private void DrawMajorTicksVert(FRPaintEventArgs e)
  147. {
  148. IGraphics g = e.Graphics;
  149. Pen pen = e.Cache.GetPen(MajorTicks.Color, MajorTicks.Width * e.ScaleY, DashStyle.Solid);
  150. Brush brush = TextFill.CreateBrush(new RectangleF(Parent.AbsLeft * e.ScaleX, Parent.AbsTop * e.ScaleY,
  151. Parent.Width * e.ScaleX, Parent.Height * e.ScaleY), e.ScaleX, e.ScaleY);
  152. pointerWidthOffset = (Parent.Pointer as SimplePointer).Width / 2 + Parent.Pointer.BorderWidth * 2 * e.ScaleX;
  153. float y = top + height;
  154. float x1 = left;
  155. float x2 = left + width / 2 - pointerWidthOffset;
  156. float x3 = left + width / 2 + pointerWidthOffset;
  157. float x4 = left + width;
  158. float step = height / (majorTicksNum - 1);
  159. int textStep = (int)((Parent.Maximum - Parent.Minimum) / (majorTicksNum - 1));
  160. Font font = e.Cache.GetFont(Font.FontFamily, Parent.IsPrinting ? Font.Size : Font.Size * e.ScaleX * 96f / DrawUtils.ScreenDpi, Font.Style);
  161. string text = Parent.Minimum.ToString();
  162. if (firstSubScale.Enabled)
  163. {
  164. for (int i = 0; i < majorTicksNum; i++)
  165. {
  166. g.DrawLine(pen, x1, y, x2, y);
  167. if (firstSubScale.ShowCaption)
  168. {
  169. SizeF strSize = g.MeasureString(text, Font);
  170. g.DrawString(text, font, brush, x1 - strSize.Width * e.ScaleX / (DrawUtils.ScreenDpi / 96f) - 0.04f * Units.Centimeters * e.ScaleX, y - strSize.Height / 2 * e.ScaleY / (DrawUtils.ScreenDpi / 96f));
  171. text = Convert.ToString(textStep * (i + 1) + Parent.Minimum);
  172. }
  173. y -= step;
  174. }
  175. }
  176. y = top + height;
  177. text = Parent.Minimum.ToString();
  178. if (secondSubScale.Enabled)
  179. {
  180. for (int i = 0; i < majorTicksNum; i++)
  181. {
  182. g.DrawLine(pen, x3, y, x4, y);
  183. if (secondSubScale.ShowCaption)
  184. {
  185. SizeF strSize = g.MeasureString(text, Font);
  186. g.DrawString(text, font, brush, x4 + 0.04f * Units.Centimeters * e.ScaleX, y - strSize.Height / 2 * e.ScaleY / (DrawUtils.ScreenDpi / 96f));
  187. text = Convert.ToString(textStep * (i + 1) + Parent.Minimum);
  188. }
  189. y -= step;
  190. }
  191. }
  192. brush.Dispose();
  193. }
  194. private void DrawMinorTicksVert(FRPaintEventArgs e)
  195. {
  196. IGraphics g = e.Graphics;
  197. Pen pen = e.Cache.GetPen(MinorTicks.Color, MinorTicks.Width * e.ScaleY, DashStyle.Solid);
  198. pointerWidthOffset = (Parent.Pointer as SimplePointer).Width / 2 + Parent.Pointer.BorderWidth * 2 * e.ScaleX;
  199. float y = top + height;
  200. float x1 = left + width * 0.15f;
  201. float x2 = left + width / 2 - pointerWidthOffset;
  202. float x3 = left + width / 2 + pointerWidthOffset;
  203. float x4 = left + width - width * 0.15f;
  204. float step = height / (majorTicksNum - 1) / 4;
  205. if (firstSubScale.Enabled)
  206. {
  207. for (int i = 0; i < majorTicksNum - 1; i++)
  208. {
  209. y -= step;
  210. for (int j = 0; j < 3; j++)
  211. {
  212. g.DrawLine(pen, x1, y, x2, y);
  213. y -= step;
  214. }
  215. }
  216. }
  217. y = top + height;
  218. if (secondSubScale.Enabled)
  219. {
  220. for (int i = 0; i < majorTicksNum - 1; i++)
  221. {
  222. y -= step;
  223. for (int j = 0; j < 3; j++)
  224. {
  225. g.DrawLine(pen, x3, y, x4, y);
  226. y -= step;
  227. }
  228. }
  229. }
  230. }
  231. #endregion // Private Methods
  232. #region Public Methods
  233. /// <inheritdoc/>
  234. public override void Assign(GaugeScale src)
  235. {
  236. base.Assign(src);
  237. SimpleScale s = src as SimpleScale;
  238. MajorTicks.Assign(s.MajorTicks);
  239. MinorTicks.Assign(s.MinorTicks);
  240. FirstSubScale.Assign(s.FirstSubScale);
  241. SecondSubScale.Assign(s.SecondSubScale);
  242. }
  243. /// <inheritdoc/>
  244. public override void Draw(FastReport.Utils.FRPaintEventArgs e)
  245. {
  246. base.Draw(e);
  247. if (Parent.Vertical)
  248. {
  249. left = (Parent.AbsLeft + 0.7f * Units.Centimeters) * e.ScaleX;
  250. top = (Parent.AbsTop + 0.5f * Units.Centimeters) * e.ScaleY;
  251. height = (Parent.Height - 1.0f * Units.Centimeters) * e.ScaleY;
  252. width = (Parent.Width - 1.4f * Units.Centimeters) * e.ScaleX;
  253. DrawMajorTicksVert(e);
  254. DrawMinorTicksVert(e);
  255. }
  256. else
  257. {
  258. left = (Parent.AbsLeft + 0.5f * Units.Centimeters) * e.ScaleX;
  259. top = (Parent.AbsTop + 0.6f * Units.Centimeters) * e.ScaleY;
  260. height = (Parent.Height - 1.2f * Units.Centimeters) * e.ScaleY;
  261. width = (Parent.Width - 1.0f * Units.Centimeters) * e.ScaleX;
  262. DrawMajorTicksHorz(e);
  263. DrawMinorTicksHorz(e);
  264. }
  265. }
  266. /// <inheritdoc/>
  267. public override void Serialize(FRWriter writer, string prefix, GaugeScale diff)
  268. {
  269. base.Serialize(writer, prefix, diff);
  270. SimpleScale dc = diff as SimpleScale;
  271. MajorTicks.Serialize(writer, prefix + ".MajorTicks", dc.MajorTicks);
  272. MinorTicks.Serialize(writer, prefix + ".MinorTicks", dc.MinorTicks);
  273. FirstSubScale.Serialize(writer, prefix + ".FirstSubScale", dc.FirstSubScale);
  274. SecondSubScale.Serialize(writer, prefix + ".SecondSubScale", dc.SecondSubScale);
  275. }
  276. #endregion // Public Methods
  277. }
  278. /// <summary>
  279. /// Represent the subscale of simple scale.
  280. /// </summary>
  281. [ToolboxItem(false)]
  282. public class SimpleSubScale : Component
  283. {
  284. #region Fields
  285. private bool enabled;
  286. private bool showCaption;
  287. #endregion // Fields
  288. #region Properties
  289. /// <summary>
  290. /// Gets or sets a value that specifies enabled subscale or not.
  291. /// </summary>
  292. [Browsable(true)]
  293. public bool Enabled
  294. {
  295. get { return enabled; }
  296. set { enabled = value; }
  297. }
  298. /// <summary>
  299. /// Gets or sets a value that specifies show caption or not.
  300. /// </summary>
  301. [Browsable(true)]
  302. public bool ShowCaption
  303. {
  304. get { return showCaption; }
  305. set { showCaption = value; }
  306. }
  307. #endregion // Properties
  308. #region Constructors
  309. /// <summary>
  310. /// Initializes a new instance of the <see cref="SimpleSubScale"/> class.
  311. /// </summary>
  312. public SimpleSubScale()
  313. {
  314. enabled = true;
  315. showCaption = true;
  316. }
  317. #endregion // Constructors
  318. #region Public Methods
  319. /// <summary>
  320. /// Copies the contents of another SimpleSubScale.
  321. /// </summary>
  322. /// <param name="src">The SimpleSubScale instance to copy the contents from.</param>
  323. public virtual void Assign(SimpleSubScale src)
  324. {
  325. Enabled = src.Enabled;
  326. ShowCaption = src.ShowCaption;
  327. }
  328. /// <summary>
  329. /// Serializes the SimpleSubScale.
  330. /// </summary>
  331. /// <param name="writer">Writer object.</param>
  332. /// <param name="prefix">SimpleSubScale property name.</param>
  333. /// <param name="diff">Another SimpleSubScale to compare with.</param>
  334. /// <remarks>
  335. /// This method is for internal use only.
  336. /// </remarks>
  337. public virtual void Serialize(FRWriter writer, string prefix, SimpleSubScale diff)
  338. {
  339. if (Enabled != diff.Enabled)
  340. {
  341. writer.WriteBool(prefix + ".Enabled", Enabled);
  342. }
  343. if (ShowCaption != diff.ShowCaption)
  344. {
  345. writer.WriteBool(prefix + ".ShowCaption", ShowCaption);
  346. }
  347. }
  348. #endregion // Public Methods
  349. }
  350. }