C# 绘制统计图(柱状图, 折线图, 扇形图)
统 计图形种类繁多, 有柱状图, 折线图, 扇形图等等, 而统计图形的绘制方法也有很多, 有Flash制作的统计图形, 有水晶报表生成统计图形, 有 专门制图软件制作, 也有编程语言自己制作的;这里我们用就C# 制作三款最经典的统计图: 柱状图, 折线图和扇形图;既然是统计, 当然需要数 据, 这里演示的数据存于Sql Server2000中, 三款统计图形都是动态生成. 其中柱状图我会附上制作步骤, 其他两款统计图直接附源码 .
说明: 需求不一样, 统计图形绘制后的显示效果也不一样, 比如这里柱状图的主要需求是为了比较每一期报名人数与通过人数的差, 因此会把两根柱子放在一起会使比较结果一目了然. 因此大家可以根据需要灵活绘制.
一. 柱状图的绘制.
绘制步骤如下:
1. 定义绘图用到的类.
定义绘图类
1 int height = 500, width = 700; 2 Bitmap image = new Bitmap(width, height); 3 Graphics g = Graphics.FromImage(image); 4 Pen mypen = new Pen(brush, 1);
2. 绘制图框.
绘制图框.
1 g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);
3. 绘制横向坐标线
绘制横向坐标线
1 for (int i = 0; i < 14; i++) 2 { 3 g.DrawLine(mypen, x, 80, x, 340); 4 x = x + 40; 5 }
4. 绘制纵向坐标线
4. 绘制纵向坐标线
1 for (int i = 0; i < 9; i++) 2 { 3 g.DrawLine(mypen, 60, y, 620, y); 4 y = y + 26; 5 }
5. 绘制横坐标值
绘制横坐标值
1 String[] n = { "第一期", "第二期", "第三期", "第四期", "全年" }; 2 for (int i = 0; i < 7; i++) 3 { 4 g.DrawString(n[i].ToString(), font, Brushes.Blue, x, 348); 5 x = x + 78; 6 }
6. 绘制纵坐标值
绘制纵坐标值
1 String[] m = {"250","225", "200", "175", "150", "125", "100“}; 2 for (int i = 0; i < 10; i++) 3 { 4 g.DrawString(m[i].ToString(), font, Brushes.Blue, 25, y); 5 y = y + 26; 6 }
7. 定义数组存储数据库中统计的数据
定义数组存储数据库中统计的数据
1 int[] Count1 = new int[7]; //存储从数据库读取的报名人数 2 int[] Count2 = new int[7]; //存储从数据库读取的通过人数
8. 从数据库中读取报名人数与通过人数
读取数据
1 SqlConnection Con = new SqlConnection( 2 "Server=(Local);Database=committeeTraining;"); 3 Con.Open(); 4 string cmdtxt2 = "SELECT * FROM ##Count 5 where Company='" + ****+ "'"; 6 SqlDataAdapter da = new SqlDataAdapter(cmdtxt2, Con); 7 DataSet ds = new DataSet(); 8 da.Fill(ds);
9. 将读取的数据存储到数组中
将数据存储到数组中
1 Count1[0] = Convert.ToInt32(ds.Tables[0].Rows[0][“count1”].ToString()); 2 Count1[1] = Convert.ToInt32(ds.Tables[0].Rows[0][“count3”].ToString()); 3 Count2[0] = Convert.ToInt32(ds.Tables[0].Rows[0][“count2”].ToString()); 4 Count2[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count4"].ToString());
10.定义画笔和画刷准备绘图
准备绘制柱状图
1 x = 80; 2 Font font2 = new System.Drawing.Font( 3 "Arial", 10, FontStyle.Bold); 4 SolidBrush mybrush = new SolidBrush(Color.Red); 5 SolidBrush mybrush2 = new SolidBrush(Color.Green);
11. 根据数组中的值绘制柱状图
绘制柱状图
1 (1)第一期报名人数 2 g.FillRectangle(mybrush, x, 340 - Count1[0], 20, Count1[0]); 3 g.DrawString(Count1[0].ToString(), font2, 4 Brushes.Red, x, 340 - Count1[0] - 15); 5 6 (2) 第一期通过人数 7 x = x + 20; 8 g.FillRectangle(mybrush2, x, 340 - Count2[0], 20, Count2[0]); 9 g.DrawString(Count2[0].ToString(), font2, 10 Brushes.Green, x, 340 - Count2[0] - 15);
12. 将图形输出到页面.
将页面输出到页中
1 System.IO.MemoryStream ms = new 2 System.IO.MemoryStream(); 3 image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 4 Response.ClearContent(); 5 Response.ContentType = "image/Jpeg"; 6 Response.BinaryWrite(ms.ToArray());
最终柱状图的效果图:
柱状图的完整代码:
绘制柱状统计图的完整代码
1 private void CreateImage() 2 { 3 int height = 500, width = 700; 4 Bitmap image = new Bitmap(width, height); 5 //创建Graphics类对象 6 Graphics g = Graphics.FromImage(image); 7 8 try 9 { 10 //清空图片背景色 11 g.Clear(Color.White); 12 13 Font font = new Font("Arial", 10, FontStyle.Regular); 14 Font font1 = new Font("宋体", 20, FontStyle.Bold); 15 16 LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), 17 Color.Blue, Color.BlueViolet, 1.2f, true); 18 g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height); 19 // Brush brush1 = new SolidBrush(Color.Blue); 20 21 g.DrawString(this.ddlTaget.SelectedItem.Text + " " + this.ddlYear.SelectedItem.Text + 22 " 成绩统计柱状图", font1, brush, new PointF(70, 30)); 23 //画图片的边框线 24 g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1); 25 26 27 Pen mypen = new Pen(brush, 1); 28 //绘制线条 29 //绘制横向线条 30 int x = 100; 31 for (int i = 0; i < 14; i++) 32 { 33 g.DrawLine(mypen, x, 80, x, 340); 34 x = x + 40; 35 } 36 Pen mypen1 = new Pen(Color.Blue, 2); 37 x = 60; 38 g.DrawLine(mypen1, x, 80, x, 340); 39 40 //绘制纵向线条 41 int y = 106; 42 for (int i = 0; i < 9; i++) 43 { 44 g.DrawLine(mypen, 60, y, 620, y); 45 y = y + 26; 46 } 47 g.DrawLine(mypen1, 60, y, 620, y); 48 49 //x轴 50 String[] n = { "第一期", "第二期", "第三期", "第四期", "上半年", "下半年", "全年统计" }; 51 x = 78; 52 for (int i = 0; i < 7; i++) 53 { 54 g.DrawString(n[i].ToString(), font, Brushes.Blue, x, 348); //设置文字内容及输出位置 55 x = x + 78; 56 } 57 58 //y轴 59 String[] m = {"250","225", "200", "175", "150", "125", "100", " 75", 60 " 50", " 25", " 0"}; 61 y = 72; 62 for (int i = 0; i < 10; i++) 63 { 64 g.DrawString(m[i].ToString(), font, Brushes.Blue, 25, y); //设置文字内容及输出位置 65 y = y + 26; 66 } 67 68 int[] Count1 = new int[7]; 69 int[] Count2 = new int[7]; 70 71 SqlConnection Con = new SqlConnection("Server=(Local);Database=committeeTraining;Uid=sa;Pwd=**"); 72 Con.Open(); 73 string cmdtxt2 = "SELECT * FROM ##Count where Company='" + this.ddlTaget.SelectedItem.Text.Trim() + "'"; 74 SqlDataAdapter da = new SqlDataAdapter(cmdtxt2, Con); 75 DataSet ds = new DataSet(); 76 da.Fill(ds); 77 78 Count1[0] = Convert.ToInt32(ds.Tables[0].Rows[0]["count1"].ToString()); 79 Count1[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count3"].ToString()); 80 Count1[2] = Convert.ToInt32(ds.Tables[0].Rows[0]["count5"].ToString()); 81 Count1[3] = Convert.ToInt32(ds.Tables[0].Rows[0]["count7"].ToString()); 82 83 Count1[4] = Count1[0] + Count1[1]; 84 Count1[5] = Count1[2] + Count1[3]; 85 86 Count1[6] = Convert.ToInt32(ds.Tables[0].Rows[0]["count9"].ToString()); 87 88 89 Count2[0] = Convert.ToInt32(ds.Tables[0].Rows[0]["count2"].ToString()); 90 Count2[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count4"].ToString()); 91 Count2[2] = Convert.ToInt32(ds.Tables[0].Rows[0]["count6"].ToString()); 92 Count2[3] = Convert.ToInt32(ds.Tables[0].Rows[0]["count8"].ToString()); 93 94 Count2[4] = Count2[0] + Count2[1]; 95 Count2[5] = Count2[2] + Count2[3]; 96 97 Count2[6] = Convert.ToInt32(ds.Tables[0].Rows[0]["count10"].ToString()); 98 99 //绘制柱状图. 100 x = 80; 101 Font font2 = new System.Drawing.Font("Arial", 10, FontStyle.Bold); 102 SolidBrush mybrush = new SolidBrush(Color.Red); 103 SolidBrush mybrush2 = new SolidBrush(Color.Green); 104 105 //第一期 106 g.FillRectangle(mybrush, x, 340 - Count1[0], 20, Count1[0]); 107 g.DrawString(Count1[0].ToString(), font2, Brushes.Red, x, 340 - Count1[0] - 15); 108 109 x = x + 20; 110 g.FillRectangle(mybrush2, x, 340 - Count2[0], 20, Count2[0]); 111 g.DrawString(Count2[0].ToString(), font2, Brushes.Green, x, 340 - Count2[0] - 15); 112 113 114 //第二期 115 x = x + 60; 116 g.FillRectangle(mybrush, x, 340 - Count1[1], 20, Count1[1]); 117 g.DrawString(Count1[1].ToString(), font2, Brushes.Red, x, 340 - Count1[1] - 15); 118 119 120 x = x + 20; 121 g.FillRectangle(mybrush2, x, 340 - Count2[1], 20, Count2[1]); 122 g.DrawString(Count2[1].ToString(), font2, Brushes.Green, x, 340 - Count2[1] - 15); 123 124 125 //第三期 126 x = x + 60; 127 g.FillRectangle(mybrush, x, 340 - Count1[2], 20, Count1[2]); 128 g.DrawString(Count1[2].ToString(), font2, Brushes.Red, x, 340 - Count1[2] - 15); 129 130 x = x + 20; 131 g.FillRectangle(mybrush2, x, 340 - Count2[2], 20, Count2[2]); 132 g.DrawString(Count2[2].ToString(), font2, Brushes.Green, x, 340 - Count2[2] - 15); 133 134 //第四期 135 x = x + 60; 136 g.FillRectangle(mybrush, x, 340 - Count1[3], 20, Count1[3]); 137 g.DrawString(Count1[3].ToString(), font2, Brushes.Red, x, 340 - Count1[3] - 15); 138 139 x = x + 20; 140 g.FillRectangle(mybrush2, x, 340 - Count2[3], 20, Count2[3]); 141 g.DrawString(Count2[3].ToString(), font2, Brushes.Green, x, 340 - Count2[3] - 15); 142 143 //上半年 144 x = x + 60; 145 g.FillRectangle(mybrush, x, 340 - Count1[4], 20, Count1[4]); 146 g.DrawString(Count1[4].ToString(), font2, Brushes.Red, x, 340 - Count1[4] - 15); 147 148 x = x + 20; 149 g.FillRectangle(mybrush2, x, 340 - Count2[4], 20, Count2[4]); 150 g.DrawString(Count2[4].ToString(), font2, Brushes.Green, x, 340 - Count2[4] - 15); 151 152 //下半年 153 x = x + 60; 154 g.FillRectangle(mybrush, x, 340 - Count1[5], 20, Count1[5]); 155 g.DrawString(Count1[5].ToString(), font2, Brushes.Red, x, 340 - Count1[5] - 15); 156 157 x = x + 20; 158 g.FillRectangle(mybrush2, x, 340 - Count2[5], 20, Count2[5]); 159 g.DrawString(Count2[5].ToString(), font2, Brushes.Green, x, 340 - Count2[5] - 15); 160 161 //全年 162 x = x + 60; 163 g.FillRectangle(mybrush, x, 340 - Count1[6], 20, Count1[6]); 164 g.DrawString(Count1[6].ToString(), font2, Brushes.Red, x, 340 - Count1[6] - 15); 165 166 167 x = x + 20; 168 g.FillRectangle(mybrush2, x, 340 - Count2[6], 20, Count2[6]); 169 g.DrawString(Count2[6].ToString(), font2, Brushes.Green, x, 340 - Count2[6] - 15); 170 171 172 //绘制标识 173 Font font3 = new System.Drawing.Font("Arial", 10, FontStyle.Regular); 174 g.DrawRectangle(new Pen(Brushes.Blue), 170, 400, 250, 50); //绘制范围框 175 g.FillRectangle(Brushes.Red, 270, 410, 20, 10); //绘制小矩形 176 g.DrawString("报名人数", font3, Brushes.Red, 292, 408); 177 178 g.FillRectangle(Brushes.Green, 270, 430, 20, 10); 179 g.DrawString("通过人数", font3, Brushes.Green, 292, 428); 180 181 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 182 image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 183 Response.ClearContent(); 184 Response.ContentType = "image/Jpeg"; 185 Response.BinaryWrite(ms.ToArray()); 186 } 187 finally 188 { 189 g.Dispose(); 190 image.Dispose(); 191 } 192 }
二. 折线统计图的绘制
效果:
折线图的完整代码:
折线图的完整代码
1 private void CreateImage() 2 { 3 int height = 480, width = 700; 4 Bitmap image = new Bitmap(width, height); 5 Graphics g = Graphics.FromImage(image); 6 7 try 8 { 9 //清空图片背景色 10 g.Clear(Color.White); 11 12 Font font = new System.Drawing.Font("Arial", 9, FontStyle.Regular); 13 Font font1 = new System.Drawing.Font("宋体", 20, FontStyle.Regular); 14 Font font2 = new System.Drawing.Font("Arial", 8, FontStyle.Regular); 15 LinearGradientBrush brush = new LinearGradientBrush( 16 new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Blue, 1.2f, true); 17 g.FillRectangle(Brushes.AliceBlue, 0, 0, width, height); 18 Brush brush1 = new SolidBrush(Color.Blue); 19 Brush brush2 = new SolidBrush(Color.SaddleBrown); 20 21 g.DrawString(this.ddlTaget.SelectedItem.Text + " " + this.ddlYear.SelectedItem.Text + 22 " 成绩统计折线图", font1, brush1, new PointF(85, 30)); 23 //画图片的边框线 24 g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1); 25 26 Pen mypen = new Pen(brush, 1); 27 Pen mypen2 = new Pen(Color.Red, 2); 28 //绘制线条 29 //绘制纵向线条 30 int x = 60; 31 for (int i = 0; i < 8; i++) 32 { 33 g.DrawLine(mypen, x, 80, x, 340); 34 x = x + 80; 35 } 36 Pen mypen1 = new Pen(Color.Blue, 3); 37 x = 60; 38 g.DrawLine(mypen1, x, 82, x, 340); 39 40 //绘制横向线条 41 int y = 106; 42 for (int i = 0; i < 10; i++) 43 { 44 g.DrawLine(mypen, 60, y, 620, y); 45 y = y + 26; 46 } 47 // y = 106; 48 g.DrawLine(mypen1, 60, y - 26, 620, y - 26); 49 50 //x轴 51 String[] n = { "第一期", "第二期", "第三期", "第四期", "上半年", "下半年", "全年统计" }; 52 x = 45; 53 for (int i = 0; i < 7; i++) 54 { 55 g.DrawString(n[i].ToString(), font, Brushes.Red, x, 348); //设置文字内容及输出位置 56 x = x + 77; 57 } 58 59 //y轴 60 String[] m = { "220人", " 200人", " 175人", "150人", " 125人", " 100人", " 75人", " 50人", 61 " 25人"}; 62 y = 100; 63 for (int i = 0; i < 9; i++) 64 { 65 g.DrawString(m[i].ToString(), font, Brushes.Red, 10, y); //设置文字内容及输出位置 66 y = y + 26; 67 } 68 69 int[] Count1 = new int[7]; 70 int[] Count2 = new int[7]; 71 72 SqlConnection Con = new SqlConnection("Server=(Local);Database=committeeTraining;Uid=sa;Pwd=eesoft"); 73 Con.Open(); 74 string cmdtxt2 = "SELECT * FROM ##Count where Company='" + this.ddlTaget.SelectedItem.Text.Trim() + "'"; 75 SqlDataAdapter da = new SqlDataAdapter(cmdtxt2, Con); 76 DataSet ds = new DataSet(); 77 da.Fill(ds); 78 79 //报名人数 80 Count1[0] = Convert.ToInt32(ds.Tables[0].Rows[0]["count1"].ToString()); 81 Count1[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count3"].ToString()); 82 Count1[2] = Convert.ToInt32(ds.Tables[0].Rows[0]["count5"].ToString()); 83 Count1[3] = Convert.ToInt32(ds.Tables[0].Rows[0]["count7"].ToString()); 84 85 Count1[6] = Convert.ToInt32(ds.Tables[0].Rows[0]["count9"].ToString()); //全年 86 87 Count1[4] = Count1[0] + Count1[1]; 88 Count1[5] = Count1[2] + Count1[3]; 89 90 91 Count2[0] = Convert.ToInt32(ds.Tables[0].Rows[0]["count2"].ToString()); 92 Count2[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count4"].ToString()); 93 Count2[2] = Convert.ToInt32(ds.Tables[0].Rows[0]["count6"].ToString()); 94 Count2[3] = Convert.ToInt32(ds.Tables[0].Rows[0]["count8"].ToString()); 95 96 Count2[6] = Convert.ToInt32(ds.Tables[0].Rows[0]["count10"].ToString()); //全年 97 98 Count2[4] = Count2[0] + Count2[1]; 99 Count2[5] = Count2[2] + Count2[3]; 100 101 102 //显示折线效果 103 Font font3 = new System.Drawing.Font("Arial", 10, FontStyle.Bold); 104 SolidBrush mybrush = new SolidBrush(Color.Red); 105 Point[] points1 = new Point[7]; 106 points1[0].X = 60; points1[0].Y = 340 - Count1[0]; //从106纵坐标开始, 到(0, 0)坐标时 107 points1[1].X = 140; points1[1].Y = 340 - Count1[1]; 108 points1[2].X = 220; points1[2].Y = 340 - Count1[2]; 109 points1[3].X = 300; points1[3].Y = 340 - Count1[3]; 110 111 points1[4].X = 380; points1[4].Y = 340 - Count1[4]; 112 points1[5].X = 460; points1[5].Y = 340 - Count1[5]; 113 114 points1[6].X = 540; points1[6].Y = 340 - Count1[6]; 115 g.DrawLines(mypen2, points1); //绘制折线 116 117 //绘制数字 118 g.DrawString(Count1[0].ToString(), font3, Brushes.Red, 58, points1[0].Y - 20); 119 g.DrawString(Count1[1].ToString(), font3, Brushes.Red, 138, points1[1].Y - 20); 120 g.DrawString(Count1[2].ToString(), font3, Brushes.Red, 218, points1[2].Y - 20); 121 g.DrawString(Count1[3].ToString(), font3, Brushes.Red, 298, points1[3].Y - 20); 122 123 g.DrawString(Count1[4].ToString(), font3, Brushes.Red, 378, points1[4].Y - 20); 124 g.DrawString(Count1[5].ToString(), font3, Brushes.Red, 458, points1[5].Y - 20); 125 126 g.DrawString(Count1[6].ToString(), font3, Brushes.Red, 538, points1[6].Y - 20); 127 128 Pen mypen3 = new Pen(Color.Green, 2); 129 Point[] points2 = new Point[7]; 130 points2[0].X = 60; points2[0].Y = 340 - Count2[0]; 131 points2[1].X = 140; points2[1].Y = 340 - Count2[1]; 132 points2[2].X = 220; points2[2].Y = 340 - Count2[2]; 133 points2[3].X = 300; points2[3].Y = 340 - Count2[3]; 134 135 points2[4].X = 380; points2[4].Y = 340 - Count2[4]; 136 points2[5].X = 460; points2[5].Y = 340 - Count2[5]; 137 138 points2[6].X = 540; points2[6].Y = 340 - Count2[6]; 139 g.DrawLines(mypen3, points2); //绘制折线 140 141 //绘制通过人数 142 g.DrawString(Count2[0].ToString(), font3, Brushes.Green, 61, points2[0].Y - 15); 143 g.DrawString(Count2[1].ToString(), font3, Brushes.Green, 131, points2[1].Y - 15); 144 g.DrawString(Count2[2].ToString(), font3, Brushes.Green, 221, points2[2].Y - 15); 145 g.DrawString(Count2[3].ToString(), font3, Brushes.Green, 301, points2[3].Y - 15); 146 147 g.DrawString(Count2[4].ToString(), font3, Brushes.Green, 381, points2[4].Y - 15); 148 g.DrawString(Count2[5].ToString(), font3, Brushes.Green, 461, points2[5].Y - 15); 149 150 g.DrawString(Count2[6].ToString(), font3, Brushes.Green, 541, points2[6].Y - 15); 151 152 //绘制标识 153 g.DrawRectangle(new Pen(Brushes.Red), 180, 390, 250, 50); //绘制范围框 154 g.FillRectangle(Brushes.Red, 270, 402, 20, 10); //绘制小矩形 155 g.DrawString("报名人数", font2, Brushes.Red, 292, 400); 156 157 g.FillRectangle(Brushes.Green, 270, 422, 20, 10); 158 g.DrawString("通过人数", font2, Brushes.Green, 292, 420); 159 160 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 161 image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 162 Response.ClearContent(); 163 Response.ContentType = "image/Jpeg"; 164 Response.BinaryWrite(ms.ToArray()); 165 } 166 finally 167 { 168 g.Dispose(); 169 image.Dispose(); 170 } 171 }
三. 扇形统计图的绘制
效果图:
完整代码:
扇形统计图的绘制
1 private void CreateImage() 2 { 3 //把连接字串指定为一个常量 4 SqlConnection Con = new SqlConnection("Server=(Local); 5 Database=committeeTraining;Uid=sa;Pwd=**"); 6 Con.Open(); 7 string cmdtxt = selectString; // "select * from ##Count"; // 8 //SqlCommand Com = new SqlCommand(cmdtxt, Con); 9 DataSet ds = new DataSet(); 10 SqlDataAdapter Da = new SqlDataAdapter(cmdtxt, Con); 11 Da.Fill(ds); 12 Con.Close(); 13 float Total = 0.0f, Tmp; 14 15 //转换成单精度。也可写成Convert.ToInt32 16 Total = Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]]); 17 18 // Total=Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]]); 19 //设置字体,fonttitle为主标题的字体 20 Font fontlegend = new Font("verdana", 9); 21 Font fonttitle = new Font("verdana", 10, FontStyle.Bold); 22 23 //背景宽 24 int width = 350; 25 int bufferspace = 15; 26 int legendheight = fontlegend.Height * 10 + bufferspace; //高度 27 int titleheight = fonttitle.Height + bufferspace; 28 int height = width + legendheight + titleheight + bufferspace;//白色背景高 29 int pieheight = width; 30 Rectangle pierect = new Rectangle(0, titleheight, width, pieheight); 31 32 //加上各种随机色 33 ArrayList colors = new ArrayList(); 34 Random rnd = new Random(); 35 for (int i = 0; i < 2; i++) 36 colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255)))); 37 38 //创建一个bitmap实例 39 Bitmap objbitmap = new Bitmap(width, height); 40 Graphics objgraphics = Graphics.FromImage(objbitmap); 41 42 //画一个白色背景 43 objgraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height); 44 45 //画一个亮黄色背景 46 objgraphics.FillRectangle(new SolidBrush(Color.Beige), pierect); 47 48 //以下为画饼图(有几行row画几个) 49 float currentdegree = 0.0f; 50 51 //画通过人数 52 objgraphics.FillPie((SolidBrush)colors[1], pierect, currentdegree, 53 Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]) / Total * 360); 54 currentdegree += Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]) / Total * 360; 55 56 //未通过人数饼状图 57 objgraphics.FillPie((SolidBrush)colors[0], pierect, currentdegree, 58 ((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]]))-(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]))) / Total * 360); 59 currentdegree += ((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])) - 60 (Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]))) / Total * 360; 61 62 63 //以下为生成主标题 64 SolidBrush blackbrush = new SolidBrush(Color.Black); 65 SolidBrush bluebrush = new SolidBrush(Color.Blue); 66 string title = " 机关单位成绩统计饼状图: " 67 + "/n /n/n"; 68 StringFormat stringFormat = new StringFormat(); 69 stringFormat.Alignment = StringAlignment.Center; 70 stringFormat.LineAlignment = StringAlignment.Center; 71 72 objgraphics.DrawString(title, fonttitle, blackbrush, 73 new Rectangle(0, 0, width, titleheight), stringFormat); 74 75 //列出各字段与得数目 76 objgraphics.DrawRectangle(new Pen(Color.Red, 2), 0, height + 10 - legendheight, width, legendheight + 50); 77 78 objgraphics.DrawString("----------------统计信息------------------", 79 fontlegend, bluebrush, 20, height - legendheight + fontlegend.Height * 1 + 1); 80 objgraphics.DrawString("统计单位: " + this.ddlTaget.SelectedItem.Text, 81 fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 3 + 1); 82 objgraphics.DrawString("统计年份: " + this.ddlYear.SelectedItem.Text, 83 fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 4 + 1); 84 objgraphics.DrawString("统计期数: " + this.ddlSpan.SelectedItem.Text, 85 fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 5 + 1); 86 87 objgraphics.FillRectangle((SolidBrush)colors[1], 5,height - legendheight + fontlegend.Height * 8 + 1, 10, 10); 88 objgraphics.DrawString("报名总人数: " + Convert.ToString(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])), 89 fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 7 + 1); 90 objgraphics.FillRectangle((SolidBrush)colors[0], 5, height - legendheight + fontlegend.Height * 9 + 1, 10, 10); 91 objgraphics.DrawString("通过总人数: " + Convert.ToString(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]])), 92 fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 8 + 1); 93 objgraphics.DrawString("未通过人数: " + ((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])) - 94 (Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]))), fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 9 + 1); 95 96 objgraphics.DrawString("通过率: " + Convert.ToString((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]) / 97 Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])) * 100)+ " %", fontlegend, 98 blackbrush, 20, height - legendheight + fontlegend.Height * 10 + 1); 99 100 Response.ContentType = "image/Jpeg"; 101 objbitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); 102 objgraphics.Dispose(); 103 objbitmap.Dispose(); 104 105 }
文章来源:http://blog.csdn.net/gisfarmer/article/details/3736452