WinForm绘制柱形图
生活随笔
收集整理的這篇文章主要介紹了
WinForm绘制柱形图
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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;}
?
posted on 2018-02-03 16:25 adminyu 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/zyadmin/p/8409813.html
總結
以上是生活随笔為你收集整理的WinForm绘制柱形图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS11适配tableView顶部空白
- 下一篇: 为什么要逃离物理?