網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了C#繪制餅狀圖和柱狀圖的具體代碼,供大家參考,具體內(nèi)容如下
#代碼如下:
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); ? ? ? ? ? ? //創(chuàng)建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); //設置文字內(nèi)容及輸出位置 ? ? ? ? ? ? ? ? ? ? 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); //設置文字內(nèi)容及輸出位置 ? ? ? ? ? ? ? ? ? ? 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 ? ? ? ? } ? ? ? ? ? } }
原文鏈接:https://blog.csdn.net/qq_15042311/article/details/89724877
相關推薦
- 2022-08-27 .Net實現(xiàn)延遲隊列_實用技巧
- 2022-03-19 MongoDB數(shù)據(jù)庫授權(quán)認證的實現(xiàn)_MongoDB
- 2022-04-05 用Python實現(xiàn)局域網(wǎng)控制電腦_python
- 2023-07-31 TypeError:cannot read property ‘getAttribute‘ of
- 2022-09-21 WPF+SkiaSharp實現(xiàn)自繪拖曳小球_C#教程
- 2023-03-04 c++元編程模板函數(shù)重載匹配規(guī)則示例詳解_C 語言
- 2022-08-11 利用python繪制線型圖_python
- 2023-02-23 一文詳解C語言char類型中的存儲_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支