(七十)c#Winform自定义控件-饼状图
程序员文章站
2022-05-18 17:13:50
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_contr ......
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
github:https://github.com/kwwwvagaa/netwinformcontrol
码云:
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492
麻烦博客下方点个【推荐】,谢谢
nuget
install-package hzh_controls
目录
用处及效果
准备工作
使用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 25 namespace hzh_controls.controls 26 { 27 /// <summary> 28 /// class ucpiechart. 29 /// implements the <see cref="system.windows.forms.usercontrol" /> 30 /// </summary> 31 /// <seealso cref="system.windows.forms.usercontrol" /> 32 public class ucpiechart : usercontrol 33 { 34 /// <summary> 35 /// the pie items 36 /// </summary> 37 private pieitem[] pieitems = new pieitem[0]; 38 39 /// <summary> 40 /// the random 41 /// </summary> 42 private random random = null; 43 44 /// <summary> 45 /// the format center 46 /// </summary> 47 private stringformat formatcenter = null; 48 49 /// <summary> 50 /// the margin 51 /// </summary> 52 private int margin = 50; 53 54 /// <summary> 55 /// the m is render percent 56 /// </summary> 57 private bool m_isrenderpercent = false; 58 59 /// <summary> 60 /// the percen format 61 /// </summary> 62 private string percenformat = "{0:f2}%"; 63 64 /// <summary> 65 /// the components 66 /// </summary> 67 private icontainer components = null; 68 69 /// <summary> 70 /// gets or sets a value indicating whether this instance is render percent. 71 /// </summary> 72 /// <value><c>true</c> if this instance is render percent; otherwise, <c>false</c>.</value> 73 [browsable(true)] 74 [category("自定义")] 75 [defaultvalue(false)] 76 [description("获取或设置是否显示百分比占用")] 77 public bool isrenderpercent 78 { 79 get 80 { 81 return m_isrenderpercent; 82 } 83 set 84 { 85 m_isrenderpercent = value; 86 invalidate(); 87 } 88 } 89 90 91 /// <summary> 92 /// gets or sets the text margin. 93 /// </summary> 94 /// <value>the text margin.</value> 95 [browsable(true)] 96 [category("自定义")] 97 [description("获取或设置文本距离,单位为像素,默认50")] 98 [defaultvalue(50)] 99 public int textmargin 100 { 101 get 102 { 103 return margin; 104 } 105 set 106 { 107 margin = value; 108 invalidate(); 109 } 110 } 111 112 113 /// <summary> 114 /// gets or sets the percent format. 115 /// </summary> 116 /// <value>the percent format.</value> 117 [browsable(true)] 118 [category("自定义")] 119 [description("获取或设置文百分比文字的格式化信息")] 120 [defaultvalue("{0:f2}%")] 121 public string percentformat 122 { 123 get 124 { 125 return percenformat; 126 } 127 set 128 { 129 percenformat = value; 130 invalidate(); 131 } 132 } 133 134 /// <summary> 135 /// the center of circle color 136 /// </summary> 137 private color centerofcirclecolor = color.white; 138 /// <summary> 139 /// gets or sets the color of the center of circle. 140 /// </summary> 141 /// <value>the color of the center of circle.</value> 142 [browsable(true)] 143 [category("自定义")] 144 [description("获取或设置圆心颜色")] 145 public color centerofcirclecolor 146 { 147 get { return centerofcirclecolor; } 148 set 149 { 150 centerofcirclecolor = value; 151 invalidate(); 152 } 153 } 154 155 /// <summary> 156 /// the center of circle width 157 /// </summary> 158 private int centerofcirclewidth = 0; 159 /// <summary> 160 /// gets or sets the width of the center of circle. 161 /// </summary> 162 /// <value>the width of the center of circle.</value> 163 [browsable(true)] 164 [category("自定义")] 165 [description("获取或设置圆心宽度")] 166 public int centerofcirclewidth 167 { 168 get { return centerofcirclewidth; } 169 set 170 { 171 if (value < 0) 172 return; 173 centerofcirclewidth = value; 174 invalidate(); 175 } 176 } 177 178 /// <summary> 179 /// the title 180 /// </summary> 181 private string title; 182 /// <summary> 183 /// gets or sets the ti tle. 184 /// </summary> 185 /// <value>the ti tle.</value> 186 [browsable(true)] 187 [category("自定义")] 188 [description("获取或设置标题")] 189 public string title 190 { 191 get { return title; } 192 set 193 { 194 title = value; 195 resettitleheight(); 196 invalidate(); 197 } 198 } 199 /// <summary> 200 /// the title font 201 /// </summary> 202 private font titlefont = new font("微软雅黑", 12); 203 /// <summary> 204 /// gets or sets the title font. 205 /// </summary> 206 /// <value>the title font.</value> 207 [browsable(true)] 208 [category("自定义")] 209 [description("获取或设置标题字体")] 210 public font titlefont 211 { 212 get { return titlefont; } 213 set 214 { 215 titlefont = value; 216 resettitleheight(); 217 invalidate(); 218 } 219 } 220 221 /// <summary> 222 /// the title froe color 223 /// </summary> 224 private color titlefroecolor = color.black; 225 /// <summary> 226 /// gets or sets the color of the title froe. 227 /// </summary> 228 /// <value>the color of the title froe.</value> 229 [browsable(true)] 230 [category("自定义")] 231 [description("获取或设置标题颜色")] 232 public color titlefroecolor 233 { 234 get { return titlefroecolor; } 235 set 236 { 237 titlefroecolor = value; 238 invalidate(); 239 } 240 } 241 242 /// <summary> 243 /// the title size 244 /// </summary> 245 private sizef titlesize = sizef.empty; 246 /// <summary> 247 /// resets the height of the title. 248 /// </summary> 249 private void resettitleheight() 250 { 251 if (string.isnullorempty(title)) 252 titlesize = sizef.empty; 253 else 254 { 255 using (var g = this.creategraphics()) 256 { 257 titlesize = g.measurestring(title, titlefont); 258 } 259 } 260 } 261 262 /// <summary> 263 /// gets or sets the data source. 264 /// </summary> 265 /// <value>the data source.</value> 266 [browsable(true)] 267 [category("自定义")] 268 [description("获取或设置标题颜色")] 269 [localizable(true)] 270 public pieitem[] datasource 271 { 272 get { return pieitems; } 273 set 274 { 275 pieitems = value; 276 invalidate(); 277 } 278 } 279 280 /// <summary> 281 /// initializes a new instance of the <see cref="ucpiechart"/> class. 282 /// </summary> 283 public ucpiechart() 284 { 285 initializecomponent(); 286 random = new random(); 287 formatcenter = new stringformat(); 288 formatcenter.alignment = stringalignment.center; 289 formatcenter.linealignment = stringalignment.center; 290 setstyle(controlstyles.userpaint | controlstyles.supportstransparentbackcolor, true); 291 setstyle(controlstyles.resizeredraw, true); 292 setstyle(controlstyles.optimizeddoublebuffer, true); 293 setstyle(controlstyles.allpaintinginwmpaint, true); 294 pieitems = new pieitem[0]; 295 if (getservice(typeof(idesignerhost)) != null || licensemanager.usagemode == licenseusagemode.designtime) 296 { 297 pieitems = new pieitem[5]; 298 299 for (int i = 0; i < 5; i++) 300 { 301 pieitems[i] = new pieitem 302 { 303 name = "source" + (i + 1), 304 value = random.next(10, 100) 305 }; 306 } 307 } 308 } 309 310 311 312 /// <summary> 313 /// gets the center point. 314 /// </summary> 315 /// <param name="width">the width.</param> 316 /// <returns>point.</returns> 317 private point getcenterpoint(out int width) 318 { 319 width = math.min(base.width, base.height - (titlesize != sizef.empty ? ((int)titlesize.height) : 0)) / 2 - margin - 8; 320 return new point(base.width / 2 - 1, base.height / 2 + (titlesize != sizef.empty ? ((int)titlesize.height) : 0) - 1); 321 } 322 323 324 /// <summary> 325 /// 引发 <see cref="e:system.windows.forms.control.paint" /> 事件。 326 /// </summary> 327 /// <param name="e">包含事件数据的 <see cref="t:system.windows.forms.painteventargs" />。</param> 328 protected override void onpaint(painteventargs e) 329 { 330 e.graphics.setgdihigh(); 331 332 int width; 333 point centerpoint = getcenterpoint(out width); 334 rectangle rectangle = new rectangle(centerpoint.x - width, centerpoint.y - width, width * 2, width * 2); 335 if (width > 0 && pieitems.length != 0) 336 { 337 if (!string.isnullorempty(title)) 338 e.graphics.drawstring(title, titlefont, new solidbrush(titlefroecolor), new pointf((this.width - titlesize.width) / 2, 5)); 339 rectangle rect = new rectangle(rectangle.x - centerpoint.x, rectangle.y - centerpoint.y, rectangle.width, rectangle.height); 340 e.graphics.translatetransform(centerpoint.x, centerpoint.y); 341 e.graphics.rotatetransform(90f); 342 int num = pieitems.sum((pieitem item) => item.value); 343 float num2 = 0f; 344 float num3 = -90f; 345 for (int i = 0; i < pieitems.length; i++) 346 { 347 color citem = pieitems[i].piecolor ?? controlhelper.colors[i]; 348 pen pen = new pen(citem, 1f); 349 solidbrush solidbrush = new solidbrush(citem); 350 solidbrush solidbrush2 = new solidbrush(citem); 351 brush percentbrush = new solidbrush(citem); 352 float num4 = e.graphics.measurestring(pieitems[i].name, font).width + 3f; 353 float num5 = (num != 0) ? convert.tosingle((double)pieitems[i].value * 1.0 / (double)num * 360.0) : ((float)(360 / pieitems.length)); 354 e.graphics.fillpie(solidbrush, rect, 0f, 0f - num5); 355 e.graphics.drawpie(new pen(solidbrush), rect, 0f, 0f - num5); 356 e.graphics.rotatetransform(0f - num5 / 2f); 357 if (num5 < 2f) 358 { 359 num2 += num5; 360 } 361 else 362 { 363 num2 += num5 / 2f; 364 int num6 = 15; 365 if (num2 < 45f || num2 > 315f) 366 { 367 num6 = 20; 368 } 369 if (num2 > 135f && num2 < 225f) 370 { 371 num6 = 20; 372 } 373 e.graphics.drawline(pen, width * 2 / 3, 0, width + num6, 0); 374 e.graphics.translatetransform(width + num6, 0f); 375 if (num2 - num3 < 5f) 376 { 377 } 378 num3 = num2; 379 if (num2 < 180f) 380 { 381 e.graphics.rotatetransform(num2 - 90f); 382 e.graphics.drawline(pen, 0f, 0f, num4, 0f); 383 e.graphics.drawstring(pieitems[i].name, font, solidbrush2, new point(0, -font.height)); 384 if (isrenderpercent) 385 { 386 e.graphics.drawstring(string.format(percenformat, num5 * 100f / 360f), font, percentbrush, new point(0, 1)); 387 } 388 e.graphics.rotatetransform(90f - num2); 389 } 390 else 391 { 392 e.graphics.rotatetransform(num2 - 270f); 393 e.graphics.drawline(pen, 0f, 0f, num4, 0f); 394 e.graphics.translatetransform(num4 - 3f, 0f); 395 e.graphics.rotatetransform(180f); 396 e.graphics.drawstring(pieitems[i].name, font, solidbrush2, new point(0, -font.height)); 397 if (isrenderpercent) 398 { 399 e.graphics.drawstring(string.format(percenformat, num5 * 100f / 360f), font, percentbrush, new point(0, 1)); 400 } 401 e.graphics.rotatetransform(-180f); 402 e.graphics.translatetransform(0f - num4 + 3f, 0f); 403 e.graphics.rotatetransform(270f - num2); 404 } 405 e.graphics.translatetransform(-width - num6, 0f); 406 e.graphics.rotatetransform(0f - num5 / 2f); 407 num2 += num5 / 2f; 408 } 409 solidbrush.dispose(); 410 pen.dispose(); 411 solidbrush2.dispose(); 412 percentbrush.dispose(); 413 } 414 e.graphics.resettransform(); 415 416 if (centerofcirclewidth > 0) 417 { 418 rectangle rectcenter = new rectangle(rect.left + rect.width / 2 - centerofcirclewidth / 2, rect.top + rect.height / 2 - centerofcirclewidth / 2, centerofcirclewidth, centerofcirclewidth); 419 e.graphics.fillellipse(new solidbrush(centerofcirclecolor), rectcenter); 420 } 421 } 422 else 423 { 424 e.graphics.fillellipse(brushes.aliceblue, rectangle); 425 e.graphics.drawellipse(pens.dodgerblue, rectangle); 426 e.graphics.drawstring("无数据", font, brushes.dimgray, rectangle, formatcenter); 427 } 428 base.onpaint(e); 429 } 430 /// <summary> 431 /// sets the data source. 432 /// </summary> 433 /// <param name="source">the source.</param> 434 public void setdatasource(pieitem[] source) 435 { 436 if (source != null) 437 { 438 datasource = source; 439 } 440 } 441 /// <summary> 442 /// sets the data source. 443 /// </summary> 444 /// <param name="names">the names.</param> 445 /// <param name="values">the values.</param> 446 /// <exception cref="system.argumentnullexception"> 447 /// names 448 /// or 449 /// values 450 /// </exception> 451 /// <exception cref="system.exception">两个数组的长度不一致!</exception> 452 public void setdatasource(string[] names, int[] values) 453 { 454 if (names == null) 455 { 456 throw new argumentnullexception("names"); 457 } 458 if (values == null) 459 { 460 throw new argumentnullexception("values"); 461 } 462 if (names.length != values.length) 463 { 464 throw new exception("两个数组的长度不一致!"); 465 } 466 pieitems = new pieitem[names.length]; 467 for (int i = 0; i < names.length; i++) 468 { 469 pieitems[i] = new pieitem 470 { 471 name = names[i], 472 value = values[i] 473 }; 474 } 475 invalidate(); 476 } 477 478 /// <summary> 479 /// releases unmanaged and - optionally - managed resources. 480 /// </summary> 481 /// <param name="disposing">为 true 则释放托管资源和非托管资源;为 false 则仅释放非托管资源。</param> 482 protected override void dispose(bool disposing) 483 { 484 if (disposing && components != null) 485 { 486 components.dispose(); 487 } 488 base.dispose(disposing); 489 } 490 491 /// <summary> 492 /// initializes the component. 493 /// </summary> 494 private void initializecomponent() 495 { 496 suspendlayout(); 497 base.autoscalemode = system.windows.forms.autoscalemode.none; 498 backcolor = system.drawing.color.transparent; 499 base.name = "ucpiechart"; 500 resumelayout(false); 501 } 502 } 503 }
最后的话
如果你喜欢的话,请到 点个星星吧