饼图的绘制
private Color selectcolor=Color.Green;
private ArrayList duixiang=new ArrayList();
private void button3_Click(object sender, System.EventArgs e)
{
float data=0;
try
{
data=float.Parse(this.textBox1.Text);
}
catch{}
Class1 cl1=new Class1(data,selectcolor);
duixiang.Add(cl1); //将新对象添加进duixiang.
this.draw(); //绘制
Random rand = new Random();
this.selectcolor = Color.FromArgb(rand.Next(255), rand.Next(255), rand.Next(255));
this.label1.BackColor = this.selectcolor;
this.label1.Text=selectcolor.Name.ToString();
}
private void draw()
{
float startangle=0.0f; //开始角度
float total=0.0f; //总体数据。
Graphics gs=this.panel1.CreateGraphics();
Rectangle rect = new Rectangle(10,10,150,150); //定义矩形位置大小
foreach(Class1 cl1 in duixiang)
{
total+=cl1.share; //计算总体的数据
}
gs.Clear(this.panel1.BackColor);
for (int i=0;i<duixiang.Count;i++)
{
Class1 cls=(Class1)duixiang[i];
float endangle=cls.share/total *360; //结束绘制的角度.
Pen pen = new Pen(this.panel1.BackColor);
SolidBrush sBrush = new SolidBrush(cls.color);
gs.DrawArc(pen,rect,startangle,endangle); //绘制弧线
gs.FillPie(sBrush,rect,startangle,endangle); //填充扇形
startangle += endangle; //重新计算开始绘制的角度
}
}
public class Class1
{
public float share;
public Color color;
public Class1(float share,Color color)
{
this.share=share;
this.color=color;
}
}
private ArrayList duixiang=new ArrayList();
private void button3_Click(object sender, System.EventArgs e)
{
float data=0;
try
{
data=float.Parse(this.textBox1.Text);
}
catch{}
Class1 cl1=new Class1(data,selectcolor);
duixiang.Add(cl1); //将新对象添加进duixiang.
this.draw(); //绘制
Random rand = new Random();
this.selectcolor = Color.FromArgb(rand.Next(255), rand.Next(255), rand.Next(255));
this.label1.BackColor = this.selectcolor;
this.label1.Text=selectcolor.Name.ToString();
}
private void draw()
{
float startangle=0.0f; //开始角度
float total=0.0f; //总体数据。
Graphics gs=this.panel1.CreateGraphics();
Rectangle rect = new Rectangle(10,10,150,150); //定义矩形位置大小
foreach(Class1 cl1 in duixiang)
{
total+=cl1.share; //计算总体的数据
}
gs.Clear(this.panel1.BackColor);
for (int i=0;i<duixiang.Count;i++)
{
Class1 cls=(Class1)duixiang[i];
float endangle=cls.share/total *360; //结束绘制的角度.
Pen pen = new Pen(this.panel1.BackColor);
SolidBrush sBrush = new SolidBrush(cls.color);
gs.DrawArc(pen,rect,startangle,endangle); //绘制弧线
gs.FillPie(sBrush,rect,startangle,endangle); //填充扇形
startangle += endangle; //重新计算开始绘制的角度
}
}
public class Class1
{
public float share;
public Color color;
public Class1(float share,Color color)
{
this.share=share;
this.color=color;
}
}