<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了C#繪製餅狀圖和柱狀圖的具體程式碼,供大家參考,具體內容如下
#程式碼如下:
using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace TEST3._2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { BarMap(); } private void BitMap()//餅狀圖 { int[] saleNum = { 300, 500, 400 }; int sum = 0, threeNum = 0, fourNum = 0, fiveNum = 0; for (int i = 0; i < saleNum.Length; i++) { sum += saleNum[i]; if (i == 0) threeNum = saleNum[0]; else if (i == 1) fourNum = saleNum[1]; else fiveNum = saleNum[2]; } int height = pictureBox1.Height, width = pictureBox1.Width; Bitmap bitmap = new Bitmap(width, height); Graphics g = Graphics.FromImage(bitmap); g.Clear(Color.White); Pen pen1 = new Pen(Color.Red); Brush brush1 = new SolidBrush(Color.PowderBlue); Brush brush2 = new SolidBrush(Color.Blue); Brush brush3 = new SolidBrush(Color.Wheat); Brush brush4 = new SolidBrush(Color.Orange); Font font1 = new Font("Couriter New", 16, FontStyle.Bold); Font font2 = new Font("Couriter New", 10); g.FillRectangle(brush1, 0, 0, width, height); g.DrawString("每月銷售佔比餅狀圖", font1, brush2, new Point(70, 20)); int piex = 100, piey = 60, piew = 200, pieh = 200; float angle1 = Convert.ToSingle((360 / Convert.ToSingle(sum)) * Convert.ToSingle(threeNum)); float angle2 = Convert.ToSingle((360 / Convert.ToSingle(sum)) * Convert.ToSingle(fourNum)); float angle3 = Convert.ToSingle((360 / Convert.ToSingle(sum)) * Convert.ToSingle(fiveNum)); g.FillPie(brush2, piex, piey, piew, pieh, 0, angle1); g.FillPie(brush3, piex, piey, piew, pieh, angle1, angle2); g.FillPie(brush4, piex, piey, piew, pieh, angle1 + angle2, angle3); g.DrawRectangle(pen1, 50, 300, 310, 130); g.FillRectangle(brush2, 90, 320, 20, 10); g.DrawString(string.Format("3月份銷量佔比:{0:P2}", Convert.ToSingle(threeNum) / Convert.ToSingle(sum)), font2, brush2, 120, 320); g.FillRectangle(brush3, 90, 360, 20, 10); g.DrawString(string.Format("4月份銷量佔比:{0:P2}", Convert.ToSingle(fourNum) / Convert.ToSingle(sum)), font2, brush2, 120, 360); g.FillRectangle(brush4, 90, 400, 20, 10); g.DrawString(string.Format("5月份銷量佔比:{0:P2}", Convert.ToSingle(fiveNum) / Convert.ToSingle(sum)), font2, brush2, 120, 400); this.groupBox1.Text = "餅狀圖"; this.pictureBox1.Width = bitmap.Width; this.pictureBox1.Height = bitmap.Height; this.pictureBox1.BackgroundImage = bitmap; } private void BarMap()//柱狀圖 { int[] saleNum = { 300, 500, 400 }; int sum = saleNum[0]+ saleNum[1]+ saleNum[2]; float[] Y_Num ={ Convert.ToSingle(saleNum[0]) / Convert.ToSingle(sum),Convert.ToSingle(saleNum[1]) / Convert.ToSingle(sum), Convert.ToSingle(saleNum[2]) / Convert.ToSingle(sum) }; int height = pictureBox1.Height, width = pictureBox1.Width; Bitmap image = new Bitmap(width, height); //建立Graphics類物件 Graphics g = Graphics.FromImage(image); try { //清空圖片背景色 g.Clear(Color.White); Font font = new Font("Arial", 10, FontStyle.Regular); Font font1 = new Font("宋體", 20, FontStyle.Bold); LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),Color.Blue, Color.BlueViolet, 1.2f, true); Font font2 = new System.Drawing.Font("Arial", 10, FontStyle.Bold); SolidBrush mybrush = new SolidBrush(Color.Red); SolidBrush mybrush2 = new SolidBrush(Color.Green); Pen mypen = new Pen(brush, 1); //繪製線條 //繪製橫向線條 int x = 100; Pen mypen1 = new Pen(Color.Blue, 2); x = 60; g.DrawLine(mypen1, x, 0, x, 300); //繪製縱向線條 int y = 0; for (int i = 0; i <11; i++) { g.DrawLine(mypen, 45, y, 60, y); y = y + 30; } g.DrawLine(mypen1, 60, y-30, 620, y-30); //x軸 String[] n = { "3月份", "4月份", "5月份"}; x = 100; for (int i = 0; i < 3; i++) { g.DrawString(n[i].ToString(), font, Brushes.Blue, x, 300); //設定文字內容及輸出位置 Console.WriteLine(300 - Y_Num[i] * 100 * 3); g.FillRectangle(mybrush, x, 300 - Y_Num[i] * 100 * 3, 20, Y_Num[i] * 100 * 3); g.DrawString(Y_Num[i].ToString(), font2, Brushes.Green, x, 300 - Y_Num[i] * 100 * 3 - 15); x = x + 100; } //y軸 String[] m = {"0","0.10","0.20", "0.30", "0.40", "0.50", "0.60", "0.70", " 0.80"," 0.90", " 1.00" }; y = 0; for (int i = 10; i >= 0; i--) { g.DrawString(m[i].ToString(), font, Brushes.Blue, 20, y); //設定文字內容及輸出位置 y = y + 30; } //繪製標識 Font font3 = new System.Drawing.Font("Arial", 10, FontStyle.Regular); g.DrawRectangle(new Pen(Brushes.Blue), 170, 390, 250, 50); //繪製範圍框 g.FillRectangle(Brushes.Red, 200, 410, 20, 10); //繪製小矩形 g.DrawString("月銷量佔比", font3, Brushes.Red, 292, 408); this.button1.Text = "檢視餅狀圖"; this.groupBox1.Text = "柱狀圖"; this.pictureBox1.Width = image.Width; this.pictureBox1.Height = image.Height; this.pictureBox1.BackgroundImage = image; } catch { } } private void Button1_Click(object sender, EventArgs e) { BitMap(); this.button1.Visible =false;//隱藏button } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援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