. 扇形统计图的绘制 <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

效果图 :

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />

完整代码 :

扇形统计图的绘制
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 = <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />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 = "p_w_picpath/Jpeg";
objbitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
objgraphics.Dispose();
objbitmap.Dispose();

}