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;
            }
       
        }

展开阅读全文

没有更多推荐了,返回首页