C#使用Chart繪制曲線
更新時間:2022年02月17日 09:17:48 作者:金增輝
這篇文章主要為大家詳細介紹了C#使用Chart繪制曲線,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C#使用Chart繪制曲線的具體代碼,供大家參考,具體內(nèi)容如下
新建一個控制臺應(yīng)用程序,程序名: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) ? ? ? ? { ? ? ? ? ? ? // 設(shè)置曲線的樣式 ? ? ? ? ? ? Series series = chart1.Series[0]; ? ? ? ? ? ? // 畫樣條曲線(Spline) ? ? ? ? ? ? series.ChartType = SeriesChartType.Spline; ? ? ? ? ? ? // 線寬2個像素 ? ? ? ? ? ? series.BorderWidth = 2; ? ? ? ? ? ? // 線的顏色:紅色 ? ? ? ? ? ? series.Color = System.Drawing.Color.Red; ? ? ? ? ? ? // 圖示上的文字 ? ? ? ? ? ? series.LegendText = "動態(tài)曲線"; ? ? ? ? ? ? // 測試代碼 數(shù)據(jù)轉(zhuǎn)換 ? ? ? ? ? ? int values = Convert.ToInt16(textBox1.Text); ? ? ? ? ? ? // 在chart中顯示數(shù)據(jù) ? ? ? ? ? ? int x = 0; ? ? ? ? ? ? series.Points.AddXY(x, values); ? ? ? ? ? ? x++; ? ? ? ? ? ? if (x == 100) x = 0; ? ? ? ? ? ? // 設(shè)置顯示范圍 ? ? ? ? ? ? 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#更新數(shù)組實現(xiàn)動態(tài)曲線:
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]; ? ?//實例化坐標數(shù)組* ? ? ? ? ? ? int data = random.Next(-800, 800); ? //生成一個隨機數(shù) ? ? ? ? ? ?// Point p; ? ? ? //申明一個坐標變量 ? ? ? ? ? ? for (int i = 1; i <50 ; i++) ? ? ? ? //更新數(shù)組 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? 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); //填充邊框所定義的橢圓內(nèi)部,由橢圓邊框左上角坐標和橢圓寬度和高度組成 ? ? ? ? ? ? ? ? ? ? ? ? //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 ? ? ? ? ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c#獲取字符串寬度的示例代碼(字節(jié)數(shù)方法)
本篇文章主要介紹了c#獲取字符串寬度的示例代碼(字節(jié)數(shù)方法)。需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01C#如何實現(xiàn)dataGridView動態(tài)綁定數(shù)據(jù)
這篇文章主要介紹了C#如何實現(xiàn)dataGridView動態(tài)綁定數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04c#將字節(jié)數(shù)組轉(zhuǎn)成易讀的字符串的實現(xiàn)
這篇文章主要介紹了c#將字節(jié)數(shù)組轉(zhuǎn)成易讀的字符串的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01