ImageVerifierCode 换一换
格式:DOCX , 页数:17 ,大小:557.70KB ,
资源ID:11647677      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/11647677.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C# 绘制统计图柱状图 折线图 扇形图.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C# 绘制统计图柱状图 折线图 扇形图.docx

1、C# 绘制统计图柱状图 折线图 扇形图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. 绘制横向坐标线for

3、(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= 第一期,第二期,第三期,第四期,全年 ;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

4、,150,125,100“;for (int i= 0; i 10; i+) g.DrawString(mi.ToString(), 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

5、();string cmdtxt2= SELECT * FROM #Countwhere 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.Tables0.Rows0“count3”.ToString(); Count20= C

6、onvert.ToInt32(ds.Tables0.Rows0“count2”.ToString(); Count21= Convert.ToInt32(ds.Tables0.Rows0count4.ToString(); 10.定义画笔和画刷准备绘图x= 80; Font font2= new System.Drawing.Font(Arial,10, FontStyle.Bold);SolidBrush mybrush= new SolidBrush(Color.Red);SolidBrush mybrush2= new SolidBrush(Color.Green); 11. 根据数组中

7、的值绘制柱状图 (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,340 - Count20,20, Count20);g.DrawString(Count20.ToString(), font2, Brushes.Green, x,340 - Count20- 15); 12

8、. 将图形输出到页面.System.IO.MemoryStream ms= new System.IO.MemoryStream();image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);Response.ClearContent();Response.ContentType= image/Jpeg;Response.BinaryWrite(ms.ToArray(); 最终柱状图的效果图: 柱状图的完整代码:private void CreateImage()int height= 500, width= 700;Bitmap imag

9、e= 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(宋体,20, FontStyle.Bold);LinearGradientBrush brush= new LinearGradientBrush(new Rectangle(0,0, image.Width, image.Heigh

10、t), Color.Blue, Color.BlueViolet,1.2f,true);g.FillRectangle(Brushes.WhiteSmoke,0,0, width, height);/ Brush 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

11、.Blue),0,0, image.Width- 1, image.Height- 1);Pen mypen= new Pen(brush,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,

12、 y);y= y+ 26;g.DrawLine(mypen1,60, y,620, 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,200,175,150,125,100, 75, 50, 25, 0;y= 72;for (int i= 0; i 10; i+)g.DrawString(mi.ToStr

13、ing(), font, Brushes.Blue,25, y);/设置文字内容及输出位置y= y+ 26;int Count1= new int7;int Count2= 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()+ ;SqlDa

14、taAdapter da= new SqlDataAdapter(cmdtxt2, Con);DataSet ds= new DataSet();da.Fill(ds);Count10= Convert.ToInt32(ds.Tables0.Rows0count1.ToString();Count11= Convert.ToInt32(ds.Tables0.Rows0count3.ToString();Count12= Convert.ToInt32(ds.Tables0.Rows0count5.ToString();Count13= Convert.ToInt32(ds.Tables0.Ro

15、ws0count7.ToString();Count14= Count10+ Count11;Count15= Count12+ Count13;Count16= Convert.ToInt32(ds.Tables0.Rows0count9.ToString();Count20= Convert.ToInt32(ds.Tables0.Rows0count2.ToString();Count21= Convert.ToInt32(ds.Tables0.Rows0count4.ToString();Count22= Convert.ToInt32(ds.Tables0.Rows0count6.To

16、String();Count23= Convert.ToInt32(ds.Tables0.Rows0count8.ToString();Count24= Count20+ Count21;Count25= Count22+ Count23;Count26= Convert.ToInt32(ds.Tables0.Rows0count10.ToString();/绘制柱状图.x= 80;Font font2= new System.Drawing.Font(Arial,10, FontStyle.Bold);SolidBrush mybrush= new SolidBrush(Color.Red)

17、;SolidBrush mybrush2= new SolidBrush(Color.Green);/第一期g.FillRectangle(mybrush, x,340 - Count10,20, Count10);g.DrawString(Count10.ToString(), font2, Brushes.Red, x,340 - Count10- 15);x= x+ 20;g.FillRectangle(mybrush2, x,340 - Count20,20, Count20);g.DrawString(Count20.ToString(), font2, Brushes.Green,

18、 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);x= x+ 20;g.FillRectangle(mybrush2, x,340 - Count21,20, Count21);g.DrawString(Count21.ToString(), font2, Brushes.Green, x,340 - Count21- 1

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

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

21、h, x,340 - Count14,20, Count14);g.DrawString(Count14.ToString(), font2, Brushes.Red, x,340 - Count14- 15);x= x+ 20;g.FillRectangle(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,

22、20, Count15);g.DrawString(Count15.ToString(), font2, Brushes.Red, x,340 - Count15- 15);x= x+ 20;g.FillRectangle(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.DrawS

23、tring(Count16.ToString(), font2, Brushes.Red, x,340 - Count16- 15);x= x+ 20;g.FillRectangle(mybrush2, 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

24、(Brushes.Blue),170,400,250,50);/绘制范围框g.FillRectangle(Brushes.Red,270,410,20,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, Sy

25、stem.Drawing.Imaging.ImageFormat.Jpeg);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, height);Graphics g= Grap

26、hics.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.Regular);LinearGradientBrush brush= new LinearGradientBrush(new Re

27、ctangle(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(this.ddlTaget.SelectedItem.Text+ + this.ddlYear.SelectedItem.Text+ 成绩

28、统计折线图, 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);/绘制线条/绘制纵向线条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,620, y- 26);/x轴String n= 第一期,第二期,第三期,第四期,上半年,下半年,全年统计 ;x= 45;for (int

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1