• WinForm绘制柱形图


    一、绘制简单的柱形图

     private void button1_Click(object sender, EventArgs e)
            {
                //创建画布
                Bitmap bitM = new Bitmap(this.panel1.Width, this.panel1.Height);
                Graphics g = Graphics.FromImage(bitM);
                g.Clear(Color.White);//清除背景色 并填充为白色
                //x y轴坐标 以及w  h绘制矩形的宽高
                int x, y, w, h;
                Random ran = new Random();
                for (int i = 0; i < 4; i++)
                {
                    //绘制名字
                  g.DrawString("名字"+(i+1),new Font("宋体",8,FontStyle.Regular),new SolidBrush(Color.Black),76+40*i,this.panel1.Height-16);
                  x = 78 + 40 * i;
                int num=ran.Next(40, 400); 
                  y = this.panel1.Height - 20-(num*20/100);
                  w = 24;
                  h = num * 20 / 100;
                  g.FillRectangle(new SolidBrush(Color.FromArgb(60, 120, 80)), x, y, w, h);
                }
                this.panel1.BackgroundImage = bitM;
               
            }

    二、绘制带有辅助线的柱形图 

     private void button1_Click(object sender, EventArgs e)
            {
               
                int panelHeight=this.panel1.Height;
                int panelWidth=this.panel1.Width;
                 //创建新的画布
                Bitmap bitM = new Bitmap(panelWidth,panelHeight);
                Graphics g = Graphics.FromImage(bitM);
                g.Clear(Color.White);
                Random ran = new Random();
                Pen RedPan = new Pen(new SolidBrush(Color.Red),2.0f);
                for (int i = 0; i < 12; i++)
                {
                    //绘制水平线
                    g.DrawLine(RedPan, 50, panelHeight - 20 - i * 20, panelWidth - 40, panelHeight - 20 - i * 20);
                    //绘制文字
                    g.DrawString(i*100+"",new Font("宋体",10,FontStyle.Regular),new SolidBrush(Color.Black),20,panelHeight-27-i*20);
                    
                }
                int x, y, w, h,ranNum;
                w = 24;
                for (int i = 0; i < 7; i++)
                {
                    //绘制垂直直线
                    g.DrawLine(RedPan,50+40*i,panelHeight-20,50+40*i,20);
                    //绘制名字
                    g.DrawString("名字" + (i + 1), new Font("宋体", 8, FontStyle.Regular), new SolidBrush
                    (Color.Black), 76 + 40 * i, panelHeight - 16);
                    x = 78 + 40 * i;
                    ranNum=ran.Next(50,250);
                    y = panelHeight - 20 - ranNum;
                    h = ranNum;
                    g.FillRectangle(new SolidBrush(Color.FromArgb(60,130,80)),x,y,w,h);
                }
                g.DrawLine(RedPan, 50 + 40 * 7, panelHeight - 20, 50 + 40 * 7, 20);
                this.panel1.BackgroundImage = bitM;
    
            }

     三、绘制带有月份的柱形图 

     private void button1_Click(object sender, EventArgs e)
            {
                int panelHeight = this.panel1.Height;
                int panelWidth = this.panel1.Width;
                //创建新的画布
                Bitmap bitM = new Bitmap(panelWidth, panelHeight);
                Graphics g = Graphics.FromImage(bitM);
                g.Clear(Color.White);
                //设置字体
                Font font = new Font("Arial",9,FontStyle.Regular);
                Font fontSong = new Font("宋体", 20, FontStyle.Regular);
                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, bitM.Width, bitM.Height),Color.Blue,Color.Green,1.2f,true);
                g.FillRectangle(Brushes.WhiteSmoke, 0, 0, panelWidth, panelHeight);
                Brush brush1 = new SolidBrush(Color.Blue);
                g.DrawString("2018XX走势", fontSong, brush1, new PointF(180, 30));
                //画图片的边框线
                g.DrawRectangle(new Pen(Color.Blue),0,0,panelWidth-4,panelHeight-4);
                Pen mypen = new Pen(brush,1f);
                //绘制纵向线条
                int x = 50;
                for (int i = 0; i < 13; i++)
                {
                    g.DrawLine(mypen,x,75,x,307);
                    x += 40;
                }
               // 绘制横向线条
                int y = 74;
    
                for (int i = 0; i < 10; i++)
                {
                    g.DrawLine(mypen, 50, y, 530, y);
                    y += 26;
                }
                //绘制X轴月份
                string[] strX = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" };
                x = 45;
                for (int i = 0; i < strX.Length; i++)
                {
                    g.DrawString(strX[i].ToString(),font,Brushes.Red,x,panelHeight-30);
                    x += 40;
                }
                //绘制Y轴数量
                string[] StrY = { "0", "100", "200", "300", "400", "500", "600", "700", "800" };
                y = 78;
                for (int i = 0; i < StrY.Length; i++)
                {
                    g.DrawString(StrY[StrY.Length-i-1].ToString(), font, Brushes.Red, 25, y);
                    y += 26;
                }
                //填充数据
                Random ran = new Random();
                x = 55;
                y = 220;
                int  h=0;
                for (int i = 0; i < strX.Length; i++)
                {
                    int ranNum=ran.Next(0,800);
                    SolidBrush sb = new SolidBrush(Color.FromArgb(150,0,0));
                    h = ranNum /4;
                    g.FillRectangle(sb, x +i * 30, y-h+88, 30, h);
                    x +=10;
                }
                this.panel1.BackgroundImage = bitM;
    
               
            }
     
  • 相关阅读:
    操作winrar
    lucene.NET详细使用与优化详解
    js实现记住帐号或密码(js读写COOKIE)
    jQuery对select操作小结
    XMLHelper
    AJAX2用法
    as3Crypto and php, what a fun ride!
    linux文件描述符导致squid拒绝服务
    Centos 增加硬盘
    安装Squid log analyzer分析工具
  • 原文地址:https://www.cnblogs.com/zyadmin/p/8409813.html
Copyright © 2020-2023  润新知