自定义控件圆饼图的几点技巧.doc

原代码:

using System;

using System.Collections;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Windows.Forms;

 

namespace ybt

{

     /// <summary>

     /// ybt 的摘要说明。

     /// </summary>

     public class ybt : System.Windows.Forms.PictureBox

     {

         /// <summary>

         /// 必需的设计器变量。

         /// </summary>

          private System.ComponentModel.Container components = null;

          private ArrayList  ArrData;

          private ArrayList  ArrItem ;

 

         public ybt()

         {

              // 该调用是 Windows.Forms 窗体设计器所必需的。

              InitializeComponent();

 

              // TODO: 在 InitComponent 调用后添加任何初始化

 

              ArrData = new ArrayList();

              ArrItem = new ArrayList();

              AddItem ("aaa",50);

              AddItem ("bbb",35);

              AddItem ("ccc",68);

              AddItem ("ddd",45);

              AddItem ("eee",55);

              AddItem ("fff",45);

              Invalidate();

         }

 

         public void AddItem(string str,float f)

         {

              if (ArrItem.Count<16)

              {

                   ArrItem.Add(str);

                   ArrData.Add(f);

                   this.Invalidate();

              }

         }

 

         public void Clear()

         {

              ArrItem.Clear();

              ArrData.Clear();

         }

 

         /// <summary>

         /// 清理所有正在使用的资源。

         /// </summary>

          protected override void Dispose( bool disposing )

         {

              if( disposing )

              {

                   if( components != null )

                        components.Dispose();

              }

              base.Dispose( disposing );

         }

 

          #region 组件设计器生成的代码

         /// <summary>

         /// 设计器支持所需的方法 - 不要使用代码编辑器

         /// 修改此方法的内容。

         /// </summary>

          private void InitializeComponent()

         {

              //

              // ybt

              //

              this.Size = new System.Drawing.Size(250, 75);

              this.Resize += new System.EventHandler(this.ybt_Resize);

              this.Paint += new System.Windows.Forms.PaintEventHandler(this.ybt_Paint);

 

         }

          #endregion

 

          private void ybt_Load(object sender, System.EventArgs e)

         {

             

         }

 

          private void ybt_Paint(object sender, System.Windows.Forms.PaintEventArgs e)

         {

              Pen myPen,p;

              SolidBrush myBrush,br;

              Color [] color={Color.Green ,Color.GreenYellow,Color.LightBlue ,Color.LightYellow ,Color.MediumVioletRed,Color.PowderBlue,Color.SeaGreen,Color.Tan,Color.Tomato,Color.RosyBrown,Color.Red,Color.PaleGoldenrod,Color.Orange,Color.MistyRose,Color.MediumOrchid,Color.LightGray};

              float start = 0;

              float sweep = 0;

              int i = 0;

              int j = 0;

              Font f;

              float sum = 0;

              int W=0,H=0;

              W=this.Width - 200;

              H=this.Height -150;

              myPen = new Pen(Color.Gold,3);

              Graphics g = e.Graphics;

              f=new Font ("宋体",12);

              br=new SolidBrush (Color.DarkBlue );

              for(i=0;i<ArrItem.Count;i++)

              {

                   sum+= (float)ArrData[i];

              }

              for(i=0;i<ArrItem.Count;i++)

              {

                   sweep=(float)ArrData[i]/sum*360;

                   myBrush = new SolidBrush(color[i]);

                   g.FillPie(myBrush,0,0,W,H,start,sweep);

                   g.DrawPie(myPen,0,0,W,H,start,sweep);

                   for(j=0;j<20;j++)

                   {

                       p = new Pen(color[i]);

                        if(start < 180 )

                       {

                            if(start+sweep<180)

                            {

                                 g.DrawArc(p,0,20-j,W,H,start,sweep);

                            }

                            else

                            {

                            g.DrawArc(p,0,20-j,W,H,start,180);

                            }

                           

                       }

                        g.FillRectangle (myBrush,W+20,20+i*30,20,20);

                        g.DrawRectangle (myPen,W+20,20+i*30,20,20);

                       float ft = (float)ArrData[i]/sum*100;

                        g.DrawString( ArrItem[i].ToString ()+string.Format("     {0,4:0.00}",ft) +"%",f,br,W+60,20+i*30);              

                   }

                   start+=sweep;

              }

              if (ArrItem.Count > 0)

              {

                   g.DrawArc(myPen,0,20,W,H,0,180);

 

              }

 

         }

 

          private void ybt_Resize(object sender, System.EventArgs e)

         {

              if (this.Width <550 )

              {

                   this.Width = 550;                

              }

              if (this.Height <250)

              {

                   this.Height = 250;

              }

         }

     }

}

一、函数Invalidate()

在圆饼图控件中,圆饼图在Paint事件中进行绘制。当你在你的工程文件中使用本控件的时候,会发现只有当你的窗体重新绘制圆饼图的时候,才能看见。为了解决这个问题,就需要在AddItem方法和构造函数中使用函数Invalidate()。函数Invalidate()就是发给窗体一个WM-PAINT的消息,去触发paint事件来重新绘制圆饼图。

二、string.Format方法

可以对字符串进行格式化。

三、Resize中设定圆饼图的最小的大小。因为图形太小的话,就不美观了,所以要设定最小的尺度。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值