欢迎来到冰点文库! | 帮助中心 分享价值,成长自我!
冰点文库
全部分类
  • 临时分类>
  • IT计算机>
  • 经管营销>
  • 医药卫生>
  • 自然科学>
  • 农林牧渔>
  • 人文社科>
  • 工程科技>
  • PPT模板>
  • 求职职场>
  • 解决方案>
  • 总结汇报>
  • ImageVerifierCode 换一换
    首页 冰点文库 > 资源分类 > DOCX文档下载
    分享到微信 分享到微博 分享到QQ空间

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

    • 资源ID:7943018       资源大小:557.70KB        全文页数:17页
    • 资源格式: DOCX        下载积分:3金币
    快捷下载 游客一键下载
    账号登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录 QQ登录
    二维码
    微信扫一扫登录
    下载资源需要3金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP,免费下载
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    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


    注意事项

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

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

    copyright@ 2008-2023 冰点文库 网站版权所有

    经营许可证编号:鄂ICP备19020893号-2


    收起
    展开