<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了C#繪製柱狀圖和折線圖的具體程式碼,供大家參考,具體內容如下
執行效果如下圖:
設計上面的柱狀圖和折線圖其實並沒有什麼難度,主要是各個座標的計算,完全是精細活。首先在表單在新增了一個tabControl控制元件來切換柱狀圖和折線圖的顯示,在tabPage1中顯示柱狀圖,在tabPage2中顯示折線圖。然後在各自的Page頁屬性中定義Paint事件,具體實現過程可以從下面的程式碼中看到。
程式碼如下:
新增標頭檔案:
using System.Drawing.Drawing2D;
tabPage1的Paint事件(畫柱狀圖):
private void tabPage1_Paint(object sender, PaintEventArgs e) { BackColor = Color.White; //標題 Graphics g = tabPage1.CreateGraphics(); Font f = new Font("宋體", 24, FontStyle.Regular); Pen p = new Pen(Color.Blue); g.DrawString("報名及考試統計柱狀圖", f, p.Brush, 200, 20); //畫表格 for (int i = 0; i <= 9; i++) { g.DrawLine(p, 30, 90 + 31 * i, 620, 90 + 31 * i); } for (int i = 1; i <= 14; i++) { g.DrawLine(p, 30 + 42 * i, 60, 30 + 42 * i, 370); } Pen MyPen = new Pen(Color.Blue, 2); Point p1 = new Point(30, 60); Point p2 = new Point(30, 370); Point p3 = new Point(30, 370); Point p4 = new Point(620, 370); g.DrawLine(MyPen, p1, p2); g.DrawLine(MyPen, p3, p4); //紅色圖形部分 Pen drawPen = new Pen(Color.Red, 1); SolidBrush mybrush = new SolidBrush(Color.Red); e.Graphics.DrawRectangle(drawPen, 30 + 21, 370 - 41, 21, 41); e.Graphics.FillRectangle(mybrush, 30 + 21, 370 - 41, 21, 41); e.Graphics.DrawRectangle(drawPen, 30 + 42 * 2 + 21, 370 - 31 * 4 - 10, 21, 31 * 4 + 10); e.Graphics.FillRectangle(mybrush, 30 + 42 * 2 + 21, 370 - 31 * 4 - 10, 21, 31 * 4 + 10); e.Graphics.DrawRectangle(drawPen, 30 + 42 * 4 + 21, 370 - 31 * 2 - 20, 21, 31 * 2 + 20); e.Graphics.FillRectangle(mybrush, 30 + 42 * 4 + 21, 370 - 31 * 2 - 20, 21, 31 * 2 + 20); e.Graphics.DrawRectangle(drawPen, 30 + 42 * 6 + 21, 370 - 31 * 1 - 20, 21, 31 * 1 + 20); e.Graphics.FillRectangle(mybrush, 30 + 42 * 6 + 21, 370 - 31 * 1 - 20, 21, 31 * 1 + 20); e.Graphics.DrawRectangle(drawPen, 30 + 42 * 8 + 21, 370 - 31 * 5 - 25, 21, 31 * 5 + 25); e.Graphics.FillRectangle(mybrush, 30 + 42 * 8 + 21, 370 - 31 * 5 - 25, 21, 31 * 5 + 25); e.Graphics.DrawRectangle(drawPen, 30 + 42 * 10 + 21, 370 - 31 * 4 - 7, 21, 31 * 4 + 7); e.Graphics.FillRectangle(mybrush, 30 + 42 * 10 + 21, 370 - 31 * 4 - 7, 21, 31 * 4 + 7); e.Graphics.DrawRectangle(drawPen, 30 + 42 * 12 + 21, 60, 21, 370 - 60); e.Graphics.FillRectangle(mybrush, 30 + 42 * 12 + 21, 60, 21, 370 - 60); //綠色圖形部分 Pen drawPen2 = new Pen(Color.Green, 1); SolidBrush brush = new SolidBrush(Color.Green); e.Graphics.DrawRectangle(drawPen2, 30 + 42, 370 - 31, 21, 31); e.Graphics.FillRectangle(brush, 30 + 42, 370 - 31, 21, 31); e.Graphics.DrawRectangle(drawPen2, 30 + 42 * 3, 370 - 31 * 2 - 15, 21, 31 * 2 + 15); e.Graphics.FillRectangle(brush, 30 + 42 * 3, 370 - 31 * 2 - 15, 21, 31 * 2 + 15); e.Graphics.DrawRectangle(drawPen2, 30 + 42 * 5, 370 - 31 - 10, 21, 41); e.Graphics.FillRectangle(brush, 30 + 42 * 5, 370 - 31 - 10, 21, 41); e.Graphics.DrawRectangle(drawPen2, 30 + 42 * 7, 370 - 16, 21, 16); e.Graphics.FillRectangle(brush, 30 + 42 * 7, 370 - 16, 21, 16); e.Graphics.DrawRectangle(drawPen2, 30 + 42 * 9, 370 - 31 * 3 - 20, 21, 31 * 3 + 20); e.Graphics.FillRectangle(brush, 30 + 42 * 9, 370 - 31 * 3 - 20, 21, 31 * 3 + 20); e.Graphics.DrawRectangle(drawPen2, 30 + 42 * 11, 370 - 31 * 1 - 28, 21, 31 * 1 + 28); e.Graphics.FillRectangle(brush, 30 + 42 * 11, 370 - 31 * 1 - 28, 21, 31 * 1 + 28); e.Graphics.DrawRectangle(drawPen2, 30 + 42 * 13, 370 - 31 * 5 - 15, 21, 31 * 5 + 15); e.Graphics.FillRectangle(brush, 30 + 42 * 13, 370 - 31 * 5 - 15, 21, 31 * 5 + 15); //圖上的文字部分 Font font2 = new Font("宋體", 10, FontStyle.Regular); g.DrawString("第一期", font2, p.Brush, 30 + 21, 375); g.DrawString("第二期", font2, p.Brush, 30 + 42 * 2 + 21, 375); g.DrawString("第三期", font2, p.Brush, 30 + 42 * 4 + 21, 375); g.DrawString("第四期", font2, p.Brush, 30 + 42 * 6 + 21, 375); g.DrawString("上半年", font2, p.Brush, 30 + 42 * 8 + 21, 375); g.DrawString("下半年", font2, p.Brush, 30 + 42 * 10 + 21, 375); g.DrawString("全年統計", font2, p.Brush, 30 + 42 * 12 + 21, 375); //圖上數位部分 g.DrawString("25", font2, p.Brush, 10, 370 - 35); g.DrawString("50", font2, p.Brush, 10, 370 - 35 * 2); g.DrawString("75", font2, p.Brush, 10, 370 - 34 * 3); g.DrawString("100", font2, p.Brush, 5, 370 - 33 * 4); g.DrawString("125", font2, p.Brush, 5, 370 - 33 * 5); g.DrawString("150", font2, p.Brush, 5, 370 - 32 * 6); g.DrawString("175", font2, p.Brush, 5, 370 - 32 * 7); g.DrawString("200", font2, p.Brush, 5, 370 - 32 * 8); g.DrawString("225", font2, p.Brush, 5, 370 - 32 * 9); g.DrawString("250", font2, p.Brush, 5, 370 - 32 * 10); //紅色數 Pen pen2 = new Pen(Color.Red); g.DrawString("39", font2, pen2.Brush, 30 + 21, 370 - 41 - 15); g.DrawString("111", font2, pen2.Brush, 30 + 42 * 2 + 21, 370 - 31 * 4 - 10 - 15); g.DrawString("71", font2, pen2.Brush, 30 + 42 * 4 + 21, 370 - 31 * 2 - 20 - 15); g.DrawString("40", font2, pen2.Brush, 30 + 42 * 6 + 21, 370 - 31 * 1 - 20 - 15); g.DrawString("150", font2, pen2.Brush, 30 + 42 * 8 + 21, 370 - 31 * 5 - 25 - 15); g.DrawString("111", font2, pen2.Brush, 30 + 42 * 10 + 21, 370 - 31 * 4 - 7 - 15); g.DrawString("261", font2, pen2.Brush, 30 + 42 * 12 + 21, 60 - 15); //綠色數 Pen pen3 = new Pen(Color.Green); g.DrawString("39", font2, pen2.Brush, 30 + 21, 370 - 41 - 15); g.DrawString("111", font2, pen2.Brush, 30 + 42 * 2 + 21, 370 - 31 * 4 - 10 - 15); g.DrawString("71", font2, pen2.Brush, 30 + 42 * 4 + 21, 370 - 31 * 2 - 20 - 15); g.DrawString("40", font2, pen2.Brush, 30 + 42 * 6 + 21, 370 - 31 * 1 - 20 - 15); g.DrawString("150", font2, pen2.Brush, 30 + 42 * 8 + 21, 370 - 31 * 5 - 25 - 15); g.DrawString("111", font2, pen2.Brush, 30 + 42 * 10 + 21, 370 - 31 * 4 - 7 - 15); g.DrawString("261", font2, pen2.Brush, 30 + 42 * 12 + 21, 60 - 15); //最下面的矩形框 e.Graphics.DrawRectangle(p, 30 + 42 * 2 + 30, 400, 42 * 7, 31 * 2); e.Graphics.DrawRectangle(drawPen, 30 + 42 * 5, 410, 21, 10); e.Graphics.FillRectangle(mybrush, 30 + 42 * 5, 410, 21, 10); g.DrawString("報名人數", font2, pen2.Brush, 30 + 42 * 6, 410); e.Graphics.DrawRectangle(drawPen2, 30 + 42 * 5, 440, 21, 10); e.Graphics.FillRectangle(brush, 30 + 42 * 5, 440, 21, 10); g.DrawString("通過人數", font2, pen3.Brush, 30 + 42 * 6, 440); }
tabPage2的Paint事件(畫折線圖):
private void tabPage2_Paint(object sender, PaintEventArgs e) { BackColor = Color.White; //標題 Graphics g = tabPage2.CreateGraphics(); Font f = new Font("宋體", 24, FontStyle.Regular); Pen p = new Pen(Color.Blue); g.DrawString("報名及考試統計折線圖", f, p.Brush, 200, 20); //畫表格 for (int i = 0; i <= 9; i++) { g.DrawLine(p, 30, 90 + 31 * i, 620, 90 + 31 * i); } for (int i = 1; i <= 7; i++) { g.DrawLine(p, 30 + 84 * i, 60, 30 + 84 * i, 370); } Pen MyPen = new Pen(Color.Blue, 2); Point p1 = new Point(30, 60); Point p2 = new Point(30, 370); Point p3 = new Point(30, 370); Point p4 = new Point(620, 370); g.DrawLine(MyPen, p1, p2); g.DrawLine(MyPen, p3, p4); //繪製折線 Pen pen1 = new Pen(Color.Red, 2); Pen pen2 = new Pen(Color.Green,2); //紅色折線 Point a1, a2, a3, a4, a5, a6, a7; a1 = new Point(30,370-31-20); a2 = new Point(30+84*1,370-(31*4+9)); a3 = new Point(30 + 84 * 2,370-(31*2+28)); a4 = new Point(30 + 84 * 3, 370 - (31 * 1 + 20)); a5 = new Point(30 + 84 * 4, 370 - (31 * 5 + 21)); a6 = new Point(30 + 84 * 5, 370 - (31 * 4 + 10)); a7 = new Point(30 + 84 * 6, 60); Point[] points = { a1,a2,a3,a4,a5,a6,a7}; g.DrawLines(pen1, points); //綠色折線 Point b1, b2, b3, b4, b5, b6, b7; b1 = new Point(30,370-(31*1+1)); b2 = new Point(30+84*1,370-(31*2+15)); b3 = new Point(30 + 84 * 2, 370 - (31 * 1 + 10)); b4 = new Point(30 + 84 * 3, 370 - (31 * 0 + 15)); b5 = new Point(30 + 84 * 4, 370 - (31 * 3 + 15)); b6 = new Point(30 + 84 * 5, 370 - (31 * 1 + 29)); b7 = new Point(30 + 84 * 6, 370 - (31 * 5 + 14)); Point[] points2 = { b1, b2, b3, b4, b5, b6, b7 }; g.DrawLines(pen2,points2); //圖上數位部分 Font font2 = new Font("宋體", 10, FontStyle.Regular); g.DrawString("25", font2, pen1.Brush, 10, 370 - 35); g.DrawString("50", font2, pen1.Brush, 10, 370 - 35 * 2); g.DrawString("75", font2, pen1.Brush, 10, 370 - 34 * 3); g.DrawString("100", font2, pen1.Brush, 5, 370 - 33 * 4); g.DrawString("125", font2, pen1.Brush, 5, 370 - 33 * 5); g.DrawString("150", font2, pen1.Brush, 5, 370 - 32 * 6); g.DrawString("175", font2, pen1.Brush, 5, 370 - 32 * 7); g.DrawString("200", font2, pen1.Brush, 5, 370 - 32 * 8); g.DrawString("225", font2, pen1.Brush, 5, 370 - 32 * 9); g.DrawString("250", font2, pen1.Brush, 5, 370 - 32 * 10); //文字 g.DrawString("第一期", font2, pen1.Brush, 15, 375); g.DrawString("第二期", font2, pen1.Brush, 15 + 84 * 1, 375); g.DrawString("第三期", font2, pen1.Brush, 15 + 84 * 2, 375); g.DrawString("第四期", font2, pen1.Brush, 15 + 84 * 3, 375); g.DrawString("上半年", font2, pen1.Brush, 15 + 84 * 4, 375); g.DrawString("下半年", font2, pen1.Brush, 15 + 84 * 5, 375); g.DrawString("全年統計", font2, pen1.Brush, 15 + 84 * 6, 375); //折線圖上的數位 g.DrawString("39", font2, pen1.Brush, 30, 370 - 31 - 20 - 15); g.DrawString("111", font2, pen1.Brush, 30 + 84 * 1, 370 - (31 * 4 + 9) - 15); g.DrawString("71", font2, pen1.Brush, 30 + 84 * 2, 370 - (31 * 2 + 28) - 15); g.DrawString("40", font2, pen1.Brush, 30 + 84 * 3, 370 - (31 * 1 + 20) - 15); g.DrawString("150", font2, pen1.Brush, 30 + 84 * 4, 370 - (31 * 5 + 21) - 15); g.DrawString("111", font2, pen1.Brush, 30 + 84 * 5, 370 - (31 * 4 + 10) - 15); g.DrawString("261", font2, pen1.Brush, 30 + 84 * 6, 60 - 15); g.DrawString("26", font2, pen2.Brush, 30, 370 - (31 * 1 + 1) - 15); g.DrawString("68", font2, pen2.Brush, 30 + 84 * 1, 370 - (31 * 2 + 15) - 15); g.DrawString("35", font2, pen2.Brush, 30 + 84 * 2, 370 - (31 * 1 + 10) - 15); g.DrawString("14", font2, pen2.Brush, 30 + 84 * 3, 370 - (31 * 0 + 15) - 15); g.DrawString("94", font2, pen2.Brush, 30 + 84 * 4, 370 - (31 * 3 + 15) - 15); g.DrawString("49", font2, pen2.Brush, 30 + 84 * 5, 370 - (31 * 1 + 29) - 15); g.DrawString("143", font2, pen2.Brush, 30 + 84 * 6, 370 - (31 * 5 + 14) - 15); //最下面的矩形框 SolidBrush mybrush = new SolidBrush(Color.Red); SolidBrush brush = new SolidBrush(Color.Green); e.Graphics.DrawRectangle(pen1, 30 + 42 * 2 + 30, 400, 42 * 7, 31 * 2); e.Graphics.DrawRectangle(pen1, 30 + 42 * 5, 410, 21, 10); e.Graphics.FillRectangle(mybrush, 30 + 42 * 5, 410, 21, 10); g.DrawString("報名人數", font2, pen1.Brush, 30 + 42 * 6, 410); e.Graphics.DrawRectangle(pen2, 30 + 42 * 5, 440, 21, 10); e.Graphics.FillRectangle(brush, 30 + 42 * 5, 440, 21, 10); g.DrawString("通過人數", font2, pen2.Brush, 30 + 42 * 6, 440); }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45