C#绘图

private void CreateImage()
{
//把连接字串指定为一个常量
SqlConnection Con = new SqlConnection("Server=(Local);
Database=committeeTraining;Uid=sa;Pwd=**");
Con.Open();
string cmdtxt = selectString; // "select * from ##Count"; //
//SqlCommand Com = new SqlCommand(cmdtxt, Con);
DataSet ds = new DataSet();
SqlDataAdapter Da = new SqlDataAdapter(cmdtxt, Con);
Da.Fill(ds);
Con.Close();
float Total = 0.0f, Tmp;

//转换成单精度。也可写成Convert.ToInt32
Total = Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]]);

// Total=Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]]);
//设置字体,fonttitle为主标题的字体
Font fontlegend = new Font("verdana", 9);
Font fonttitle = new Font("verdana", 10, FontStyle.Bold);

//背景宽
int width = 350;
int bufferspace = 15;
int legendheight = fontlegend.Height * 10 + bufferspace; //高度
int titleheight = fonttitle.Height + bufferspace;
int height = width + legendheight + titleheight + bufferspace;//白色背景高
int pieheight = width;
Rectangle pierect = new Rectangle(0, titleheight, width, pieheight);

//加上各种随机色
ArrayList colors = new ArrayList();
Random rnd = new Random();
for (int i = 0; i < 2; i++)
colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255))));

//创建一个bitmap实例
Bitmap objbitmap = new Bitmap(width, height);
Graphics objgraphics = Graphics.FromImage(objbitmap);

//画一个白色背景
objgraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);

//画一个亮黄色背景
objgraphics.FillRectangle(new SolidBrush(Color.Beige), pierect);

//以下为画饼图(有几行row画几个)
float currentdegree = 0.0f;

//画通过人数
objgraphics.FillPie((SolidBrush)colors[1], pierect, currentdegree,
Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]) / Total * 360);
currentdegree += Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]) / Total * 360;

//未通过人数饼状图
objgraphics.FillPie((SolidBrush)colors[0], pierect, currentdegree,
((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]]))-(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]))) / Total * 360);
currentdegree += ((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])) -
(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]))) / Total * 360;


//以下为生成主标题
SolidBrush blackbrush = new SolidBrush(Color.Black);
SolidBrush bluebrush = new SolidBrush(Color.Blue);
string title = " 机关单位成绩统计饼状图: "
+ "\n \n\n";
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;

objgraphics.DrawString(title, fonttitle, blackbrush,
new Rectangle(0, 0, width, titleheight), stringFormat);

//列出各字段与得数目
objgraphics.DrawRectangle(new Pen(Color.Red, 2), 0, height + 10 - legendheight, width, legendheight + 50);

objgraphics.DrawString("----------------统计信息------------------",
fontlegend, bluebrush, 20, height - legendheight + fontlegend.Height * 1 + 1);
objgraphics.DrawString("统计单位: " + this.ddlTaget.SelectedItem.Text,
fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 3 + 1);
objgraphics.DrawString("统计年份: " + this.ddlYear.SelectedItem.Text,
fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 4 + 1);
objgraphics.DrawString("统计期数: " + this.ddlSpan.SelectedItem.Text,
fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 5 + 1);

objgraphics.FillRectangle((SolidBrush)colors[1], 5,height - legendheight + fontlegend.Height * 8 + 1, 10, 10);
objgraphics.DrawString("报名总人数: " + Convert.ToString(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])),
fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 7 + 1);
objgraphics.FillRectangle((SolidBrush)colors[0], 5, height - legendheight + fontlegend.Height * 9 + 1, 10, 10);
objgraphics.DrawString("通过总人数: " + Convert.ToString(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]])),
fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 8 + 1);
objgraphics.DrawString("未通过人数: " + ((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])) -
(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]))), fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 9 + 1);

objgraphics.DrawString("通过率: " + Convert.ToString((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]) /
Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])) * 100)+ " %", fontlegend,
blackbrush, 20, height - legendheight + fontlegend.Height * 10 + 1);

Response.ContentType = "image/Jpeg";
objbitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
objgraphics.Dispose();
objbitmap.Dispose();

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值