form代码:
DarwGrapClass drawpic = new DarwGrapClass();
string topic = "2009-2012年成绩统计表";
string[] xmess = { "第一学期", "第二学期", "第三学期", "第四学期", "第五学期", "第六学期", "第七学期"};
int[] county = { 45, 25, 50, 10, 150, 140, 160};
Bitmap pic = drawpic.CreatePieImage(pictureBox1.Width,
pictureBox1.Height, topic, county, xmess);
this.pictureBox1.Image = pic;
类的代码:
/// <summary>
/// 绘制柱状统计图
/// </summary>
/// <returns></returns>
public Bitmap CreateZhuImage(int width, int height, string topic, int beginy, int partx, int beginx, int party, string[] xmess, string[] ymess, int[] county, int ymax, int xmax)
{
Bitmap image = new Bitmap(width, height);
//创建Graphics类对象
Graphics g = Graphics.FromImage(image);
try
{
//清空图片背景色
g.Clear(Color.White);
Font font = new Font("Arial", 10, FontStyle.Regular);
Font font1 = new Font("宋体", 15, FontStyle.Bold);
//绘制间线性渐变Brush对象,读者可以自行改变该笔刷式样
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
Color.Blue, Color.BlueViolet, 1.2f, true);
//图片的底色为白烟色
g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);
//在图片上面写标题文字,请注意文字出现的坐标点(width/4, height-20),读者可以根据实际情况
//适当调整文字出现的坐标点
g.DrawString(topic, font1, brush, new PointF(width / 4, height - 20));
//画图片的边框线
g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 20);
///==================下面开始绘制线条===================
/////-------------------首先绘制XY坐标轴------------
Pen mypen = new Pen(brush, 1); //这个pen对象将绘制内部的网格线,1像素
Pen mypen1 = new Pen(Color.Blue, 2); //这个pen对象将绘制XY对象,2像素,蓝色
//如果将下面的注释打开,将绘制带箭头的直线,此时mypen1对象至少需设为3像素,否则不明显
//mypen1.StartCap = LineCap.ArrowAnchor;
//计算剩余的x轴长度(xLength),并根据x轴等分原则计算每分的长度(xpart)
int xlength = image.Width - beginy;
int xpart = Convert.ToInt32(xlength / partx);
//计算剩余的Y轴长度(ylength),并根据Y轴等分原则计算每分的长度
int ylength = image.Height - beginy - 40;
int ypart = Convert.ToInt32(ylength / party);
//---------------------开始绘制坐标轴内部纵向线条-----------------------------
int x = beginy;
for(int i = 0; i <= partx; i++)
{
g.DrawLine(mypen, x, beginx, x, beginx + ylength - 10);
x = x + xpart;
}
g.DrawLine(mypen1, beginy, beginx, beginy, beginx + ylength - 10);
//绘制Y轴,注意:真实的Y轴长度是yLength,此时坐标的x参量相同
//--------------------绘制纵向线条结束---------------------------------------
//--------------------开始绘制横向线条---------------------------------------
int y = beginx - 5;
for (int i = 0; i < party; i++)
{
g.DrawLine(mypen, beginy, y, beginy + xlength, y);
y = y + ypart;
}
//绘制X轴。注意:此时坐标的Y参量相同
g.DrawLine(mypen1, beginy, beginx + ylength - 10,
beginy + xlength, beginx + ylength - 10);
//--------------------绘制横向线条结束--------------------------------------
//====================绘制X、Y轴的坐标内容=================================
//x轴信息
int x1 = beginy;
for (int i = 0; i < partx; i++)
{
//设置文字内容及输出位置
g.DrawString(xmess[i].ToString(), font,
Brushes.Blue, x1, beginx + ylength);
x1 = x1 + xpart;
}
//Y轴信息
int y1 = beginx + ypart;
for (int i = 0; i < party; i++)
{
//设置文字内容及输出位置
g.DrawString(ymess[i].ToString(), font, Brushes.Blue, 1, y1);
y1 = y1 + ypart;
}
//====根据传过来的数组中的值(数目与x轴个数相等,但是Y值被放置在数组中)
//绘制柱状图
x = 80;
Font font2 = new System.Drawing.Font("Arial", 10, FontStyle.Bold);
SolidBrush mybrush = new SolidBrush(Color.Red);
for (int i = 0; i < county.Length; i++ )
{
//第i季度:一下代码为最核心的绘制柱状图代码
//求小数时,分母必须为decimal类型,否则一律为0
int part1 = Convert.ToInt32((decimal)Convert.ToInt32(county[i] / ymax) * ylength);
g.FillRectangle(mybrush, beginy, ylength - part1, xpart / 2, part1);
g.DrawString(county[i].ToString(),
font2, Brushes.GreenYellow, beginy, ylength - part1);
//此处注意:最后补偿增加为x轴的宽度
beginy += xpart;
}
return image;
}
catch{
return image;
}
}
public Bitmap CreatePieImage(int width, int height, string topic, int[] county, string[] xmess)
{
Bitmap image = new Bitmap(width, height);
//创建Graphics类对象
Graphics g = Graphics.FromImage(image);
try
{
//清空图像背景颜色
g.Clear(Color.White);
//设置字体,fonttitle为主标题的字体
Font fontlegend = new Font("verdana", 9, FontStyle.Regular);
Font fonttitle = new Font("宋体", 13, FontStyle.Bold);
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height)
, Color.Blue, Color.BlueViolet, 1.2f, true);
//图片的底色
g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);
//图像的边框线
g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);
//计算饼形图的宽和高,设计时考虑为宽度大于高度,横行显示。目的是在饼形图右侧写文字
//饼型直径
int pieheight = height - 10;
//文字区域宽度
int fontwidth = width - height;
//文字区域左上角Y坐标
int fonty = 10;
//计算数组的和,得到饼形的总和
int count = 0;
for (int i = 0; i < county.Length; i++)
{
count += Convert.ToInt32(county[i]);
}
//绘制饼形图背景色
SolidBrush brush1 = new SolidBrush(Color.Beige);
//饼形图边框颜色
Pen pen1 = new Pen(Color.Blue, 1);
Rectangle picret = new Rectangle(5, 5, pieheight, pieheight);
g.FillRectangle(brush1, picret);
g.DrawRectangle(pen1, picret);
//加上各种随机色。这里有一个问题:如果颜色相同怎么办?
ArrayList colors = new ArrayList();
Random rnd = new Random();
for (int i = 0; i < county.Length; i++)
{
colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255),
rnd.Next(255), rnd.Next(255))));
}
//开始绘制饼形图
SolidBrush blackbrush = new SolidBrush(Color.Black);
SolidBrush bluebrush = new SolidBrush(Color.Blue);
//开始绘制扇形的角度
Single currentdegree = 0.0f;
int pieheight1 = pieheight + 10;
int fonty1 = fonty + 30;
for (int j = 0; j < county.Length; j++)
{
g.FillPie((SolidBrush)colors[j], picret,
currentdegree, Convert.ToSingle(county[j]) / count * 360);
currentdegree += Convert.ToSingle(county[j]) / count * 360;
//下面开始在图像上除标题外添加注释
g.DrawString(Convert.ToString(xmess[j]) + ":" + Convert.ToString(Convert.ToString(county[j])),
fontlegend, blackbrush, pieheight1, fonty1);
g.FillRectangle((SolidBrush)colors[j], pieheight1 + 130, fonty, 50, 10);
fonty1 = fonty1 + fontlegend.Height * 3;
}
//以下为生成主标题
topic = topic + "\n\n\n";
StringFormat stringFormat = new StringFormat();
g.DrawString(topic, fonttitle, blackbrush,
new Rectangle(pieheight + 10, 10, width, height), stringFormat);
return image;
}
catch
{
return image;
}
}
}