C绘制统计图(柱状图,折线图,扇形图)_第1页
C绘制统计图(柱状图,折线图,扇形图)_第2页
C绘制统计图(柱状图,折线图,扇形图)_第3页
C绘制统计图(柱状图,折线图,扇形图)_第4页
C绘制统计图(柱状图,折线图,扇形图)_第5页
已阅读5页,还剩9页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、C# 绘制统计图(柱状图, 折线图, 扇形图)统计图形种类繁多, 有柱状图, 折线图, 扇形图等等, 而统计图形的绘制方法也有很多, 有Flash制作的统计图形, 有水晶报表生成统计图形, 有专门制图软件制作, 也有编程语言自己制作的;这里我们用就C# 制作三款最经典的统计图: 柱状图, 折线图和扇形图;既然是统计, 当然需要数据, 这里演示的数据存于Sql Server2000中, 三款统计图形都是动态生成. 其中柱状图我会附上制作步骤, 其他两款统计图直接附源码. 说明: 需求不一样, 统计图形绘制后的显示效果也不一样, 比如这里柱状图的主要需求是为了比较每一期报名人数与通过人数

2、的差, 因此会把两根柱子放在一起会使比较结果一目了然. 因此大家可以根据需要灵活绘制. 一. 柱状图的绘制.绘制步骤如下: 1. 定义绘图用到的类.定义绘图类int height = 500, width = 700;Bitmap image = new Bitmap(width, height);Graphics g = Graphics.FromImage(image);Pen mypen = new Pen(brush, 1); 2. 绘制图框.绘制图框g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);  

3、;3. 绘制横向坐标线绘制横向坐标线for (int i = 0; i < 14; i+) g.DrawLine(mypen, x, 80, x, 340);x = x + 40; 4. 绘制纵向坐标线绘制纵向坐标线for (int i = 0; i < 9; i+) g.DrawLine(mypen, 60, y, 620, y);y = y + 26; 5. 绘制横坐标值绘制横坐标值String n = "第一期", "第二期", "第三期", "第四期", "全年&q

4、uot; ;for (int i = 0; i < 7; i+) g.DrawString(ni.ToString(), font, Brushes.Blue, x, 348); x = x + 78; 6. 绘制纵坐标值绘制纵坐标String m = "250","225", "200", "175", "150", "125", "100“;for (int i = 0; i < 10; i+) g.DrawString(mi.ToStri

5、ng(), font, Brushes.Blue, 25, y);y = y + 26; 7. 定义数组存储数据库中统计的数据定义存储统计数据的数组int Count1 = new int7; /存储从数据库读取的报名人数int Count2 = new int7; /存储从数据库读取的通过人数  8. 从数据库中读取报名人数与通过人数读取数据SqlConnection Con = new SqlConnection("Server=(Local);Database=committeeTraining;");Con.Open();string cmdtx

6、t2 = "SELECT * FROM #Count where Company='" + *+ "'"SqlDataAdapter da = new SqlDataAdapter(cmdtxt2, Con);DataSet ds = new DataSet();da.Fill(ds);  9. 将读取的数据存储到数组中将数据存储到数组中Count10 = Convert.ToInt32(ds.Tables0.Rows0“count1”.ToString(); Count11 = Convert.ToInt32(ds.Table

7、s0.Rows0“count3”.ToString(); Count20 = Convert.ToInt32(ds.Tables0.Rows0“count2”.ToString(); Count21 = Convert.ToInt32(ds.Tables0.Rows0"count4".ToString(); 10.定义画笔和画刷准备绘图 准备绘制柱状图x = 80; Font font2 = new System.Drawing.Font("Arial", 10, FontStyle.Bold);SolidBrush mybrush = new

8、 SolidBrush(Color.Red);SolidBrush mybrush2 = new SolidBrush(Color.Green); 11. 根据数组中的值绘制柱状图 绘制柱状图(1)第一期报名人数g.FillRectangle(mybrush, x, 340 - Count10, 20, Count10);g.DrawString(Count10.ToString(), font2, Brushes.Red, x, 340 - Count10 - 15);(2) 第一期通过人数x = x + 20;g.FillRectangle(mybrush2, x, 3

9、40 - Count20, 20, Count20);g.DrawString(Count20.ToString(), font2, Brushes.Green, x, 340 - Count20 - 15); 12. 将图形输出到页面.将页面输出到页中System.IO.MemoryStream ms = new System.IO.MemoryStream();image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);Response.ClearContent();Response.ContentType = "im

10、age/Jpeg"Response.BinaryWrite(ms.ToArray();  最终柱状图的效果图:  柱状图的完整代码:绘制柱状统计图的完整代码private void CreateImage()int height = 500, width = 700;Bitmap image = new Bitmap(width, height);/创建Graphics类对象Graphics g = Graphics.FromImage(image);try/清空图片背景色g.Clear(Color.White);Font font = new Font(&quo

11、t;Arial", 10, FontStyle.Regular);Font font1 = new Font("宋体", 20, 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);/ Bru

12、sh brush1 = new SolidBrush(Color.Blue);g.DrawString(this.ddlTaget.SelectedItem.Text + " " + this.ddlYear.SelectedItem.Text + " 成绩统计柱状图", font1, brush, new PointF(70, 30);/画图片的边框线g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);Pen mypen = new Pen(brus

13、h, 1);/绘制线条/绘制横向线条int x = 100;for (int i = 0; i < 14; i+)g.DrawLine(mypen, x, 80, x, 340);x = x + 40;Pen mypen1 = new Pen(Color.Blue, 2);x = 60;g.DrawLine(mypen1, x, 80, x, 340);/绘制纵向线条int y = 106;for (int i = 0; i < 9; i+)g.DrawLine(mypen, 60, y, 620, y);y = y + 26;g.DrawLine(mypen1, 60, y, 6

14、20, y);/x轴String n = "第一期", "第二期", "第三期", "第四期", "上半年", "下半年", "全年统计" ;x = 78;for (int i = 0; i < 7; i+)g.DrawString(ni.ToString(), font, Brushes.Blue, x, 348); /设置文字内容及输出位置x = x + 78;/y轴String m = "250","225&quo

15、t;, "200", "175", "150", "125", "100", " 75"," 50", " 25", " 0"y = 72;for (int i = 0; i < 10; i+)g.DrawString(mi.ToString(), font, Brushes.Blue, 25, y); /设置文字内容及输出位置y = y + 26;int Count1 = new int7;int Count

16、2 = new int7;SqlConnection Con = new SqlConnection("Server=(Local);Database=committeeTraining;Uid=sa;Pwd=*");Con.Open();string cmdtxt2 = "SELECT * FROM #Count where Company='" + this.ddlTaget.SelectedItem.Text.Trim() + "'"SqlDataAdapter da = new SqlDataAdapter(c

17、mdtxt2, Con);DataSet ds = new DataSet();da.Fill(ds);Count10 = Convert.ToInt32(ds.Tables0.Rows0"count1".ToString();Count11 = Convert.ToInt32(ds.Tables0.Rows0"count3".ToString();Count12 = Convert.ToInt32(ds.Tables0.Rows0"count5".ToString();Count13 = Convert.ToInt32(ds.Tab

18、les0.Rows0"count7".ToString();Count14 = Count10 + Count11;Count15 = Count12 + Count13;Count16 = Convert.ToInt32(ds.Tables0.Rows0"count9".ToString();Count20 = Convert.ToInt32(ds.Tables0.Rows0"count2".ToString();Count21 = Convert.ToInt32(ds.Tables0.Rows0"count4"

19、.ToString();Count22 = Convert.ToInt32(ds.Tables0.Rows0"count6".ToString();Count23 = Convert.ToInt32(ds.Tables0.Rows0"count8".ToString();Count24 = Count20 + Count21;Count25 = Count22 + Count23;Count26 = Convert.ToInt32(ds.Tables0.Rows0"count10".ToString();/绘制柱状图.x = 80;F

20、ont font2 = new System.Drawing.Font("Arial", 10, FontStyle.Bold);SolidBrush mybrush = new SolidBrush(Color.Red);SolidBrush mybrush2 = new SolidBrush(Color.Green);/第一期g.FillRectangle(mybrush, x, 340 - Count10, 20, Count10);g.DrawString(Count10.ToString(), font2, Brushes.Red, x, 340 - Count1

21、0 - 15);x = x + 20;g.FillRectangle(mybrush2, x, 340 - Count20, 20, Count20);g.DrawString(Count20.ToString(), font2, Brushes.Green, x, 340 - Count20 - 15);/第二期x = x + 60;g.FillRectangle(mybrush, x, 340 - Count11, 20, Count11);g.DrawString(Count11.ToString(), font2, Brushes.Red, x, 340 - Count11 - 15)

22、;x = x + 20;g.FillRectangle(mybrush2, x, 340 - Count21, 20, Count21);g.DrawString(Count21.ToString(), font2, Brushes.Green, x, 340 - Count21 - 15);/第三期x = x + 60;g.FillRectangle(mybrush, x, 340 - Count12, 20, Count12);g.DrawString(Count12.ToString(), font2, Brushes.Red, x, 340 - Count12 - 15);x = x

23、+ 20;g.FillRectangle(mybrush2, x, 340 - Count22, 20, Count22);g.DrawString(Count22.ToString(), font2, Brushes.Green, x, 340 - Count22 - 15);/第四期x = x + 60;g.FillRectangle(mybrush, x, 340 - Count13, 20, Count13);g.DrawString(Count13.ToString(), font2, Brushes.Red, x, 340 - Count13 - 15);x = x + 20;g.

24、FillRectangle(mybrush2, x, 340 - Count23, 20, Count23);g.DrawString(Count23.ToString(), font2, Brushes.Green, x, 340 - Count23 - 15);/上半年x = x + 60;g.FillRectangle(mybrush, x, 340 - Count14, 20, Count14);g.DrawString(Count14.ToString(), font2, Brushes.Red, x, 340 - Count14 - 15);x = x + 20;g.FillRec

25、tangle(mybrush2, x, 340 - Count24, 20, Count24);g.DrawString(Count24.ToString(), font2, Brushes.Green, x, 340 - Count24 - 15);/下半年x = x + 60;g.FillRectangle(mybrush, x, 340 - Count15, 20, Count15);g.DrawString(Count15.ToString(), font2, Brushes.Red, x, 340 - Count15 - 15);x = x + 20;g.FillRectangle(

26、mybrush2, x, 340 - Count25, 20, Count25);g.DrawString(Count25.ToString(), font2, Brushes.Green, x, 340 - Count25 - 15);/全年x = x + 60;g.FillRectangle(mybrush, x, 340 - Count16, 20, Count16);g.DrawString(Count16.ToString(), font2, Brushes.Red, x, 340 - Count16 - 15);x = x + 20;g.FillRectangle(mybrush2

27、, x, 340 - Count26, 20, Count26);g.DrawString(Count26.ToString(), font2, Brushes.Green, x, 340 - Count26 - 15);/绘制标识Font font3 = new System.Drawing.Font("Arial", 10, FontStyle.Regular);g.DrawRectangle(new Pen(Brushes.Blue), 170, 400, 250, 50); /绘制范围框g.FillRectangle(Brushes.Red, 270, 410, 2

28、0, 10); /绘制小矩形g.DrawString("报名人数", font3, Brushes.Red, 292, 408);g.FillRectangle(Brushes.Green, 270, 430, 20, 10);g.DrawString("通过人数", font3, Brushes.Green, 292, 428);System.IO.MemoryStream ms = new System.IO.MemoryStream();image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

29、Response.ClearContent();Response.ContentType = "image/Jpeg"Response.BinaryWrite(ms.ToArray();finallyg.Dispose();image.Dispose();   二. 折线统计图的绘制效果:    折线图的完整代码:折线图的完整代码private void CreateImage()int height = 480, width = 700;Bitmap image = new Bitmap(width, heigh

30、t);Graphics g = Graphics.FromImage(image);try/清空图片背景色g.Clear(Color.White);Font font = new System.Drawing.Font("Arial", 9, FontStyle.Regular);Font font1 = new System.Drawing.Font("宋体", 20, FontStyle.Regular);Font font2 = new System.Drawing.Font("Arial", 8, FontStyle.Regu

31、lar);LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Blue, 1.2f, true);g.FillRectangle(Brushes.AliceBlue, 0, 0, width, height);Brush brush1 = new SolidBrush(Color.Blue);Brush brush2 = new SolidBrush(Color.SaddleBrown);g.DrawString

32、(this.ddlTaget.SelectedItem.Text + " " + this.ddlYear.SelectedItem.Text + " 成绩统计折线图", font1, brush1, new PointF(85, 30);/画图片的边框线g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);Pen mypen = new Pen(brush, 1);Pen mypen2 = new Pen(Color.Red, 2);/绘制线条/绘制纵

33、向线条int x = 60;for (int i = 0; i < 8; i+)g.DrawLine(mypen, x, 80, x, 340);x = x + 80;Pen mypen1 = new Pen(Color.Blue, 3);x = 60;g.DrawLine(mypen1, x, 82, x, 340);/绘制横向线条int y = 106;for (int i = 0; i < 10; i+)g.DrawLine(mypen, 60, y, 620, y);y = y + 26;/ y = 106;g.DrawLine(mypen1, 60, y - 26, 62

34、0, y - 26);/x轴String n = "第一期", "第二期", "第三期", "第四期", "上半年", "下半年", "全年统计" ;x = 45;for (int i = 0; i < 7; i+)g.DrawString(ni.ToString(), font, Brushes.Red, x, 348); /设置文字内容及输出位置x = x + 77;/y轴String m = "220人", " 2

35、00人", " 175人", "150人", " 125人", " 100人", " 75人", " 50人"," 25人"y = 100;for (int i = 0; i < 9; i+)g.DrawString(mi.ToString(), font, Brushes.Red, 10, y); /设置文字内容及输出位置y = y + 26;int Count1 = new int7;int Count2 = new int7;SqlC

36、onnection Con = new SqlConnection("Server=(Local);Database=committeeTraining;Uid=sa;Pwd=eesoft");Con.Open();string cmdtxt2 = "SELECT * FROM #Count where Company='" + this.ddlTaget.SelectedItem.Text.Trim() + "'"SqlDataAdapter da = new SqlDataAdapter(cmdtxt2, Con)

37、;DataSet ds = new DataSet();da.Fill(ds);/报名人数Count10 = Convert.ToInt32(ds.Tables0.Rows0"count1".ToString();Count11 = Convert.ToInt32(ds.Tables0.Rows0"count3".ToString();Count12 = Convert.ToInt32(ds.Tables0.Rows0"count5".ToString();Count13 = Convert.ToInt32(ds.Tables0.Ro

38、ws0"count7".ToString();Count16 = Convert.ToInt32(ds.Tables0.Rows0"count9".ToString(); /全年Count14 = Count10 + Count11;Count15 = Count12 + Count13;Count20 = Convert.ToInt32(ds.Tables0.Rows0"count2".ToString();Count21 = Convert.ToInt32(ds.Tables0.Rows0"count4".To

39、String();Count22 = Convert.ToInt32(ds.Tables0.Rows0"count6".ToString();Count23 = Convert.ToInt32(ds.Tables0.Rows0"count8".ToString();Count26 = Convert.ToInt32(ds.Tables0.Rows0"count10".ToString(); /全年Count24 = Count20 + Count21;Count25 = Count22 + Count23;/显示折线效果Font fo

40、nt3 = new System.Drawing.Font("Arial", 10, FontStyle.Bold);SolidBrush mybrush = new SolidBrush(Color.Red);Point points1 = new Point7;points10.X = 60; points10.Y = 340 - Count10; /从106纵坐标开始, 到(0, 0)坐标时points11.X = 140; points11.Y = 340 - Count11;points12.X = 220; points12.Y = 340 - Count12;

41、points13.X = 300; points13.Y = 340 - Count13;points14.X = 380; points14.Y = 340 - Count14;points15.X = 460; points15.Y = 340 - Count15;points16.X = 540; points16.Y = 340 - Count16;g.DrawLines(mypen2, points1); /绘制折线/绘制数字g.DrawString(Count10.ToString(), font3, Brushes.Red, 58, points10.Y - 20);g.Draw

42、String(Count11.ToString(), font3, Brushes.Red, 138, points11.Y - 20);g.DrawString(Count12.ToString(), font3, Brushes.Red, 218, points12.Y - 20);g.DrawString(Count13.ToString(), font3, Brushes.Red, 298, points13.Y - 20);g.DrawString(Count14.ToString(), font3, Brushes.Red, 378, points14.Y - 20);g.Draw

43、String(Count15.ToString(), font3, Brushes.Red, 458, points15.Y - 20);g.DrawString(Count16.ToString(), font3, Brushes.Red, 538, points16.Y - 20);Pen mypen3 = new Pen(Color.Green, 2);Point points2 = new Point7;points20.X = 60; points20.Y = 340 - Count20;points21.X = 140; points21.Y = 340 - Count21;poi

44、nts22.X = 220; points22.Y = 340 - Count22;points23.X = 300; points23.Y = 340 - Count23;points24.X = 380; points24.Y = 340 - Count24;points25.X = 460; points25.Y = 340 - Count25;points26.X = 540; points26.Y = 340 - Count26;g.DrawLines(mypen3, points2); /绘制折线/绘制通过人数g.DrawString(Count20.ToString(), fon

45、t3, Brushes.Green, 61, points20.Y - 15);g.DrawString(Count21.ToString(), font3, Brushes.Green, 131, points21.Y - 15);g.DrawString(Count22.ToString(), font3, Brushes.Green, 221, points22.Y - 15);g.DrawString(Count23.ToString(), font3, Brushes.Green, 301, points23.Y - 15);g.DrawString(Count24.ToString

46、(), font3, Brushes.Green, 381, points24.Y - 15);g.DrawString(Count25.ToString(), font3, Brushes.Green, 461, points25.Y - 15);g.DrawString(Count26.ToString(), font3, Brushes.Green, 541, points26.Y - 15);/绘制标识g.DrawRectangle(new Pen(Brushes.Red), 180, 390, 250, 50); /绘制范围框g.FillRectangle(Brushes.Red,

47、270, 402, 20, 10); /绘制小矩形g.DrawString("报名人数", font2, Brushes.Red, 292, 400);g.FillRectangle(Brushes.Green, 270, 422, 20, 10);g.DrawString("通过人数", font2, Brushes.Green, 292, 420);System.IO.MemoryStream ms = new System.IO.MemoryStream();image.Save(ms, System.Drawing.Imaging.ImageFo

48、rmat.Jpeg);Response.ClearContent();Response.ContentType = "image/Jpeg"Response.BinaryWrite(ms.ToArray();finallyg.Dispose();image.Dispose();  三. 扇形统计图的绘制效果图:  完整代码:扇形统计图的绘制private void CreateImage()/把连接字串指定为一个常量SqlConnection Con = new SqlConnection("Server=(Local);D

49、atabase=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;/转换成单

50、精度。也可写成Convert.ToInt32Total = Convert.ToSingle(ds.Tables0.Rows0this.count0);/ Total=Convert.ToSingle(ds.Tables0.Rows0this.count0);/设置字体,fonttitle为主标题的字体Font fontlegend = new Font("verdana", 9);Font fonttitle = new Font("verdana", 10, FontStyle.Bold);/背景宽int width = 350;int buffer

51、space = 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 colo

52、rs = 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.FillRect

53、angle(new SolidBrush(Color.White), 0, 0, width, height);/画一个亮黄色背景 objgraphics.FillRectangle(new SolidBrush(Color.Beige), pierect);/以下为画饼图(有几行row画几个)float currentdegree = 0.0f;/画通过人数objgraphics.FillPie(SolidBrush)colors1, pierect, currentdegree,Convert.ToSingle(ds.Tables0.Rows0this.count1) / Total *

54、360);currentdegree += Convert.ToSingle(ds.Tables0.Rows0this.count1) / Total * 360;/未通过人数饼状图objgraphics.FillPie(SolidBrush)colors0, pierect, currentdegree,(Convert.ToSingle(ds.Tables0.Rows0this.count0)-(Convert.ToSingle(ds.Tables0.Rows0this.count1) / Total * 360);currentdegree += (Convert.ToSingle(ds

55、.Tables0.Rows0this.count0) - (Convert.ToSingle(ds.Tables0.Rows0this.count1) / Total * 360;/以下为生成主标题SolidBrush blackbrush = new SolidBrush(Color.Black);SolidBrush bluebrush = new SolidBrush(Color.Blue);string title = " 机关单位成绩统计饼状图: "+ "n nn"StringFormat stringFormat = new StringFo

56、rmat();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("-统计信息-

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论