UCWaveChart.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-22-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCWaveChart.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.ComponentModel;
  19. using System.Drawing;
  20. using System.Drawing.Drawing2D;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Windows.Forms;
  24. namespace HZH_Controls.Controls
  25. {
  26. /// <summary>
  27. /// Class UCWaveChart.
  28. /// Implements the <see cref="HZH_Controls.Controls.UCControlBase" />
  29. /// </summary>
  30. /// <seealso cref="HZH_Controls.Controls.UCControlBase" />
  31. public class UCWaveChart : UCControlBase
  32. {
  33. /// <summary>
  34. /// The m wave actual width
  35. /// </summary>
  36. private int m_waveActualWidth = 50;
  37. /// <summary>
  38. /// The m wave width
  39. /// </summary>
  40. private int m_waveWidth = 50;
  41. /// <summary>
  42. /// Gets or sets the width of the wave.
  43. /// </summary>
  44. /// <value>The width of the wave.</value>
  45. [Description("波形宽度"), Category("自定义")]
  46. public int WaveWidth
  47. {
  48. get { return m_waveWidth; }
  49. set
  50. {
  51. if (value <= 0)
  52. return;
  53. m_waveWidth = value;
  54. ResetWaveCount();
  55. Refresh();
  56. }
  57. }
  58. /// <summary>
  59. /// The m sleep time
  60. /// </summary>
  61. private int m_sleepTime = 1000;
  62. /// <summary>
  63. /// 波运行速度(运行时间间隔,毫秒)
  64. /// </summary>
  65. /// <value>The sleep time.</value>
  66. [Description("运行速度(运行时间间隔,毫秒)"), Category("自定义")]
  67. public int SleepTime
  68. {
  69. get { return m_sleepTime; }
  70. set
  71. {
  72. if (value <= 0)
  73. return;
  74. m_sleepTime = value;
  75. if (timer != null)
  76. {
  77. timer.Enabled = false;
  78. timer.Interval = value;
  79. timer.Enabled = true;
  80. }
  81. }
  82. }
  83. /// <summary>
  84. /// The m line tension
  85. /// </summary>
  86. private float m_lineTension = 0.5f;
  87. /// <summary>
  88. /// 线弯曲程度
  89. /// </summary>
  90. /// <value>The line tension.</value>
  91. [Description("线弯曲程度(0-1)"), Category("自定义")]
  92. public float LineTension
  93. {
  94. get { return m_lineTension; }
  95. set
  96. {
  97. if (!(value >= 0 && value <= 1))
  98. {
  99. return;
  100. }
  101. m_lineTension = value;
  102. Refresh();
  103. }
  104. }
  105. /// <summary>
  106. /// The m line color
  107. /// </summary>
  108. private Color m_lineColor = Color.FromArgb(150, 255, 77, 59);
  109. /// <summary>
  110. /// Gets or sets the color of the line.
  111. /// </summary>
  112. /// <value>The color of the line.</value>
  113. [Description("曲线颜色"), Category("自定义")]
  114. public Color LineColor
  115. {
  116. get { return m_lineColor; }
  117. set
  118. {
  119. m_lineColor = value;
  120. Refresh();
  121. }
  122. }
  123. /// <summary>
  124. /// The m grid line color
  125. /// </summary>
  126. private Color m_gridLineColor = Color.FromArgb(50, 255, 77, 59);
  127. /// <summary>
  128. /// Gets or sets the color of the grid line.
  129. /// </summary>
  130. /// <value>The color of the grid line.</value>
  131. [Description("网格线颜色"), Category("自定义")]
  132. public Color GridLineColor
  133. {
  134. get { return m_gridLineColor; }
  135. set
  136. {
  137. m_gridLineColor = value;
  138. Refresh();
  139. }
  140. }
  141. /// <summary>
  142. /// The m grid line text color
  143. /// </summary>
  144. private Color m_gridLineTextColor = Color.FromArgb(150, 255, 77, 59);
  145. /// <summary>
  146. /// Gets or sets the color of the grid line text.
  147. /// </summary>
  148. /// <value>The color of the grid line text.</value>
  149. [Description("网格文本颜色"), Category("自定义")]
  150. public Color GridLineTextColor
  151. {
  152. get { return m_gridLineTextColor; }
  153. set
  154. {
  155. m_gridLineTextColor = value;
  156. Refresh();
  157. }
  158. }
  159. /// <summary>
  160. /// 获取或设置控件显示的文字的字体。
  161. /// </summary>
  162. /// <value>The font.</value>
  163. /// <PermissionSet>
  164. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  165. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  166. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
  167. /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  168. /// </PermissionSet>
  169. public override Font Font
  170. {
  171. get
  172. {
  173. return base.Font;
  174. }
  175. set
  176. {
  177. base.Font = value;
  178. }
  179. }
  180. /// <summary>
  181. /// 数据源,用以缓存所有需要显示的数据
  182. /// </summary>
  183. List<KeyValuePair<string, double>> m_dataSource = new List<KeyValuePair<string, double>>();
  184. /// <summary>
  185. /// 当前需要显示的数据
  186. /// </summary>
  187. List<KeyValuePair<string, double>> m_currentSource = new List<KeyValuePair<string, double>>();
  188. /// <summary>
  189. /// The timer
  190. /// </summary>
  191. Timer timer = new Timer();
  192. /// <summary>
  193. /// 画图区域
  194. /// </summary>
  195. Rectangle m_drawRect;
  196. /// <summary>
  197. /// The m wave count
  198. /// </summary>
  199. int m_waveCount = 0;
  200. /// <summary>
  201. /// Initializes a new instance of the <see cref="UCWaveChart" /> class.
  202. /// </summary>
  203. public UCWaveChart()
  204. {
  205. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  206. this.SetStyle(ControlStyles.DoubleBuffer, true);
  207. this.SetStyle(ControlStyles.ResizeRedraw, true);
  208. this.SetStyle(ControlStyles.Selectable, true);
  209. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  210. this.SetStyle(ControlStyles.UserPaint, true);
  211. this.SizeChanged += UCWaveWithSource_SizeChanged;
  212. this.IsShowRect = true;
  213. this.RectColor = Color.FromArgb(232, 232, 232);
  214. this.FillColor = Color.FromArgb(50, 255, 77, 59);
  215. this.RectWidth = 1;
  216. this.ConerRadius = 10;
  217. this.IsRadius = true;
  218. this.Size = new Size(300, 200);
  219. timer.Interval = m_sleepTime;
  220. timer.Tick += timer_Tick;
  221. this.VisibleChanged += UCWave_VisibleChanged;
  222. }
  223. /// <summary>
  224. /// 添加需要显示的数据
  225. /// </summary>
  226. /// <param name="key">名称</param>
  227. /// <param name="value">值</param>
  228. public void AddSource(string key, double value)
  229. {
  230. m_dataSource.Add(new KeyValuePair<string, double>(key, value));
  231. }
  232. /// <summary>
  233. /// Handles the VisibleChanged event of the UCWave control.
  234. /// </summary>
  235. /// <param name="sender">The source of the event.</param>
  236. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  237. void UCWave_VisibleChanged(object sender, EventArgs e)
  238. {
  239. if (!DesignMode)
  240. {
  241. timer.Enabled = this.Visible;
  242. }
  243. }
  244. /// <summary>
  245. /// Handles the Tick event of the timer control.
  246. /// </summary>
  247. /// <param name="sender">The source of the event.</param>
  248. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  249. void timer_Tick(object sender, EventArgs e)
  250. {
  251. m_currentSource = GetCurrentList();
  252. m_dataSource.RemoveAt(0);
  253. this.Refresh();
  254. }
  255. /// <summary>
  256. /// Handles the SizeChanged event of the UCWaveWithSource control.
  257. /// </summary>
  258. /// <param name="sender">The source of the event.</param>
  259. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  260. void UCWaveWithSource_SizeChanged(object sender, EventArgs e)
  261. {
  262. m_drawRect = new Rectangle(60, 20, this.Width - 80, this.Height - 60);
  263. ResetWaveCount();
  264. }
  265. /// <summary>
  266. /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
  267. /// </summary>
  268. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
  269. protected override void OnPaint(PaintEventArgs e)
  270. {
  271. base.OnPaint(e);
  272. var g = e.Graphics;
  273. g.SetGDIHigh();
  274. int intLineSplit = m_drawRect.Height / 4;
  275. for (int i = 0; i <= 4; i++)
  276. {
  277. var pen = new Pen(new SolidBrush(m_gridLineColor), 1);
  278. // pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
  279. g.DrawLine(pen, m_drawRect.Left, m_drawRect.Bottom - 1 - i * intLineSplit, m_drawRect.Right, m_drawRect.Bottom - 1 - i * intLineSplit);
  280. }
  281. if (m_currentSource == null || m_currentSource.Count <= 0)
  282. {
  283. for (int i = 0; i <= 4; i++)
  284. {
  285. string strText = (100 / 4 * i).ToString();
  286. System.Drawing.SizeF _numSize = g.MeasureString(strText, this.Font);
  287. g.DrawString(strText, Font, new SolidBrush(m_gridLineTextColor), m_drawRect.Left - _numSize.Width - 1, m_drawRect.Bottom - 1 - i * intLineSplit - (_numSize.Height / 2));
  288. }
  289. return;
  290. }
  291. List<Point> lst1 = new List<Point>();
  292. double dblValue = m_currentSource.Max(p => p.Value);
  293. int intValue = (int)dblValue;
  294. int intDivisor = ("1".PadRight(intValue.ToString().Length - 1, '0')).ToInt();
  295. if (intDivisor < 100)
  296. intDivisor = 100;
  297. int intTop = intValue;
  298. if (intValue % intDivisor != 0)
  299. {
  300. intTop = (intValue / intDivisor + 1) * intDivisor;
  301. }
  302. if (intTop == 0)
  303. intTop = 100;
  304. for (int i = 0; i <= 4; i++)
  305. {
  306. string strText = (intTop / 4 * i).ToString();
  307. System.Drawing.SizeF _numSize = g.MeasureString(strText, this.Font);
  308. g.DrawString(strText, Font, new SolidBrush(m_gridLineTextColor), m_drawRect.Left - _numSize.Width - 1, m_drawRect.Bottom - 1 - i * intLineSplit - (_numSize.Height / 2));
  309. }
  310. int intEndX = 0;
  311. int intEndY = 0;
  312. for (int i = 0; i < m_currentSource.Count; i++)
  313. {
  314. intEndX = i * m_waveActualWidth + m_drawRect.X;
  315. intEndY = m_drawRect.Bottom - 1 - (int)(m_currentSource[i].Value / intTop * m_drawRect.Height);
  316. lst1.Add(new Point(intEndX, intEndY));
  317. if (!string.IsNullOrEmpty(m_currentSource[i].Key))
  318. {
  319. System.Drawing.SizeF _numSize = g.MeasureString(m_currentSource[i].Key, this.Font);
  320. int txtX = intEndX - (int)(_numSize.Width / 2) + 1;
  321. g.DrawString(m_currentSource[i].Key, Font, new SolidBrush(m_gridLineTextColor), new PointF(txtX, m_drawRect.Bottom + 5));
  322. }
  323. }
  324. int intFirstY = m_drawRect.Bottom - 1 - (int)(m_currentSource[0].Value / intTop * m_drawRect.Height);
  325. GraphicsPath path1 = new GraphicsPath();
  326. path1.AddCurve(lst1.ToArray(), m_lineTension);
  327. g.DrawPath(new Pen(new SolidBrush(m_lineColor), 1), path1);
  328. }
  329. /// <summary>
  330. /// 得到当前需要画图的数据
  331. /// </summary>
  332. /// <returns>List&lt;KeyValuePair&lt;System.String, System.Double&gt;&gt;.</returns>
  333. private List<KeyValuePair<string, double>> GetCurrentList()
  334. {
  335. if (m_dataSource.Count < m_waveCount)
  336. {
  337. int intCount = m_waveCount - m_dataSource.Count;
  338. for (int i = 0; i < intCount; i++)
  339. {
  340. m_dataSource.Add(new KeyValuePair<string, double>("", 0));
  341. }
  342. }
  343. var lst = m_dataSource.GetRange(0, m_waveCount);
  344. if (lst.Count == 1)
  345. lst.Insert(0, new KeyValuePair<string, double>("", 0));
  346. return lst;
  347. }
  348. /// <summary>
  349. /// 计算需要显示的个数
  350. /// </summary>
  351. private void ResetWaveCount()
  352. {
  353. m_waveCount = m_drawRect.Width / m_waveWidth;
  354. m_waveActualWidth = m_waveWidth + (m_drawRect.Width % m_waveWidth) / m_waveCount;
  355. m_waveCount++;
  356. if (m_dataSource.Count < m_waveCount)
  357. {
  358. int intCount = m_waveCount - m_dataSource.Count;
  359. for (int i = 0; i < intCount; i++)
  360. {
  361. m_dataSource.Insert(0, new KeyValuePair<string, double>("", 0));
  362. }
  363. }
  364. }
  365. }
  366. }