UCFunnelChart.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 2019-09-26
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCFunnelChart.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.Linq;
  19. using System.Text;
  20. using System.Windows.Forms;
  21. using System.Drawing;
  22. using System.Drawing.Drawing2D;
  23. using System.ComponentModel;
  24. namespace HZH_Controls.Controls
  25. {
  26. /// <summary>
  27. /// Class UCFunnelChart.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. public class UCFunnelChart : UserControl
  32. {
  33. /// <summary>
  34. /// The title
  35. /// </summary>
  36. private string title;
  37. /// <summary>
  38. /// Gets or sets the title.
  39. /// </summary>
  40. /// <value>The title.</value>
  41. [Browsable(true)]
  42. [Category("自定义")]
  43. [Description("获取或设置标题")]
  44. public string Title
  45. {
  46. get { return title; }
  47. set
  48. {
  49. title = value;
  50. ResetTitleSize();
  51. Invalidate();
  52. }
  53. }
  54. /// <summary>
  55. /// The title font
  56. /// </summary>
  57. private Font titleFont = new Font("微软雅黑", 12);
  58. /// <summary>
  59. /// Gets or sets the title font.
  60. /// </summary>
  61. /// <value>The title font.</value>
  62. [Browsable(true)]
  63. [Category("自定义")]
  64. [Description("获取或设置标题字体")]
  65. public Font TitleFont
  66. {
  67. get { return titleFont; }
  68. set
  69. {
  70. titleFont = value;
  71. ResetTitleSize();
  72. Invalidate();
  73. }
  74. }
  75. /// <summary>
  76. /// The title fore color
  77. /// </summary>
  78. private Color titleForeColor = Color.Black;
  79. /// <summary>
  80. /// Gets or sets the color of the title fore.
  81. /// </summary>
  82. /// <value>The color of the title fore.</value>
  83. [Browsable(true)]
  84. [Category("自定义")]
  85. [Description("获取或设置标题文字颜色")]
  86. public Color TitleForeColor
  87. {
  88. get { return titleForeColor; }
  89. set
  90. {
  91. titleForeColor = value;
  92. Invalidate();
  93. }
  94. }
  95. /// <summary>
  96. /// The items
  97. /// </summary>
  98. private FunelChartItem[] items;
  99. /// <summary>
  100. /// Gets or sets the items.
  101. /// </summary>
  102. /// <value>The items.</value>
  103. [Browsable(true)]
  104. [Category("自定义")]
  105. [Description("获取或设置项目")]
  106. public FunelChartItem[] Items
  107. {
  108. get { return items; }
  109. set
  110. {
  111. items = value;
  112. Invalidate();
  113. }
  114. }
  115. /// <summary>
  116. /// The direction
  117. /// </summary>
  118. private FunelChartDirection direction = FunelChartDirection.UP;
  119. /// <summary>
  120. /// Gets or sets the direction.
  121. /// </summary>
  122. /// <value>The direction.</value>
  123. [Browsable(true)]
  124. [Category("自定义")]
  125. [Description("获取或设置方向")]
  126. public FunelChartDirection Direction
  127. {
  128. get { return direction; }
  129. set
  130. {
  131. direction = value;
  132. Invalidate();
  133. }
  134. }
  135. /// <summary>
  136. /// The alignment
  137. /// </summary>
  138. private FunelChartAlignment alignment = FunelChartAlignment.Center;
  139. /// <summary>
  140. /// Gets or sets the alignment.
  141. /// </summary>
  142. /// <value>The alignment.</value>
  143. [Browsable(true)]
  144. [Category("自定义")]
  145. [Description("获取或设置对齐方式")]
  146. public FunelChartAlignment Alignment
  147. {
  148. get { return alignment; }
  149. set
  150. {
  151. alignment = value;
  152. Invalidate();
  153. }
  154. }
  155. /// <summary>
  156. /// The item text align
  157. /// </summary>
  158. private FunelChartAlignment itemTextAlign = FunelChartAlignment.Center;
  159. /// <summary>
  160. /// Gets or sets the item text align.
  161. /// </summary>
  162. /// <value>The item text align.</value>
  163. [Browsable(true)]
  164. [Category("自定义")]
  165. [Description("获取或设置文字位置")]
  166. public FunelChartAlignment ItemTextAlign
  167. {
  168. get { return itemTextAlign; }
  169. set
  170. {
  171. itemTextAlign = value;
  172. ResetWorkingRect();
  173. Invalidate();
  174. }
  175. }
  176. /// <summary>
  177. /// The show value
  178. /// </summary>
  179. private bool showValue = false;
  180. /// <summary>
  181. /// Gets or sets a value indicating whether [show value].
  182. /// </summary>
  183. /// <value><c>true</c> if [show value]; otherwise, <c>false</c>.</value>
  184. [Browsable(true)]
  185. [Category("自定义")]
  186. [Description("获取或设置是否显示值")]
  187. public bool ShowValue
  188. {
  189. get { return showValue; }
  190. set
  191. {
  192. showValue = value;
  193. Invalidate();
  194. }
  195. }
  196. /// <summary>
  197. /// The value format
  198. /// </summary>
  199. private string valueFormat = "0.##";
  200. /// <summary>
  201. /// Gets or sets the value format.
  202. /// </summary>
  203. /// <value>The value format.</value>
  204. [Browsable(true)]
  205. [Category("自定义")]
  206. [Description("获取或设置值格式化")]
  207. public string ValueFormat
  208. {
  209. get { return valueFormat; }
  210. set
  211. {
  212. valueFormat = value;
  213. Invalidate();
  214. }
  215. }
  216. /// <summary>
  217. /// The m rect working
  218. /// </summary>
  219. RectangleF m_rectWorking;
  220. /// <summary>
  221. /// The m title size
  222. /// </summary>
  223. SizeF m_titleSize = SizeF.Empty;
  224. /// <summary>
  225. /// The int split width
  226. /// </summary>
  227. int intSplitWidth = 1;
  228. /// <summary>
  229. /// Initializes a new instance of the <see cref="UCFunnelChart"/> class.
  230. /// </summary>
  231. public UCFunnelChart()
  232. {
  233. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  234. this.SetStyle(ControlStyles.DoubleBuffer, true);
  235. this.SetStyle(ControlStyles.ResizeRedraw, true);
  236. this.SetStyle(ControlStyles.Selectable, true);
  237. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  238. this.SetStyle(ControlStyles.UserPaint, true);
  239. this.FontChanged += UCFunnelChart_FontChanged;
  240. Font = new Font("微软雅黑", 8);
  241. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
  242. this.SizeChanged += UCFunnelChart_SizeChanged;
  243. Size = new System.Drawing.Size(150, 150);
  244. items = new FunelChartItem[0];
  245. if (ControlHelper.IsDesignMode())
  246. {
  247. items = new FunelChartItem[5];
  248. for (int i = 0; i < 5; i++)
  249. {
  250. items[i] = new FunelChartItem()
  251. {
  252. Text = "item" + i,
  253. Value = 10 * (i + 1)
  254. };
  255. }
  256. }
  257. }
  258. /// <summary>
  259. /// Handles the FontChanged event of the UCFunnelChart control.
  260. /// </summary>
  261. /// <param name="sender">The source of the event.</param>
  262. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  263. void UCFunnelChart_FontChanged(object sender, EventArgs e)
  264. {
  265. ResetWorkingRect();
  266. }
  267. /// <summary>
  268. /// Handles the SizeChanged event of the UCFunnelChart control.
  269. /// </summary>
  270. /// <param name="sender">The source of the event.</param>
  271. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  272. void UCFunnelChart_SizeChanged(object sender, EventArgs e)
  273. {
  274. ResetWorkingRect();
  275. }
  276. /// <summary>
  277. /// Resets the working rect.
  278. /// </summary>
  279. private void ResetWorkingRect()
  280. {
  281. if (itemTextAlign == FunelChartAlignment.Center)
  282. {
  283. m_rectWorking = new RectangleF(0, m_titleSize.Height == 0 ? 0 : (m_titleSize.Height + 10), this.Width, this.Height - (m_titleSize.Height == 0 ? 0 : (m_titleSize.Height + 10)));
  284. }
  285. else if (itemTextAlign == FunelChartAlignment.Left)
  286. {
  287. float fltMax = 0;
  288. if (items != null && items.Length > 0)
  289. {
  290. using (Graphics g = this.CreateGraphics())
  291. {
  292. fltMax = items.Max(p => g.MeasureString(p.Text, Font).Width);
  293. }
  294. }
  295. m_rectWorking = new RectangleF(fltMax, m_titleSize.Height == 0 ? 0 : (m_titleSize.Height + 10), this.Width - fltMax, this.Height - (m_titleSize.Height == 0 ? 0 : (m_titleSize.Height + 10)));
  296. }
  297. else
  298. {
  299. float fltMax = 0;
  300. if (items != null && items.Length > 0)
  301. {
  302. using (Graphics g = this.CreateGraphics())
  303. {
  304. fltMax = items.Max(p => g.MeasureString(p.Text, Font).Width);
  305. }
  306. }
  307. m_rectWorking = new RectangleF(0, m_titleSize.Height == 0 ? 0 : (m_titleSize.Height + 10), this.Width - fltMax, this.Height - (m_titleSize.Height == 0 ? 0 : (m_titleSize.Height + 10)));
  308. }
  309. }
  310. /// <summary>
  311. /// Resets the size of the title.
  312. /// </summary>
  313. private void ResetTitleSize()
  314. {
  315. if (string.IsNullOrEmpty(title))
  316. {
  317. m_titleSize = SizeF.Empty;
  318. }
  319. else
  320. {
  321. using (Graphics g = this.CreateGraphics())
  322. {
  323. m_titleSize = g.MeasureString(title, titleFont);
  324. m_titleSize.Height += 20;
  325. }
  326. }
  327. ResetWorkingRect();
  328. }
  329. /// <summary>
  330. /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
  331. /// </summary>
  332. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
  333. protected override void OnPaint(PaintEventArgs e)
  334. {
  335. base.OnPaint(e);
  336. var g = e.Graphics;
  337. g.SetGDIHigh();
  338. if (!string.IsNullOrEmpty(title))
  339. {
  340. g.DrawString(title, titleFont, new SolidBrush(titleForeColor), new RectangleF(0, 0, this.Width, m_titleSize.Height), new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
  341. }
  342. if (items == null || items.Length <= 0)
  343. {
  344. g.DrawString("没有数据", Font, new SolidBrush(Color.Black), this.m_rectWorking, new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
  345. return;
  346. }
  347. List<FunelChartItem> lstItems;
  348. if (direction == FunelChartDirection.UP)
  349. {
  350. lstItems = items.OrderBy(p => p.Value).ToList();
  351. }
  352. else
  353. {
  354. lstItems = items.OrderByDescending(p => p.Value).ToList();
  355. }
  356. List<RectangleF> lstRects = new List<RectangleF>();
  357. List<GraphicsPath> lstPaths = new List<GraphicsPath>();
  358. float maxValue = lstItems.Max(p => p.Value);
  359. float dblSplitHeight = m_rectWorking.Height / lstItems.Count;
  360. for (int i = 0; i < lstItems.Count; i++)
  361. {
  362. FunelChartItem item = lstItems[i];
  363. if (item.ValueColor == null || item.ValueColor == Color.Empty || item.ValueColor == Color.Transparent)
  364. item.ValueColor = ControlHelper.Colors[i];
  365. switch (alignment)
  366. {
  367. case FunelChartAlignment.Left:
  368. lstRects.Add(new RectangleF(m_rectWorking.Left, m_rectWorking.Top + dblSplitHeight * i, item.Value / maxValue * m_rectWorking.Width, dblSplitHeight));
  369. break;
  370. case FunelChartAlignment.Center:
  371. lstRects.Add(new RectangleF(m_rectWorking.Left + (m_rectWorking.Width - (item.Value / maxValue * m_rectWorking.Width)) / 2, m_rectWorking.Top + dblSplitHeight * i, item.Value / maxValue * m_rectWorking.Width, dblSplitHeight));
  372. break;
  373. case FunelChartAlignment.Right:
  374. lstRects.Add(new RectangleF(m_rectWorking.Right - (item.Value / maxValue * m_rectWorking.Width), m_rectWorking.Top + dblSplitHeight * i, item.Value / maxValue * m_rectWorking.Width, dblSplitHeight));
  375. break;
  376. }
  377. }
  378. for (int i = 0; i < lstRects.Count; i++)
  379. {
  380. var rect = lstRects[i];
  381. GraphicsPath path = new GraphicsPath();
  382. List<PointF> lstPoints = new List<PointF>();
  383. if (direction == FunelChartDirection.UP)
  384. {
  385. switch (alignment)
  386. {
  387. case FunelChartAlignment.Left:
  388. lstPoints.Add(new PointF(rect.Left, rect.Top));
  389. if (i != 0)
  390. {
  391. lstPoints.Add(new PointF(lstRects[i - 1].Right, rect.Top));
  392. }
  393. break;
  394. case FunelChartAlignment.Center:
  395. if (i == 0)
  396. {
  397. lstPoints.Add(new PointF(rect.Left + rect.Width / 2, rect.Top));
  398. }
  399. else
  400. {
  401. lstPoints.Add(new PointF(lstRects[i - 1].Left, rect.Top));
  402. lstPoints.Add(new PointF(lstRects[i - 1].Right, rect.Top));
  403. }
  404. break;
  405. case FunelChartAlignment.Right:
  406. if (i == 0)
  407. {
  408. lstPoints.Add(new PointF(rect.Right, rect.Top));
  409. }
  410. else
  411. {
  412. lstPoints.Add(new PointF(rect.Right - lstRects[i - 1].Width, rect.Top));
  413. lstPoints.Add(new PointF(rect.Right, rect.Top));
  414. }
  415. break;
  416. }
  417. lstPoints.Add(new PointF(rect.Right, rect.Bottom - intSplitWidth));
  418. lstPoints.Add(new PointF(rect.Left, rect.Bottom - intSplitWidth));
  419. }
  420. else
  421. {
  422. lstPoints.Add(new PointF(rect.Left, rect.Top + intSplitWidth));
  423. lstPoints.Add(new PointF(rect.Right, rect.Top + intSplitWidth));
  424. switch (alignment)
  425. {
  426. case FunelChartAlignment.Left:
  427. if (i == lstRects.Count - 1)
  428. {
  429. lstPoints.Add(new PointF(rect.Left, rect.Bottom));
  430. }
  431. else
  432. {
  433. lstPoints.Add(new PointF(lstRects[i + 1].Right, rect.Bottom));
  434. lstPoints.Add(new PointF(rect.Left, rect.Bottom));
  435. }
  436. break;
  437. case FunelChartAlignment.Center:
  438. if (i == lstRects.Count - 1)
  439. {
  440. lstPoints.Add(new PointF(rect.Left + rect.Width / 2, rect.Bottom));
  441. }
  442. else
  443. {
  444. lstPoints.Add(new PointF(lstRects[i + 1].Right, rect.Bottom));
  445. lstPoints.Add(new PointF(lstRects[i + 1].Left, rect.Bottom));
  446. }
  447. break;
  448. case FunelChartAlignment.Right:
  449. if (i == lstRects.Count - 1)
  450. {
  451. lstPoints.Add(new PointF(rect.Right, rect.Bottom));
  452. }
  453. else
  454. {
  455. lstPoints.Add(new PointF(rect.Right, rect.Bottom));
  456. lstPoints.Add(new PointF(lstRects[i + 1].Left, rect.Bottom));
  457. }
  458. break;
  459. }
  460. }
  461. path.AddLines(lstPoints.ToArray());
  462. path.CloseAllFigures();
  463. // g.DrawPath(new Pen(new SolidBrush(lstItems[i].ValueColor.Value)), path);
  464. g.FillPath(new SolidBrush(lstItems[i].ValueColor.Value), path);
  465. //写字
  466. if (itemTextAlign == FunelChartAlignment.Center)
  467. {
  468. g.DrawString(lstItems[i].Text + (ShowValue ? lstItems[i].Value.ToString("\n" + valueFormat) : ""), Font, new SolidBrush((lstItems[i].TextForeColor == null || lstItems[i].TextForeColor == Color.Empty || lstItems[i].TextForeColor == Color.Transparent) ? Color.White : lstItems[i].TextForeColor.Value), rect, new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });
  469. }
  470. else if (itemTextAlign == FunelChartAlignment.Left)
  471. {
  472. g.DrawString(lstItems[i].Text + (ShowValue ? lstItems[i].Value.ToString("\n" + valueFormat) : ""), Font, new SolidBrush((lstItems[i].TextForeColor == null || lstItems[i].TextForeColor == Color.Empty || lstItems[i].TextForeColor == Color.Transparent) ? lstItems[i].ValueColor.Value : lstItems[i].TextForeColor.Value), new RectangleF(0, rect.Top, rect.Left, rect.Height), new StringFormat() { Alignment = StringAlignment.Far, LineAlignment = StringAlignment.Center });
  473. g.DrawLine(new Pen(new SolidBrush((lstItems[i].TextForeColor == null || lstItems[i].TextForeColor == Color.Empty || lstItems[i].TextForeColor == Color.Transparent) ? lstItems[i].ValueColor.Value : lstItems[i].TextForeColor.Value)), rect.Left, rect.Top + rect.Height / 2, rect.Left + rect.Width / 2, rect.Top + rect.Height / 2);
  474. }
  475. else
  476. {
  477. g.DrawString(lstItems[i].Text + (ShowValue ? lstItems[i].Value.ToString("\n" + valueFormat) : ""), Font, new SolidBrush((lstItems[i].TextForeColor == null || lstItems[i].TextForeColor == Color.Empty || lstItems[i].TextForeColor == Color.Transparent) ? lstItems[i].ValueColor.Value : lstItems[i].TextForeColor.Value), new RectangleF(rect.Right, rect.Top, this.Width - rect.Right, rect.Height), new StringFormat() { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Center });
  478. g.DrawLine(new Pen(new SolidBrush((lstItems[i].TextForeColor == null || lstItems[i].TextForeColor == Color.Empty || lstItems[i].TextForeColor == Color.Transparent) ? lstItems[i].ValueColor.Value : lstItems[i].TextForeColor.Value)), rect.Left + rect.Width / 2, rect.Top + rect.Height / 2, rect.Right, rect.Top + rect.Height / 2);
  479. }
  480. }
  481. }
  482. }
  483. }