UserPieChart.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using HslCommunication.Core;
  10. namespace HslCommunication.Controls
  11. {
  12. /// <summary>
  13. /// 一个饼图的控件
  14. /// </summary>
  15. public partial class UserPieChart : UserControl
  16. {
  17. /// <summary>
  18. /// 实例化一个饼图的控件
  19. /// </summary>
  20. public UserPieChart( )
  21. {
  22. InitializeComponent( );
  23. random = new Random( );
  24. DoubleBuffered = true;
  25. formatCenter = new StringFormat( );
  26. formatCenter.Alignment = StringAlignment.Center;
  27. formatCenter.LineAlignment = StringAlignment.Center;
  28. pieItems = new HslPieItem[0];
  29. }
  30. private void UserPieChart_Load( object sender, EventArgs e )
  31. {
  32. }
  33. #region Private
  34. private HslPieItem[] pieItems = new HslPieItem[0]; // 饼图的数据
  35. private Random random = null; // 随机数
  36. private StringFormat formatCenter = null; // 格式化的文本位置
  37. private int margin = 40; // 边界距离
  38. private bool m_IsRenderPercent = false; // 是否显示百分比
  39. private bool m_IsRenderSmall = true; // 是否在图形上显示占比非常小的文本信息
  40. #endregion
  41. /// <summary>
  42. /// 是否显示百分比信息
  43. /// </summary>
  44. [Browsable( true )]
  45. [Category( "外观" )]
  46. [DefaultValue( false )]
  47. [Description( "获取或设置是否显示百分比占用" )]
  48. public bool IsRenderPercent
  49. {
  50. get { return m_IsRenderPercent; }
  51. set { m_IsRenderPercent = value; Invalidate( ); }
  52. }
  53. /// <summary>
  54. /// 是否在图形上显示占比非常小的文本信息
  55. /// </summary>
  56. [Browsable(true)]
  57. [Category("外观")]
  58. [Description("获取或设置是否显示占比很小的文本信息")]
  59. [DefaultValue(true)]
  60. public bool IsRenderSmall
  61. {
  62. get { return m_IsRenderSmall; }
  63. set { m_IsRenderSmall = value; Invalidate( ); }
  64. }
  65. private void SetMarginPaint( int value )
  66. {
  67. if (value > 500)
  68. {
  69. margin = 80;
  70. }
  71. else if (value > 300)
  72. {
  73. margin = 60;
  74. }
  75. else
  76. {
  77. margin = 40;
  78. }
  79. }
  80. private Point GetCenterPoint( out int width )
  81. {
  82. if (Width > Height)
  83. {
  84. SetMarginPaint( Height );
  85. width = Height / 2 - margin;
  86. return new Point( Height / 2 - 1, Height / 2 - 1 );
  87. }
  88. else
  89. {
  90. SetMarginPaint( Width );
  91. width = Width / 2 - margin;
  92. return new Point( Width / 2 - 1, Width / 2 - 1 );
  93. }
  94. }
  95. /// <summary>
  96. /// 随机生成颜色,该颜色相对于白色为深色颜色
  97. /// </summary>
  98. /// <returns></returns>
  99. private Color GetRandomColor( )
  100. {
  101. int int_Red = random.Next( 256 );
  102. int int_Green = random.Next( 256 );
  103. int int_Blue = (int_Red + int_Green > 430) ? random.Next( 100 ) : random.Next( 200 );
  104. return Color.FromArgb( int_Red, int_Green, int_Blue );
  105. }
  106. private void UserPieChart_Paint( object sender, PaintEventArgs e )
  107. {
  108. // 画刷初始化
  109. e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  110. e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
  111. // 开始绘制饼图,寻找中心点
  112. int width = 0;
  113. Point center = GetCenterPoint( out width);
  114. Rectangle rect_center = new Rectangle( center.X - width, center.Y - width, width + width, width + width );
  115. if (width > 0 && pieItems.Length > 0)
  116. {
  117. e.Graphics.FillEllipse( Brushes.AliceBlue, rect_center );
  118. e.Graphics.DrawEllipse( Pens.DodgerBlue, rect_center );
  119. Rectangle rect_tran = new Rectangle( rect_center.X - center.X, rect_center.Y - center.Y, rect_center.Width, rect_center.Height );
  120. e.Graphics.TranslateTransform( center.X, center.Y );
  121. e.Graphics.RotateTransform( 90 );
  122. e.Graphics.DrawLine( Pens.DimGray, 0, 0, width, 0 );
  123. // 计算分布
  124. int totle = pieItems.Sum( item => item.Value );
  125. float totleAngle = 0; // 本次绘制的角度
  126. float lastAngle = -90; // 上一次的角度
  127. for (int i = 0; i < pieItems.Length; i++)
  128. {
  129. float single = 0;
  130. if (totle == 0)
  131. {
  132. single = 360 / pieItems.Length;
  133. }
  134. else
  135. {
  136. single = Convert.ToSingle( pieItems[i].Value * 1.0d / totle * 360 );
  137. }
  138. using (Brush brush = new SolidBrush( pieItems[i].Back ))
  139. {
  140. e.Graphics.FillPie( brush, rect_tran, 0, -single );
  141. }
  142. e.Graphics.RotateTransform( 0 - single / 2 );
  143. if (single < 2f && !IsRenderSmall)
  144. {
  145. totleAngle += single;
  146. }
  147. else
  148. {
  149. // 画标线
  150. totleAngle += single / 2;
  151. int mark = 8;
  152. if (totleAngle < 45 || totleAngle > 315) mark = 15;
  153. if (totleAngle > 135 && totleAngle < 225) mark = 15;
  154. e.Graphics.DrawLine( Pens.DimGray, width * 2 / 3, 0, width + mark, 0 );
  155. e.Graphics.TranslateTransform( width + mark, 0 );
  156. if (totleAngle - lastAngle < 5)
  157. {
  158. // 两次的标线非常接近
  159. }
  160. lastAngle = totleAngle;
  161. if (totleAngle < 90)
  162. {
  163. e.Graphics.RotateTransform( totleAngle - 90 );
  164. e.Graphics.DrawLine( Pens.DimGray, 0, 0, margin - mark, 0 );
  165. e.Graphics.DrawString( pieItems[i].Name, Font, Brushes.DimGray, new Point( 0, -Font.Height ) );
  166. if (IsRenderPercent) e.Graphics.DrawString( Math.Round( single * 100 / 360, 2 ).ToString( ) + "%", Font, Brushes.DodgerBlue, new Point( 0, 1 ) );
  167. e.Graphics.RotateTransform( 90 - totleAngle );
  168. }
  169. else if (totleAngle < 180)
  170. {
  171. e.Graphics.RotateTransform( totleAngle - 90 );
  172. e.Graphics.DrawLine( Pens.DimGray, 0, 0, margin - mark, 0 );
  173. e.Graphics.DrawString( pieItems[i].Name, Font, Brushes.DimGray, new Point( 0, -Font.Height ) );
  174. if (IsRenderPercent) e.Graphics.DrawString( Math.Round( single * 100 / 360, 2 ).ToString( ) + "%", Font, Brushes.DodgerBlue, new Point( 0, 1 ) );
  175. e.Graphics.RotateTransform( 90 - totleAngle );
  176. }
  177. else if (totleAngle < 270)
  178. {
  179. e.Graphics.RotateTransform( totleAngle - 270 );
  180. e.Graphics.DrawLine( Pens.DimGray, 0, 0, margin - mark, 0 );
  181. e.Graphics.TranslateTransform( margin - 8, 0 );
  182. e.Graphics.RotateTransform( 180 );
  183. e.Graphics.DrawString( pieItems[i].Name, Font, Brushes.DimGray, new Point( 0, -Font.Height ) );
  184. if (IsRenderPercent) e.Graphics.DrawString( Math.Round( single * 100 / 360, 2 ).ToString( ) + "%", Font, Brushes.DodgerBlue, new Point( 0, 1 ) );
  185. e.Graphics.RotateTransform( -180 );
  186. e.Graphics.TranslateTransform( 8 - margin, 0 );
  187. e.Graphics.RotateTransform( 270 - totleAngle );
  188. }
  189. else
  190. {
  191. e.Graphics.RotateTransform( totleAngle - 270 );
  192. e.Graphics.DrawLine( Pens.DimGray, 0, 0, margin - mark, 0 );
  193. e.Graphics.TranslateTransform( margin - 8, 0 );
  194. e.Graphics.RotateTransform( 180 );
  195. e.Graphics.DrawString( pieItems[i].Name, Font, Brushes.DimGray, new Point( 0, -Font.Height ) );
  196. if (IsRenderPercent) e.Graphics.DrawString( Math.Round( single * 100 / 360, 2 ).ToString( ) + "%", Font, Brushes.DodgerBlue, new Point( 0, 1 ) );
  197. e.Graphics.RotateTransform( -180 );
  198. e.Graphics.TranslateTransform( 8 - margin, 0 );
  199. e.Graphics.RotateTransform( 270 - totleAngle );
  200. }
  201. e.Graphics.TranslateTransform( -width - mark, 0 );
  202. e.Graphics.RotateTransform( 0 - single / 2 );
  203. totleAngle += single / 2;
  204. }
  205. }
  206. e.Graphics.ResetTransform( );
  207. }
  208. else
  209. {
  210. e.Graphics.FillEllipse( Brushes.AliceBlue, rect_center );
  211. e.Graphics.DrawEllipse( Pens.DodgerBlue, rect_center );
  212. e.Graphics.DrawString( "空", Font, Brushes.DimGray, rect_center, formatCenter );
  213. }
  214. }
  215. #region Public Method
  216. /// <summary>
  217. /// 设置显示的数据源
  218. /// </summary>
  219. /// <param name="source">特殊的显示对象</param>
  220. /// <exception cref="ArgumentNullException"></exception>
  221. public void SetDataSource( HslPieItem[] source )
  222. {
  223. if (source != null)
  224. {
  225. pieItems = source;
  226. Invalidate( );
  227. }
  228. }
  229. /// <summary>
  230. /// 根据名称和值进行数据源的显示,两者的长度需要一致
  231. /// </summary>
  232. /// <param name="names">名称</param>
  233. /// <param name="values">值</param>
  234. /// <exception cref="ArgumentNullException"></exception>
  235. public void SetDataSource( string[] names, int[] values )
  236. {
  237. if (names == null) throw new ArgumentNullException( "names" );
  238. if (values == null) throw new ArgumentNullException( "values" );
  239. if (names.Length != values.Length) throw new Exception( "两个数组的长度不一致!" );
  240. pieItems = new HslPieItem[names.Length];
  241. for (int i = 0; i < names.Length; i++)
  242. {
  243. pieItems[i] = new HslPieItem( )
  244. {
  245. Name = names[i],
  246. Value = values[i],
  247. Back = GetRandomColor( ),
  248. };
  249. }
  250. Invalidate( );
  251. }
  252. #endregion
  253. }
  254. }