LinearScale.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.ComponentModel;
  5. using FastReport.Utils;
  6. namespace FastReport.Gauge.Linear
  7. {
  8. /// <summary>
  9. /// Represents a linear scale.
  10. /// </summary>
  11. #if !DEBUG
  12. [DesignTimeVisible(false)]
  13. #endif
  14. public class LinearScale : 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. #endregion // Fields
  23. #region Properties
  24. #endregion // Properties
  25. #region Constructors
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="LinearScale"/> class.
  28. /// </summary>
  29. /// <param name="parent">The parent gauge object.</param>
  30. public LinearScale(GaugeObject parent) : base(parent)
  31. {
  32. MajorTicks = new ScaleTicks(10, 2, Color.Black);
  33. MinorTicks = new ScaleTicks(6, 1, Color.Black);
  34. majorTicksNum = 6;
  35. }
  36. #endregion // Constructors
  37. #region Private Methods
  38. private void DrawMajorTicksHorz(FRPaintEventArgs e)
  39. {
  40. IGraphics g = e.Graphics;
  41. Pen pen = e.Cache.GetPen(MajorTicks.Color, MajorTicks.Width * e.ScaleX, DashStyle.Solid);
  42. Brush brush = TextFill.CreateBrush(new RectangleF(Parent.AbsLeft * e.ScaleX, Parent.AbsTop * e.ScaleY,
  43. Parent.Width * e.ScaleX, Parent.Height * e.ScaleY), e.ScaleX, e.ScaleY);
  44. float x = left;
  45. float y1 = top;
  46. float y2 = top + height;
  47. float step = width / (majorTicksNum - 1);
  48. int textStep = (int)((Parent.Maximum - Parent.Minimum) / (majorTicksNum - 1));
  49. Font font = e.Cache.GetFont(Font.FontFamily, Parent.IsPrinting ? Font.Size : Font.Size * e.ScaleX * 96f / DrawUtils.ScreenDpi, Font.Style);
  50. string text = Parent.Minimum.ToString();
  51. float y3 = y1 - 0.4f * Units.Centimeters * e.ScaleY;
  52. if ((Parent as LinearGauge).Inverted)
  53. {
  54. y3 = y2 - g.MeasureString(text, Font).Height + 0.4f * Units.Centimeters * e.ScaleY;
  55. }
  56. for (int i = 0; i < majorTicksNum; i++)
  57. {
  58. g.DrawLine(pen, x, y1, x, y2);
  59. SizeF strSize = g.MeasureString(text, Font);
  60. g.DrawString(text, font, brush, x - strSize.Width / 2 * e.ScaleX / (DrawUtils.ScreenDpi / 96f), y3);
  61. text = Convert.ToString(textStep * (i + 1) + Parent.Minimum);
  62. x += step;
  63. }
  64. brush.Dispose();
  65. }
  66. private void DrawMinorTicksHorz(FRPaintEventArgs e)
  67. {
  68. IGraphics g = e.Graphics;
  69. Pen pen = e.Cache.GetPen(MinorTicks.Color, MinorTicks.Width * e.ScaleX, DashStyle.Solid);
  70. float x = left;
  71. float y1 = top + height * 0.2f;
  72. float y2 = top + height - height * 0.2f;
  73. float step = width / (majorTicksNum - 1) / 4;
  74. for (int i = 0; i < majorTicksNum - 1; i++)
  75. {
  76. x += step;
  77. for (int j = 0; j < 3; j++)
  78. {
  79. g.DrawLine(pen, x, y1, x, y2);
  80. x += step;
  81. }
  82. }
  83. }
  84. private void DrawMajorTicksVert(FRPaintEventArgs e)
  85. {
  86. IGraphics g = e.Graphics;
  87. Pen pen = e.Cache.GetPen(MajorTicks.Color, MajorTicks.Width * e.ScaleX, DashStyle.Solid);
  88. Brush brush = TextFill.CreateBrush(new RectangleF(Parent.AbsLeft * e.ScaleX, Parent.AbsTop * e.ScaleY,
  89. Parent.Width * e.ScaleX, Parent.Height * e.ScaleY), e.ScaleX, e.ScaleY);
  90. float y = top + height;
  91. float x1 = left;
  92. float x2 = left + width;
  93. float step = height / (majorTicksNum - 1);
  94. int textStep = (int)((Parent.Maximum - Parent.Minimum) / (majorTicksNum - 1));
  95. Font font = e.Cache.GetFont(Font.FontFamily, Parent.IsPrinting ? Font.Size : Font.Size * e.ScaleX * 96f / DrawUtils.ScreenDpi, Font.Style);
  96. string text = Parent.Minimum.ToString();
  97. for (int i = 0; i < majorTicksNum; i++)
  98. {
  99. g.DrawLine(pen, x1, y, x2, y);
  100. SizeF strSize = g.MeasureString(text, Font);
  101. float x3 = x1 - strSize.Width * e.ScaleX / (DrawUtils.ScreenDpi / 96f) - 0.04f * Units.Centimeters * e.ScaleX;
  102. if ((Parent as LinearGauge).Inverted)
  103. {
  104. x3 = x2 + 0.04f * Units.Centimeters * e.ScaleX;
  105. }
  106. g.DrawString(text, font, brush, x3, y - strSize.Height / 2 * e.ScaleY / (DrawUtils.ScreenDpi / 96f));
  107. text = Convert.ToString(textStep * (i + 1) + Parent.Minimum);
  108. y -= step;
  109. }
  110. brush.Dispose();
  111. }
  112. private void DrawMinorTicksVert(FRPaintEventArgs e)
  113. {
  114. IGraphics g = e.Graphics;
  115. Pen pen = e.Cache.GetPen(MinorTicks.Color, MinorTicks.Width * e.ScaleX, DashStyle.Solid);
  116. float y = top + height;
  117. float x1 = left + width * 0.2f;
  118. float x2 = left + width - width * 0.2f;
  119. float step = height / (majorTicksNum - 1) / 4;
  120. for (int i = 0; i < majorTicksNum - 1; i++)
  121. {
  122. y -= step;
  123. for (int j = 0; j < 3; j++)
  124. {
  125. g.DrawLine(pen, x1, y, x2, y);
  126. y -= step;
  127. }
  128. }
  129. }
  130. #endregion // Private Methods
  131. #region Public Methods
  132. /// <inheritdoc/>
  133. public override void Assign(GaugeScale src)
  134. {
  135. base.Assign(src);
  136. LinearScale s = src as LinearScale;
  137. MajorTicks.Assign(s.MajorTicks);
  138. MinorTicks.Assign(s.MinorTicks);
  139. }
  140. /// <inheritdoc/>
  141. public override void Draw(FRPaintEventArgs e)
  142. {
  143. base.Draw(e);
  144. if (Parent.Vertical)
  145. {
  146. left = (Parent.AbsLeft + 0.7f * Units.Centimeters) * e.ScaleX;
  147. top = (Parent.AbsTop + 0.5f * Units.Centimeters) * e.ScaleY;
  148. height = (Parent.Height - 1.0f * Units.Centimeters) * e.ScaleY;
  149. width = (Parent.Width - 1.4f * Units.Centimeters) * e.ScaleX;
  150. DrawMajorTicksVert(e);
  151. DrawMinorTicksVert(e);
  152. }
  153. else
  154. {
  155. left = (Parent.AbsLeft + 0.5f * Units.Centimeters) * e.ScaleX;
  156. top = (Parent.AbsTop + 0.6f * Units.Centimeters) * e.ScaleY;
  157. height = (Parent.Height - 1.2f * Units.Centimeters) * e.ScaleY;
  158. width = (Parent.Width - 1.0f * Units.Centimeters) * e.ScaleX;
  159. DrawMajorTicksHorz(e);
  160. DrawMinorTicksHorz(e);
  161. }
  162. }
  163. /// <inheritdoc/>
  164. public override void Serialize(FRWriter writer, string prefix, GaugeScale diff)
  165. {
  166. base.Serialize(writer, prefix, diff);
  167. LinearScale dc = diff as LinearScale;
  168. MajorTicks.Serialize(writer, prefix + ".MajorTicks", dc.MajorTicks);
  169. MinorTicks.Serialize(writer, prefix + ".MinorTicks", dc.MinorTicks);
  170. }
  171. #endregion // Public Methods
  172. }
  173. }