網站首頁 編程語言 正文
本文實例為大家分享了C#實現在窗體上的統計圖,供大家參考,具體內容如下
忽然要用到C#來制作統計圖,起初不太清除怎么弄,看了一些代碼實現統計圖的文檔。終于實現了條形圖的設置,后來又需要餅狀圖。本來還是想自己畫的后來才發現C#是有這種窗體控件的,控件畫比我強太多了。
這是我自己做的條形圖的函數:
public static int width = 400, height = 400;//聲明寬與高 ? Bitmap bitmap = new Bitmap(width, height);//創建一個繪圖對象 ?? ?//這四個參數分別是表的列名,tp1,tp2,tp3均是條形的長,s1,s2,s3代表每個條形的數據 ?? ?public void createImage(string s, int tp1, int tp2,int tp3, string s1, string s2,string s3) ? ? ? ? { ? ? ? ? ? ? Graphics g = Graphics.FromImage(bitmap); ? ? ? ? ? ? g.FillRectangle(Brushes.Black, 0, 0, 400, 400);//邊框 ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? g.Clear(Color.White); ? ? ? ? ? ? ? ? //創建6個brush,用于填充顏色 ? ? ? ? ? ? ? ? Brush brush1 = new SolidBrush(Color.White); ? ? ? ? ? ? ? ? Brush brush2 = new SolidBrush(Color.Black); ? ? ? ? ? ? ? ? Brush brush3 = new SolidBrush(Color.Red); ? ? ? ? ? ? ? ? Brush brush4 = new SolidBrush(Color.Green); ? ? ? ? ? ? ? ? Brush brush5 = new SolidBrush(Color.Orange); ? ? ? ? ? ? ? ? Brush brush6 = new SolidBrush(Color.DarkBlue); ? ? ? ? ? ? ? ? //創建兩個Font對象,用于設置字體 ? ? ? ? ? ? ? ? Font font1 = new Font("宋體", 16, FontStyle.Bold); ? ? ? ? ? ? ? ? Font font2 = new Font("Courier New", 8); ? ? ? ? ? ? ? ? g.FillRectangle(brush1, 0, 0, width, height);//繪制背景圖 ? ? ? ? ? ? ? ? g.DrawString(s, font1, brush2, new Point(90, 20));//繪制標題 ? ? ? ? ? ? ? ? Point p1 = new Point(30, 300); ? ? ? ? ? ? ? ? Point p2 = new Point(330, 300); ? ? ? ? ? ? ? ? //這里用到的減式是由于畫布的坐標系和數學中的坐標系不同,參考下面的圖 ? ? ? ? ? ? ? ? g.DrawString(s1, font2, brush2, new Point(90, 285 - (tp1 * 10))); ? ? ? ? ? ? ? ? g.DrawString(s2, font2, brush2, new Point(150, 285 - (tp2 * 10))); ? ? ? ? ? ? ? ? g.DrawString(s2, font2, brush2, new Point(210, 285 - (tp3 * 10))); ? ? ? ? ? ? ? ? g.FillRectangle(brush3, 90, 300 - (tp1 * 10), 50, tp1 * 10);// ? ? ? ? ? ? ? ? g.FillRectangle(brush4, 150, 300 - (tp2 * 10), 50, tp2 * 10);// ? ? ? ? ? ? ? ? g.FillRectangle(brush5, 210, 300 - (tp3 * 10), 50, tp3 * 10); ? ? ? ? ? ? ? ? g.DrawLine(new Pen(Color.Black), p1, p2);//繪制一條直線 ? ? ? ? ? ? ? ? pictureBox1.Image = bitmap; ? ? ? ? ? ? } ? ? ? ? ? ? catch (Exception) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? throw; ? ? ? ? ? ? } ? ? ? ? }
畫布中的坐標系:
如果換成C#中的chart控件來做就簡單多了
1.找到控件的Legends屬性把其中的Enabled改成False。
2.找到控件的Series屬性把其中的ChartType改成你需要的表形式。
3.接下就是為chart控件添加數據。
添加數據的方式之一:
string[] xData = { "經理", "總監", "銷售" }; ? int[] yData = {10, 20, 30}; ? chart1.Series[0]["PieLabelStyle"] = "Outside";//將文字移到外側 ? chart1.Series[0]["PieLineColor"] = "Black";//繪制黑色的連線。 ? chart1.Series[0].Points.DataBindXY(xData, yData);
原文鏈接:https://blog.csdn.net/Adollar/article/details/106576602
相關推薦
- 2022-07-18 CSS基礎語法和盒模型
- 2022-12-15 Android?NotificationListenerService?通知服務原理解析_Andro
- 2022-06-06 web前端實現水平垂直居中、position、relative、absolute、transform
- 2022-04-12 Http 請求常見狀態碼報錯(200/404/500)
- 2022-09-13 Nginx如何限制IP訪問只允許特定域名訪問_nginx
- 2022-07-17 Docker?Push?Skipped?foreign?layer?的錯誤問題及解決方案_docke
- 2022-02-17 使用Postman測試接口提示Error: connect ECONNREFUSED 127.0.0
- 2022-09-02 pytest使用@pytest.mark.parametrize()實現參數化的示例代碼_pytho
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支