網站首頁 編程語言 正文
本文實例為大家分享了C#使用Chart繪制曲線的具體代碼,供大家參考,具體內容如下
新建一個控制臺應用程序,程序名:WindowsFormsApp2,將下面的代碼拷貝進去即可
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace WindowsFormsApp2 { ? ? public partial class Form1 : Form ? ? { ? ? ? ? public Form1() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? ?? ? ? ? ? ? ?? ? ? ? ? ? ? timer1.Interval = 5000; ? ? ? ? ? ? textBox1.Text = "0"; ? ? ?? ? ? ? ? } ? ? ? ? private void Form1_Load(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ?? ? ? ? ? } ? ? ? ? private void chart1_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? } ? ? ? ? private void button1_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? timer1.Start(); ? ? ? ? } ? ? ? ? private void timer1_Tick(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? // 設置曲線的樣式 ? ? ? ? ? ? Series series = chart1.Series[0]; ? ? ? ? ? ? // 畫樣條曲線(Spline) ? ? ? ? ? ? series.ChartType = SeriesChartType.Spline; ? ? ? ? ? ? // 線寬2個像素 ? ? ? ? ? ? series.BorderWidth = 2; ? ? ? ? ? ? // 線的顏色:紅色 ? ? ? ? ? ? series.Color = System.Drawing.Color.Red; ? ? ? ? ? ? // 圖示上的文字 ? ? ? ? ? ? series.LegendText = "動態曲線"; ? ? ? ? ? ? // 測試代碼 數據轉換 ? ? ? ? ? ? int values = Convert.ToInt16(textBox1.Text); ? ? ? ? ? ? // 在chart中顯示數據 ? ? ? ? ? ? int x = 0; ? ? ? ? ? ? series.Points.AddXY(x, values); ? ? ? ? ? ? x++; ? ? ? ? ? ? if (x == 100) x = 0; ? ? ? ? ? ? // 設置顯示范圍 ? ? ? ? ? ? ChartArea chartArea = chart1.ChartAreas[0]; ? ? ? ? ? ? chartArea.AxisX.Minimum = 0; ? ? ? ? ? ? chartArea.AxisX.Maximum = System.Double.NaN; ? ? ? ? ? ? chartArea.AxisY.Minimum = 0d; ? ? ? ? ? ? chartArea.AxisY.Maximum = System.Double.NaN; ?//自動去判斷 ? ? ? ? } ? ? ? ? private void textBox1_TextChanged(object sender, EventArgs e) ? ? ? ? { ? ? ? ? } ? ? ? ? private void textBox2_TextChanged(object sender, EventArgs e) ? ? ? ? { ? ? ? ? } ? ? } }
小編分享一段收藏的代碼:C#更新數組實現動態曲線:
private void timer定時_Tick(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? if (Flash == 0) ? ? ? ? ?//當刷新時 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Pen middleLine = new Pen(Color.White); ? ? ? ? ? ? ? ? SolidBrush blackBrush = new SolidBrush(Color.Black); ? //定義了一個單色的黑色畫筆 ? ? ? ? ? ? ? ? graphic = Graphics.FromImage(pictureBox圖像.Image); ? ? ? ? ? ? ? ? graphic.FillRectangle(blackBrush, 0, 0, 500, 251); ? ? //將畫布填充為黑色矩形 ? ? ? ? ? ? ? ? ? ? ? ? ? ? Point pt1 = new Point(0, 125); ? ? ? ? ?//中間線的兩點 ? ? ? ? ? ? ? ? Point pt2 = new Point(500, 125); ? ? ? ? ? ? ? ? graphic.DrawLine(middleLine, pt1, pt2); ? ? ? ?//畫中間線 ? ? ? ? ? ? } ? ? ? ? ? ? ? ? Random random = new Random(); ? ? ? ? ? ? pointList = new Point[50]; ? ?//實例化坐標數組* ? ? ? ? ? ? int data = random.Next(-800, 800); ? //生成一個隨機數 ? ? ? ? ? ?// Point p; ? ? ? //申明一個坐標變量 ? ? ? ? ? ? for (int i = 1; i <50 ; i++) ? ? ? ? //更新數組 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? N[i - 1] = N[i]; ? ? ? ? ? ? ? ? pointList[i - 1] = pointList[i]; ? ? ?//更新點的坐標 ? ? ? ? ? ? } ? ? ? ? ? ? N[49] = data; ? ? ? ? ? ? pointList[49].X = 49 * time; ? ? ? ? ? ? pointList[49].Y = data; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (Flash == 0) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if (Flash == 0) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? int k1 = 0, k2 = 0; ? ? ? ? ? ? ? ? ? ? for (int i = 0; i < 49; i++) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? k1 = (int)(lineD - N[i] / times); ? ? ? ? ? ? ? ? ? ? ? ? k2 = (int)(lineD - N[i + 1] / times); ? ? ? ? ? ? ? ? ? ? ? ? graphic.FillEllipse(new SolidBrush(Color.Red), (i * time), k1, 4, 4); //填充邊框所定義的橢圓內部,由橢圓邊框左上角坐標和橢圓寬度和高度組成 ? ? ? ? ? ? ? ? ? ? ? ? //graphic.DrawLine(new Pen(Color.FromArgb(255, 255, 0), 1), new Point(i * time, k1), new Point((i + 1) * time, k2)); ? //兩點之間連線 ? ? ? ? ? ? ? ? ? ? ? ? pointList[i].X = i * time; ?pointList[i].Y = k1;? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? graphic.FillEllipse(new SolidBrush(Color.Red), (49 * time), k2, 4, 4); ? ? ? ? ? ? ? ? ? ? graphic.DrawCurve(new Pen(Color.FromArgb(255, 255, 0), 1), pointList, 0.5f); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? pictureBox圖像.Refresh(); //刷新picturebox ? ? ? ? ? ? } }
原文鏈接:https://blog.csdn.net/qq_18975227/article/details/109226563
相關推薦
- 2022-06-16 linux系統安裝hadoop真分布式集群詳解_云計算技術
- 2023-04-18 Python中selenium獲取token的方法_python
- 2023-07-13 css對盒模型的理解
- 2022-07-07 Pytorch卷積神經網絡遷移學習的目標及好處_python
- 2023-07-31 elementui中el-tabs切換實時更新數據
- 2022-04-15 基于MFC實現自定義復選框效果_C 語言
- 2022-07-04 python?selenium模擬點擊問題解決方案_python
- 2022-11-21 Go語言讀寫鎖RWMutex的源碼分析_Golang
- 最近更新
-
- 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同步修改后的遠程分支