C# winform绘制条形统计图的例子

条形统计图,的高度,颜色的填充,实质上是把各个数据按照一定的数据处理方式,做成合适的矩形宽与高,在用颜色填充即可。

本次从C#入门到精通的摘取一个例子。代码略作适当的调整。

 

        public void createImage() {
            int Tp1 = 13;
            int Tp2 = 1;
            int Tp3 = 2;
            int Tp4 = 1;
 
            double tp1 = 13.0/ 27*100;
            double tp2 = 1.0 / 27*100;
            double tp3 = 2.0 / 27*100;
            double tp4 = 1.0 / 27*100;
 
            int width = 300, height = 300;//声明宽与高
            Bitmap bitmap = new Bitmap(width, height);//创建一个绘图对象
 
            Graphics g = Graphics.FromImage(bitmap);
 
            try
            {
                g.Clear(Color.White);
                //创建6个brush,用于填充颜色
                Brush brush1 = new SolidBrush(Color.White);
                Brush brush2 = new SolidBrush(Color.Black);
                Brush brush3 = new SolidBrush(Color.Red);
                Brush brush4 = new SolidBrush(Color.Green);
                Brush brush5 = new SolidBrush(Color.Orange);
                Brush brush6 = new SolidBrush(Color.DarkBlue);
                //创建两个Font对象,用于设置字体
                Font font1 = new Font("Courier New", 16, FontStyle.Bold);
                Font font2 = new Font("Courier New", 8);
 
                g.FillRectangle(brush1, 0, 0, width, height);//绘制背景图
                g.DrawString("投票结果", font1, brush2, new Point(90, 20));//绘制标题
 
                Point p1 = new Point(70, 50);
                Point p2 = new Point(230, 50);
 
                g.DrawLine(new Pen(Color.Black), p1, p2);//绘制一条直线
 
                g.DrawString("用一生去下载你",font2,brush2,new Point(10,80));
                g.DrawString("一生所爱", font2, brush2, new Point(32, 110));
                g.DrawString("云也相逢", font2, brush2, new Point(32, 140));
                g.DrawString("情茧", font2, brush2, new Point(54, 170));
 
                g.FillRectangle(brush3, 100, 80, (int)tp1, 17);//100,代表是横向,80代表是纵向,(int)tp1代表是矩形长,17代表是宽
                g.FillRectangle(brush4, 100, 110, (int)tp2, 17);
                g.FillRectangle(brush5, 100, 140, (int)tp3, 17);
                g.FillRectangle(brush6, 100, 170, (int)tp4, 17);
 
              
                g.DrawString("用一生去下载:13票", font2, brush2, new Point(25, 220));
                g.DrawString("一生所爱:1票", font2, brush2, new Point(155, 220));
                g.DrawString("云也相逢:1票", font2, brush2, new Point(25, 250));
                g.DrawString("情茧:1票", font2, brush2, new Point(155, 250));
                g.DrawRectangle(new Pen(Color.Green), 10, 210, 280, 80);
 
                pictureBox1.Image = bitmap;
 

            }
            catch (Exception)
            {
               
                throw;
            }
       
        }

 

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值