C# Chart繪制簡(jiǎn)單圖形波形
本文實(shí)例為大家分享了C# Chart繪制簡(jiǎn)單圖形波形的具體代碼,供大家參考,具體內(nèi)容如下
此次用C#繪制波形使用的是Chart控件
1、將Chart控件拖進(jìn)主界面,然后設(shè)置屬性。
//? // chart1 //? chartArea2.Name = "ChartArea1"; this.chart1.ChartAreas.Add(chartArea2); legend2.Name = "Legend1"; this.chart1.Legends.Add(legend2); this.chart1.Location = new System.Drawing.Point(36, 6); this.chart1.Name = "chart1"; series3.BorderWidth = 3; series3.ChartArea = "ChartArea1"; series3.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; series3.Legend = "Legend1"; series3.Name = "系列1"; series4.BorderWidth = 2; series4.ChartArea = "ChartArea1"; series4.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line; series4.Legend = "Legend1"; series4.Name = "系列2"; this.chart1.Series.Add(series3); this.chart1.Series.Add(series4); this.chart1.Size = new System.Drawing.Size(839, 499); this.chart1.TabIndex = 0; this.chart1.Text = "chart1";
2、畫線委托函數(shù)
public delegate void ChartDelegate(int series, double Value_Y_Axis); // 聲明委托 public void DrawPoint(int series, double Value_Y_Axis) { ?? ?try ?? ?{ ?? ??? ?if (this.chart1.InvokeRequired) ?? ??? ?{ ?? ??? ??? ?this.chart1.Invoke((ChartDelegate)DrawPoint, series, Value_Y_Axis); ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?this.chart1.Series[series - 1].Points.Add(Value_Y_Axis); ?? ??? ?} ?? ?} ?? ?catch ?? ?{ ?? ??? ?MessageBox.Show("ERROR!"); ?? ?} ?? ?//chart1.Series[series - 1].Points.Add(Value_Y_Axis); }
3、設(shè)置一個(gè)按鍵,然后設(shè)置按鍵點(diǎn)擊觸發(fā)時(shí)候的定時(shí)器Timer屬性,用來(lái)觸發(fā)波形產(chǎn)生(用控件按鍵觸發(fā))
private void button3_Click(object sender, EventArgs e) { ?? ?System.Timers.Timer timer11 = new System.Timers.Timer(400); // 每400ms觸發(fā)一次 ?? ?timer11.Elapsed += new System.Timers.ElapsedEventHandler(timer_handle); // 執(zhí)行函數(shù)tiemr_handle ?? ?timer11.AutoReset = true; ?? ?timer11.Enabled = true; ?? ?timer11.Start(); }
4、定時(shí)器觸發(fā)執(zhí)行函數(shù)
public int gIndex = 1; public void timer_handle(object source, System.Timers.ElapsedEventArgs e) { ?? ?double mpre1 = Log10(gIndex) * 300; ?? ?double mpre2 = Sin(gIndex) * 200; ?? ?// ?? ?DrawPoint(1, mpre1); ?? ?DrawPoint(2, mpre2); ?? ?gIndex++; }
效果動(dòng)態(tài)圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)動(dòng)態(tài)執(zhí)行字符串腳本(優(yōu)化版)的示例代碼
這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)動(dòng)態(tài)執(zhí)行字符串腳本(優(yōu)化版),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03C# 16進(jìn)制與字符串、字節(jié)數(shù)組之間的轉(zhuǎn)換
在串口通訊過(guò)程中,經(jīng)常要用到 16進(jìn)制與字符串、字節(jié)數(shù)組之間的轉(zhuǎn)換2009-05-05