当前位置:   article > 正文

(七十)c#Winform自定义控件-饼状图_c# winform 饼图

c# winform 饼图

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHubhttps://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 企鹅群568015492

麻烦博客下方点个【推荐】,谢谢

NuGet

Install-Package HZH_Controls

目录

https://blog.csdn.net/kwwwvagaa/article/details/100586547

用处及效果

准备工作

使用GDI+画的控件,不了解可以先百度下

开始

添加一个类UCPieChart ,继承UserControl

添加一些属性

复制代码

  1  /// <summary>
  2         /// The pie items
  3         /// </summary>
  4         private PieItem[] pieItems = new PieItem[0];
  5 
  6         /// <summary>
  7         /// The random
  8         /// </summary>
  9         private Random random = null;
 10 
 11         /// <summary>
 12         /// The format center
 13         /// </summary>
 14         private StringFormat formatCenter = null;
 15 
 16         /// <summary>
 17         /// The margin
 18         /// </summary>
 19         private int margin = 50;
 20 
 21         /// <summary>
 22         /// The m is render percent
 23         /// </summary>
 24         private bool m_IsRenderPercent = false;
 25 
 26         /// <summary>
 27         /// The percen format
 28         /// </summary>
 29         private string percenFormat = "{0:F2}%";
 30 
 31         /// <summary>
 32         /// The components
 33         /// </summary>
 34         private IContainer components = null;
 35 
 36         /// <summary>
 37         /// Gets or sets a value indicating whether this instance is render percent.
 38         /// </summary>
 39         /// <value><c>true</c> if this instance is render percent; otherwise, <c>false</c>.</value>
 40         [Browsable(true)]
 41         [Category("自定义")]
 42         [DefaultValue(false)]
 43         [Description("获取或设置是否显示百分比占用")]
 44         public bool IsRenderPercent
 45         {
 46             get
 47             {
 48                 return m_IsRenderPercent;
 49             }
 50             set
 51             {
 52                 m_IsRenderPercent = value;
 53                 Invalidate();
 54             }
 55         }
 56 
 57 
 58         /// <summary>
 59         /// Gets or sets the text margin.
 60         /// </summary>
 61         /// <value>The text margin.</value>
 62         [Browsable(true)]
 63         [Category("自定义")]
 64         [Description("获取或设置文本距离,单位为像素,默认50")]
 65         [DefaultValue(50)]
 66         public int TextMargin
 67         {
 68             get
 69             {
 70                 return margin;
 71             }
 72             set
 73             {
 74                 margin = value;
 75                 Invalidate();
 76             }
 77         }
 78 
 79 
 80         /// <summary>
 81         /// Gets or sets the percent format.
 82         /// </summary>
 83         /// <value>The percent format.</value>
 84         [Browsable(true)]
 85         [Category("自定义")]
 86         [Description("获取或设置文百分比文字的格式化信息")]
 87         [DefaultValue("{0:F2}%")]
 88         public string PercentFormat
 89         {
 90             get
 91             {
 92                 return percenFormat;
 93             }
 94             set
 95             {
 96                 percenFormat = value;
 97                 Invalidate();
 98             }
 99         }
100 
101         /// <summary>
102         /// The center of circle color
103         /// </summary>
104         private Color centerOfCircleColor = Color.White;
105         /// <summary>
106         /// Gets or sets the color of the center of circle.
107         /// </summary>
108         /// <value>The color of the center of circle.</value>
109         [Browsable(true)]
110         [Category("自定义")]
111         [Description("获取或设置圆心颜色")]
112         public Color CenterOfCircleColor
113         {
114             get { return centerOfCircleColor; }
115             set
116             {
117                 centerOfCircleColor = value;
118                 Invalidate();
119             }
120         }
121 
122         /// <summary>
123         /// The center of circle width
124         /// </summary>
125         private int centerOfCircleWidth = 0;
126         /// <summary>
127         /// Gets or sets the width of the center of circle.
128         /// </summary>
129         /// <value>The width of the center of circle.</value>
130         [Browsable(true)]
131         [Category("自定义")]
132         [Description("获取或设置圆心宽度")]
133         public int CenterOfCircleWidth
134         {
135             get { return centerOfCircleWidth; }
136             set
137             {
138                 if (value < 0)
139                     return;
140                 centerOfCircleWidth = value;
141                 Invalidate();
142             }
143         }
144 
145         /// <summary>
146         /// The title
147         /// </summary>
148         private string title;
149         /// <summary>
150         /// Gets or sets the ti tle.
151         /// </summary>
152         /// <value>The ti tle.</value>
153         [Browsable(true)]
154         [Category("自定义")]
155         [Description("获取或设置标题")]
156         public string TiTle
157         {
158             get { return title; }
159             set
160             {
161                 title = value;
162                 ResetTitleHeight();
163                 Invalidate();
164             }
165         }
166         /// <summary>
167         /// The title font
168         /// </summary>
169         private Font titleFont = new Font("微软雅黑", 12);
170         /// <summary>
171         /// Gets or sets the title font.
172         /// </summary>
173         /// <value>The title font.</value>
174         [Browsable(true)]
175         [Category("自定义")]
176         [Description("获取或设置标题字体")]
177         public Font TitleFont
178         {
179             get { return titleFont; }
180             set
181             {
182                 titleFont = value;
183                 ResetTitleHeight();
184                 Invalidate();
185             }
186         }
187 
188         /// <summary>
189         /// The title froe color
190         /// </summary>
191         private Color titleFroeColor = Color.Black;
192         /// <summary>
193         /// Gets or sets the color of the title froe.
194         /// </summary>
195         /// <value>The color of the title froe.</value>
196         [Browsable(true)]
197         [Category("自定义")]
198         [Description("获取或设置标题颜色")]
199         public Color TitleFroeColor
200         {
201             get { return titleFroeColor; }
202             set
203             {
204                 titleFroeColor = value;
205                 Invalidate();
206             }
207         }
208 
209         /// <summary>
210         /// The title size
211         /// </summary>
212         private SizeF titleSize = SizeF.Empty;
213         /// <summary>
214         /// Resets the height of the title.
215         /// </summary>
216         private void ResetTitleHeight()
217         {
218             if (string.IsNullOrEmpty(title))
219                 titleSize = SizeF.Empty;
220             else
221             {
222                 using (var g = this.CreateGraphics())
223                 {
224                     titleSize = g.MeasureString(title, titleFont);
225                 }
226             }
227         }
228 
229         /// <summary>
230         /// Gets or sets the data source.
231         /// </summary>
232         /// <value>The data source.</value>
233         [Browsable(true)]
234         [Category("自定义")]
235         [Description("获取或设置标题颜色")]
236         [Localizable(true)]
237         public PieItem[] DataSource
238         {
239             get { return pieItems; }
240             set
241             {
242                 pieItems = value;
243                 Invalidate();
244             }
245         }

复制代码

重绘

复制代码

  1  protected override void OnPaint(PaintEventArgs e)
  2         {
  3             e.Graphics.SetGDIHigh();
  4 
  5             int width;
  6             Point centerPoint = GetCenterPoint(out width);
  7             Rectangle rectangle = new Rectangle(centerPoint.X - width, centerPoint.Y - width, width * 2, width * 2);
  8             if (width > 0 && pieItems.Length != 0)
  9             {
 10                 if (!string.IsNullOrEmpty(title))
 11                     e.Graphics.DrawString(title, titleFont, new SolidBrush(titleFroeColor), new PointF((this.Width - titleSize.Width) / 2, 5));               
 12                 Rectangle rect = new Rectangle(rectangle.X - centerPoint.X, rectangle.Y - centerPoint.Y, rectangle.Width, rectangle.Height);
 13                 e.Graphics.TranslateTransform(centerPoint.X, centerPoint.Y);
 14                 e.Graphics.RotateTransform(90f);
 15                 int num = pieItems.Sum((PieItem item) => item.Value);
 16                 float num2 = 0f;
 17                 float num3 = -90f;
 18                 for (int i = 0; i < pieItems.Length; i++)
 19                 {
 20                     Color cItem = pieItems[i].PieColor ?? ControlHelper.Colors[i];
 21                     Pen pen = new Pen(cItem, 1f);
 22                     SolidBrush solidBrush = new SolidBrush(cItem);
 23                     SolidBrush solidBrush2 = new SolidBrush(cItem);
 24                     Brush percentBrush = new SolidBrush(cItem);
 25                     float num4 = e.Graphics.MeasureString(pieItems[i].Name, Font).Width + 3f;
 26                     float num5 = (num != 0) ? Convert.ToSingle((double)pieItems[i].Value * 1.0 / (double)num * 360.0) : ((float)(360 / pieItems.Length));
 27                     e.Graphics.FillPie(solidBrush, rect, 0f, 0f - num5);
 28                     e.Graphics.DrawPie(new Pen(solidBrush), rect, 0f, 0f - num5);
 29                     e.Graphics.RotateTransform(0f - num5 / 2f);
 30                     if (num5 < 2f)
 31                     {
 32                         num2 += num5;
 33                     }
 34                     else
 35                     {
 36                         num2 += num5 / 2f;
 37                         int num6 = 15;
 38                         if (num2 < 45f || num2 > 315f)
 39                         {
 40                             num6 = 20;
 41                         }
 42                         if (num2 > 135f && num2 < 225f)
 43                         {
 44                             num6 = 20;
 45                         }
 46                         e.Graphics.DrawLine(pen, width * 2 / 3, 0, width + num6, 0);
 47                         e.Graphics.TranslateTransform(width + num6, 0f);
 48                         if (num2 - num3 < 5f)
 49                         {
 50                         }
 51                         num3 = num2;
 52                         if (num2 < 180f)
 53                         {
 54                             e.Graphics.RotateTransform(num2 - 90f);
 55                             e.Graphics.DrawLine(pen, 0f, 0f, num4, 0f);
 56                             e.Graphics.DrawString(pieItems[i].Name, Font, solidBrush2, new Point(0, -Font.Height));
 57                             if (IsRenderPercent)
 58                             {
 59                                 e.Graphics.DrawString(string.Format(percenFormat, num5 * 100f / 360f), Font, percentBrush, new Point(0, 1));
 60                             }
 61                             e.Graphics.RotateTransform(90f - num2);
 62                         }
 63                         else
 64                         {
 65                             e.Graphics.RotateTransform(num2 - 270f);
 66                             e.Graphics.DrawLine(pen, 0f, 0f, num4, 0f);
 67                             e.Graphics.TranslateTransform(num4 - 3f, 0f);
 68                             e.Graphics.RotateTransform(180f);
 69                             e.Graphics.DrawString(pieItems[i].Name, Font, solidBrush2, new Point(0, -Font.Height));
 70                             if (IsRenderPercent)
 71                             {
 72                                 e.Graphics.DrawString(string.Format(percenFormat, num5 * 100f / 360f), Font, percentBrush, new Point(0, 1));
 73                             }
 74                             e.Graphics.RotateTransform(-180f);
 75                             e.Graphics.TranslateTransform(0f - num4 + 3f, 0f);
 76                             e.Graphics.RotateTransform(270f - num2);
 77                         }
 78                         e.Graphics.TranslateTransform(-width - num6, 0f);
 79                         e.Graphics.RotateTransform(0f - num5 / 2f);
 80                         num2 += num5 / 2f;
 81                     }
 82                     solidBrush.Dispose();
 83                     pen.Dispose();
 84                     solidBrush2.Dispose();
 85                     percentBrush.Dispose();
 86                 }
 87                 e.Graphics.ResetTransform();
 88 
 89                 if (centerOfCircleWidth > 0)
 90                 {
 91                     Rectangle rectCenter = new Rectangle(rect.Left + rect.Width / 2 - centerOfCircleWidth / 2, rect.Top + rect.Height / 2 - centerOfCircleWidth / 2, centerOfCircleWidth, centerOfCircleWidth);
 92                     e.Graphics.FillEllipse(new SolidBrush(centerOfCircleColor), rectCenter);
 93                 }
 94             }
 95             else
 96             {
 97                 e.Graphics.FillEllipse(Brushes.AliceBlue, rectangle);
 98                 e.Graphics.DrawEllipse(Pens.DodgerBlue, rectangle);
 99                 e.Graphics.DrawString("无数据", Font, Brushes.DimGray, rectangle, formatCenter);
100             }
101             base.OnPaint(e);
102         }

复制代码

一些辅助函数

复制代码

 1  /// <summary>
 2         /// Sets the data source.
 3         /// </summary>
 4         /// <param name="source">The source.</param>
 5         public void SetDataSource(PieItem[] source)
 6         {
 7             if (source != null)
 8             {
 9                 DataSource = source;
10             }
11         }
12         /// <summary>
13         /// Sets the data source.
14         /// </summary>
15         /// <param name="names">The names.</param>
16         /// <param name="values">The values.</param>
17         /// <exception cref="System.ArgumentNullException">
18         /// names
19         /// or
20         /// values
21         /// </exception>
22         /// <exception cref="System.Exception">两个数组的长度不一致!</exception>
23         public void SetDataSource(string[] names, int[] values)
24         {
25             if (names == null)
26             {
27                 throw new ArgumentNullException("names");
28             }
29             if (values == null)
30             {
31                 throw new ArgumentNullException("values");
32             }
33             if (names.Length != values.Length)
34             {
35                 throw new Exception("两个数组的长度不一致!");
36             }
37             pieItems = new PieItem[names.Length];
38             for (int i = 0; i < names.Length; i++)
39             {
40                 pieItems[i] = new PieItem
41                 {
42                     Name = names[i],
43                     Value = values[i]
44                 };
45             }
46             Invalidate();
47         }

复制代码

完整代码

  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 2019-09-23
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCPieChart.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.ComponentModel;
  18. using System.ComponentModel.Design;
  19. using System.Drawing;
  20. using System.Drawing.Drawing2D;
  21. using System.Drawing.Text;
  22. using System.Linq;
  23. using System.Windows.Forms;
  24. namespace HZH_Controls.Controls
  25. {
  26. /// <summary>
  27. /// Class UCPieChart.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. public class UCPieChart : UserControl
  32. {
  33. /// <summary>
  34. /// The pie items
  35. /// </summary>
  36. private PieItem[] pieItems = new PieItem[0];
  37. /// <summary>
  38. /// The random
  39. /// </summary>
  40. private Random random = null;
  41. /// <summary>
  42. /// The format center
  43. /// </summary>
  44. private StringFormat formatCenter = null;
  45. /// <summary>
  46. /// The margin
  47. /// </summary>
  48. private int margin = 50;
  49. /// <summary>
  50. /// The m is render percent
  51. /// </summary>
  52. private bool m_IsRenderPercent = false;
  53. /// <summary>
  54. /// The percen format
  55. /// </summary>
  56. private string percenFormat = "{0:F2}%";
  57. /// <summary>
  58. /// The components
  59. /// </summary>
  60. private IContainer components = null;
  61. /// <summary>
  62. /// Gets or sets a value indicating whether this instance is render percent.
  63. /// </summary>
  64. /// <value><c>true</c> if this instance is render percent; otherwise, <c>false</c>.</value>
  65. [Browsable(true)]
  66. [Category("自定义")]
  67. [DefaultValue(false)]
  68. [Description("获取或设置是否显示百分比占用")]
  69. public bool IsRenderPercent
  70. {
  71. get
  72. {
  73. return m_IsRenderPercent;
  74. }
  75. set
  76. {
  77. m_IsRenderPercent = value;
  78. Invalidate();
  79. }
  80. }
  81. /// <summary>
  82. /// Gets or sets the text margin.
  83. /// </summary>
  84. /// <value>The text margin.</value>
  85. [Browsable(true)]
  86. [Category("自定义")]
  87. [Description("获取或设置文本距离,单位为像素,默认50")]
  88. [DefaultValue(50)]
  89. public int TextMargin
  90. {
  91. get
  92. {
  93. return margin;
  94. }
  95. set
  96. {
  97. margin = value;
  98. Invalidate();
  99. }
  100. }
  101. /// <summary>
  102. /// Gets or sets the percent format.
  103. /// </summary>
  104. /// <value>The percent format.</value>
  105. [Browsable(true)]
  106. [Category("自定义")]
  107. [Description("获取或设置文百分比文字的格式化信息")]
  108. [DefaultValue("{0:F2}%")]
  109. public string PercentFormat
  110. {
  111. get
  112. {
  113. return percenFormat;
  114. }
  115. set
  116. {
  117. percenFormat = value;
  118. Invalidate();
  119. }
  120. }
  121. /// <summary>
  122. /// The center of circle color
  123. /// </summary>
  124. private Color centerOfCircleColor = Color.White;
  125. /// <summary>
  126. /// Gets or sets the color of the center of circle.
  127. /// </summary>
  128. /// <value>The color of the center of circle.</value>
  129. [Browsable(true)]
  130. [Category("自定义")]
  131. [Description("获取或设置圆心颜色")]
  132. public Color CenterOfCircleColor
  133. {
  134. get { return centerOfCircleColor; }
  135. set
  136. {
  137. centerOfCircleColor = value;
  138. Invalidate();
  139. }
  140. }
  141. /// <summary>
  142. /// The center of circle width
  143. /// </summary>
  144. private int centerOfCircleWidth = 0;
  145. /// <summary>
  146. /// Gets or sets the width of the center of circle.
  147. /// </summary>
  148. /// <value>The width of the center of circle.</value>
  149. [Browsable(true)]
  150. [Category("自定义")]
  151. [Description("获取或设置圆心宽度")]
  152. public int CenterOfCircleWidth
  153. {
  154. get { return centerOfCircleWidth; }
  155. set
  156. {
  157. if (value < 0)
  158. return;
  159. centerOfCircleWidth = value;
  160. Invalidate();
  161. }
  162. }
  163. /// <summary>
  164. /// The title
  165. /// </summary>
  166. private string title;
  167. /// <summary>
  168. /// Gets or sets the ti tle.
  169. /// </summary>
  170. /// <value>The ti tle.</value>
  171. [Browsable(true)]
  172. [Category("自定义")]
  173. [Description("获取或设置标题")]
  174. public string TiTle
  175. {
  176. get { return title; }
  177. set
  178. {
  179. title = value;
  180. ResetTitleHeight();
  181. Invalidate();
  182. }
  183. }
  184. /// <summary>
  185. /// The title font
  186. /// </summary>
  187. private Font titleFont = new Font("微软雅黑", 12);
  188. /// <summary>
  189. /// Gets or sets the title font.
  190. /// </summary>
  191. /// <value>The title font.</value>
  192. [Browsable(true)]
  193. [Category("自定义")]
  194. [Description("获取或设置标题字体")]
  195. public Font TitleFont
  196. {
  197. get { return titleFont; }
  198. set
  199. {
  200. titleFont = value;
  201. ResetTitleHeight();
  202. Invalidate();
  203. }
  204. }
  205. /// <summary>
  206. /// The title froe color
  207. /// </summary>
  208. private Color titleFroeColor = Color.Black;
  209. /// <summary>
  210. /// Gets or sets the color of the title froe.
  211. /// </summary>
  212. /// <value>The color of the title froe.</value>
  213. [Browsable(true)]
  214. [Category("自定义")]
  215. [Description("获取或设置标题颜色")]
  216. public Color TitleFroeColor
  217. {
  218. get { return titleFroeColor; }
  219. set
  220. {
  221. titleFroeColor = value;
  222. Invalidate();
  223. }
  224. }
  225. /// <summary>
  226. /// The title size
  227. /// </summary>
  228. private SizeF titleSize = SizeF.Empty;
  229. /// <summary>
  230. /// Resets the height of the title.
  231. /// </summary>
  232. private void ResetTitleHeight()
  233. {
  234. if (string.IsNullOrEmpty(title))
  235. titleSize = SizeF.Empty;
  236. else
  237. {
  238. using (var g = this.CreateGraphics())
  239. {
  240. titleSize = g.MeasureString(title, titleFont);
  241. }
  242. }
  243. }
  244. /// <summary>
  245. /// Gets or sets the data source.
  246. /// </summary>
  247. /// <value>The data source.</value>
  248. [Browsable(true)]
  249. [Category("自定义")]
  250. [Description("获取或设置标题颜色")]
  251. [Localizable(true)]
  252. public PieItem[] DataSource
  253. {
  254. get { return pieItems; }
  255. set
  256. {
  257. pieItems = value;
  258. Invalidate();
  259. }
  260. }
  261. /// <summary>
  262. /// Initializes a new instance of the <see cref="UCPieChart"/> class.
  263. /// </summary>
  264. public UCPieChart()
  265. {
  266. InitializeComponent();
  267. random = new Random();
  268. formatCenter = new StringFormat();
  269. formatCenter.Alignment = StringAlignment.Center;
  270. formatCenter.LineAlignment = StringAlignment.Center;
  271. SetStyle(ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true);
  272. SetStyle(ControlStyles.ResizeRedraw, true);
  273. SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  274. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  275. pieItems = new PieItem[0];
  276. if (GetService(typeof(IDesignerHost)) != null || LicenseManager.UsageMode == LicenseUsageMode.Designtime)
  277. {
  278. pieItems = new PieItem[5];
  279. for (int i = 0; i < 5; i++)
  280. {
  281. pieItems[i] = new PieItem
  282. {
  283. Name = "Source" + (i + 1),
  284. Value = random.Next(10, 100)
  285. };
  286. }
  287. }
  288. }
  289. /// <summary>
  290. /// Gets the center point.
  291. /// </summary>
  292. /// <param name="width">The width.</param>
  293. /// <returns>Point.</returns>
  294. private Point GetCenterPoint(out int width)
  295. {
  296. width = Math.Min(base.Width, base.Height - (titleSize != SizeF.Empty ? ((int)titleSize.Height) : 0)) / 2 - margin - 8;
  297. return new Point(base.Width / 2 - 1, base.Height / 2 + (titleSize != SizeF.Empty ? ((int)titleSize.Height) : 0) - 1);
  298. }
  299. /// <summary>
  300. /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
  301. /// </summary>
  302. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" /></param>
  303. protected override void OnPaint(PaintEventArgs e)
  304. {
  305. e.Graphics.SetGDIHigh();
  306. int width;
  307. Point centerPoint = GetCenterPoint(out width);
  308. Rectangle rectangle = new Rectangle(centerPoint.X - width, centerPoint.Y - width, width * 2, width * 2);
  309. if (width > 0 && pieItems.Length != 0)
  310. {
  311. if (!string.IsNullOrEmpty(title))
  312. e.Graphics.DrawString(title, titleFont, new SolidBrush(titleFroeColor), new PointF((this.Width - titleSize.Width) / 2, 5));
  313. Rectangle rect = new Rectangle(rectangle.X - centerPoint.X, rectangle.Y - centerPoint.Y, rectangle.Width, rectangle.Height);
  314. e.Graphics.TranslateTransform(centerPoint.X, centerPoint.Y);
  315. e.Graphics.RotateTransform(90f);
  316. int num = pieItems.Sum((PieItem item) => item.Value);
  317. float num2 = 0f;
  318. float num3 = -90f;
  319. for (int i = 0; i < pieItems.Length; i++)
  320. {
  321. Color cItem = pieItems[i].PieColor ?? ControlHelper.Colors[i];
  322. Pen pen = new Pen(cItem, 1f);
  323. SolidBrush solidBrush = new SolidBrush(cItem);
  324. SolidBrush solidBrush2 = new SolidBrush(cItem);
  325. Brush percentBrush = new SolidBrush(cItem);
  326. float num4 = e.Graphics.MeasureString(pieItems[i].Name, Font).Width + 3f;
  327. float num5 = (num != 0) ? Convert.ToSingle((double)pieItems[i].Value * 1.0 / (double)num * 360.0) : ((float)(360 / pieItems.Length));
  328. e.Graphics.FillPie(solidBrush, rect, 0f, 0f - num5);
  329. e.Graphics.DrawPie(new Pen(solidBrush), rect, 0f, 0f - num5);
  330. e.Graphics.RotateTransform(0f - num5 / 2f);
  331. if (num5 < 2f)
  332. {
  333. num2 += num5;
  334. }
  335. else
  336. {
  337. num2 += num5 / 2f;
  338. int num6 = 15;
  339. if (num2 < 45f || num2 > 315f)
  340. {
  341. num6 = 20;
  342. }
  343. if (num2 > 135f && num2 < 225f)
  344. {
  345. num6 = 20;
  346. }
  347. e.Graphics.DrawLine(pen, width * 2 / 3, 0, width + num6, 0);
  348. e.Graphics.TranslateTransform(width + num6, 0f);
  349. if (num2 - num3 < 5f)
  350. {
  351. }
  352. num3 = num2;
  353. if (num2 < 180f)
  354. {
  355. e.Graphics.RotateTransform(num2 - 90f);
  356. e.Graphics.DrawLine(pen, 0f, 0f, num4, 0f);
  357. e.Graphics.DrawString(pieItems[i].Name, Font, solidBrush2, new Point(0, -Font.Height));
  358. if (IsRenderPercent)
  359. {
  360. e.Graphics.DrawString(string.Format(percenFormat, num5 * 100f / 360f), Font, percentBrush, new Point(0, 1));
  361. }
  362. e.Graphics.RotateTransform(90f - num2);
  363. }
  364. else
  365. {
  366. e.Graphics.RotateTransform(num2 - 270f);
  367. e.Graphics.DrawLine(pen, 0f, 0f, num4, 0f);
  368. e.Graphics.TranslateTransform(num4 - 3f, 0f);
  369. e.Graphics.RotateTransform(180f);
  370. e.Graphics.DrawString(pieItems[i].Name, Font, solidBrush2, new Point(0, -Font.Height));
  371. if (IsRenderPercent)
  372. {
  373. e.Graphics.DrawString(string.Format(percenFormat, num5 * 100f / 360f), Font, percentBrush, new Point(0, 1));
  374. }
  375. e.Graphics.RotateTransform(-180f);
  376. e.Graphics.TranslateTransform(0f - num4 + 3f, 0f);
  377. e.Graphics.RotateTransform(270f - num2);
  378. }
  379. e.Graphics.TranslateTransform(-width - num6, 0f);
  380. e.Graphics.RotateTransform(0f - num5 / 2f);
  381. num2 += num5 / 2f;
  382. }
  383. solidBrush.Dispose();
  384. pen.Dispose();
  385. solidBrush2.Dispose();
  386. percentBrush.Dispose();
  387. }
  388. e.Graphics.ResetTransform();
  389. if (centerOfCircleWidth > 0)
  390. {
  391. Rectangle rectCenter = new Rectangle(rect.Left + rect.Width / 2 - centerOfCircleWidth / 2, rect.Top + rect.Height / 2 - centerOfCircleWidth / 2, centerOfCircleWidth, centerOfCircleWidth);
  392. e.Graphics.FillEllipse(new SolidBrush(centerOfCircleColor), rectCenter);
  393. }
  394. }
  395. else
  396. {
  397. e.Graphics.FillEllipse(Brushes.AliceBlue, rectangle);
  398. e.Graphics.DrawEllipse(Pens.DodgerBlue, rectangle);
  399. e.Graphics.DrawString("无数据", Font, Brushes.DimGray, rectangle, formatCenter);
  400. }
  401. base.OnPaint(e);
  402. }
  403. /// <summary>
  404. /// Sets the data source.
  405. /// </summary>
  406. /// <param name="source">The source.</param>
  407. public void SetDataSource(PieItem[] source)
  408. {
  409. if (source != null)
  410. {
  411. DataSource = source;
  412. }
  413. }
  414. /// <summary>
  415. /// Sets the data source.
  416. /// </summary>
  417. /// <param name="names">The names.</param>
  418. /// <param name="values">The values.</param>
  419. /// <exception cref="System.ArgumentNullException">
  420. /// names
  421. /// or
  422. /// values
  423. /// </exception>
  424. /// <exception cref="System.Exception">两个数组的长度不一致!</exception>
  425. public void SetDataSource(string[] names, int[] values)
  426. {
  427. if (names == null)
  428. {
  429. throw new ArgumentNullException("names");
  430. }
  431. if (values == null)
  432. {
  433. throw new ArgumentNullException("values");
  434. }
  435. if (names.Length != values.Length)
  436. {
  437. throw new Exception("两个数组的长度不一致!");
  438. }
  439. pieItems = new PieItem[names.Length];
  440. for (int i = 0; i < names.Length; i++)
  441. {
  442. pieItems[i] = new PieItem
  443. {
  444. Name = names[i],
  445. Value = values[i]
  446. };
  447. }
  448. Invalidate();
  449. }
  450. /// <summary>
  451. /// Releases unmanaged and - optionally - managed resources.
  452. /// </summary>
  453. /// <param name="disposing">为 true 则释放托管资源和非托管资源;为 false 则仅释放非托管资源。</param>
  454. protected override void Dispose(bool disposing)
  455. {
  456. if (disposing && components != null)
  457. {
  458. components.Dispose();
  459. }
  460. base.Dispose(disposing);
  461. }
  462. /// <summary>
  463. /// Initializes the component.
  464. /// </summary>
  465. private void InitializeComponent()
  466. {
  467. SuspendLayout();
  468. base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
  469. BackColor = System.Drawing.Color.Transparent;
  470. base.Name = "UCPieChart";
  471. ResumeLayout(false);
  472. }
  473. }
  474. }

 

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/270930
推荐阅读
相关标签
  

闽ICP备14008679号