博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WinForm绘制柱形图
阅读量:5067 次
发布时间:2019-06-12

本文共 5050 字,大约阅读时间需要 16 分钟。

一、绘制简单的柱形图

 

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

 

 
posted on
2018-02-03 16:25 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/zyadmin/p/8409813.html

你可能感兴趣的文章
WPF Layout 系统概述——Arrange
查看>>
PIGOSS
查看>>
几款Http小服务器
查看>>
openSuse beginner
查看>>
Codeforces 620E(线段树+dfs序+状态压缩)
查看>>
css3动画属性
查看>>
Mongodb 基本命令
查看>>
控制文件的备份与恢复
查看>>
软件目录结构规范
查看>>
mysqladmin
查看>>
解决 No Entity Framework provider found for the ADO.NET provider
查看>>
设置虚拟机虚拟机中fedora上网配置-bridge连接方式(图解)
查看>>
[置顶] Android仿人人客户端(v5.7.1)——人人授权访问界面
查看>>
ES6内置方法find 和 filter的区别在哪
查看>>
Android实现 ScrollView + ListView无滚动条滚动
查看>>
java学习笔记之String类
查看>>
UVA 11082 Matrix Decompressing 矩阵解压(最大流,经典)
查看>>
硬件笔记之Thinkpad T470P更换2K屏幕
查看>>
iOS开发——缩放图片
查看>>
HTTP之URL的快捷方式
查看>>