C#使用Chart繪制曲線
更新時間:2022年02月17日 09:17:48 作者:金增輝
這篇文章主要為大家詳細介紹了C#使用Chart繪制曲線,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了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 = "動態(tài)曲線"; ? ? ? ? ? ? // 測試代碼 數據轉換 ? ? ? ? ? ? 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#更新數組實現動態(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]; ? ?//實例化坐標數組* ? ? ? ? ? ? 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 ? ? ? ? ? ? } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。