創(chuàng)新互聯(lián)www.cdcxhl.cn八線動(dòng)態(tài)BGP香港云服務(wù)器提供商,新人活動(dòng)買(mǎi)多久送多久,劃算不套路!
創(chuàng)新互聯(lián)建站10多年企業(yè)網(wǎng)站制作服務(wù);為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì)及高端網(wǎng)站定制服務(wù),企業(yè)網(wǎng)站制作及推廣,對(duì)混凝土攪拌站等多個(gè)行業(yè)擁有多年的網(wǎng)站維護(hù)經(jīng)驗(yàn)的網(wǎng)站建設(shè)公司。小編給大家分享一下C#繪制實(shí)時(shí)折線圖波形圖的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
此Demo是采用VS自帶的Chart圖表控件,制作實(shí)時(shí)動(dòng)態(tài)顯示的折線圖,和波形圖。
涉及知識(shí)點(diǎn):
Chart 控件,功能強(qiáng)大,可以繪制柱狀圖,折線圖,波形圖,餅狀圖,大大簡(jiǎn)化了對(duì)圖的開(kāi)發(fā)與定制。
Chart控件的相關(guān)概念:
Queue集合,表示先進(jìn)先出的集合。
主要有兩個(gè)方法:
Timer ,定時(shí)器,定時(shí)之行相應(yīng)的功能,更新數(shù)據(jù),刷新圖表。
-----------------------------------------------------------------------------------------------------------
效果圖
如下【先點(diǎn)擊初始化按鈕,再點(diǎn)擊開(kāi)始按鈕】:
折線圖【折線圖,是取[0,100]之間的隨即數(shù)進(jìn)行填充】:
波形圖【波形圖,是取正玄值,并放大50倍,然后上移50】
核心代碼
代碼如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace WindowsFormsApplication1 { public partial class RealChart : Form { private QueuedataQueue = new Queue (100); private int curValue = 0; private int num = 5;//每次刪除增加幾個(gè)點(diǎn) public RealChart() { InitializeComponent(); } /// /// 初始化事件 /// /// /// private void btnInit_Click(object sender, EventArgs e) { InitChart(); } ////// 開(kāi)始事件 /// /// /// private void btnStart_Click(object sender, EventArgs e) { this.timer1.Start(); } ////// 停止事件 /// /// /// private void btnStop_Click(object sender, EventArgs e) { this.timer1.Stop(); } ////// 定時(shí)器事件 /// /// /// private void timer1_Tick(object sender, EventArgs e) { UpdateQueueValue(); this.chart1.Series[0].Points.Clear(); for(int i=0;i/// 初始化圖表 /// private void InitChart() { //定義圖表區(qū)域 this.chart1.ChartAreas.Clear(); ChartArea chartArea1 = new ChartArea("C1"); this.chart1.ChartAreas.Add(chartArea1); //定義存儲(chǔ)和顯示點(diǎn)的容器 this.chart1.Series.Clear(); Series series1 = new Series("S1"); series1.ChartArea = "C1"; this.chart1.Series.Add(series1); //設(shè)置圖表顯示樣式 this.chart1.ChartAreas[0].AxisY.Minimum = 0; this.chart1.ChartAreas[0].AxisY.Maximum =100; this.chart1.ChartAreas[0].AxisX.Interval = 5; this.chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Silver; this.chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Silver; //設(shè)置標(biāo)題 this.chart1.Titles.Clear(); this.chart1.Titles.Add("S01"); this.chart1.Titles[0].Text = "XXX顯示"; this.chart1.Titles[0].ForeColor = Color.RoyalBlue; this.chart1.Titles[0].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F); //設(shè)置圖表顯示樣式 this.chart1.Series[0].Color = Color.Red; if (rb1.Checked) { this.chart1.Titles[0].Text =string.Format( "XXX {0} 顯示",rb1.Text); this.chart1.Series[0].ChartType = SeriesChartType.Line; } if (rb2.Checked) { this.chart1.Titles[0].Text = string.Format("XXX {0} 顯示", rb2.Text); this.chart1.Series[0].ChartType = SeriesChartType.Spline; } this.chart1.Series[0].Points.Clear(); } //更新隊(duì)列中的值 private void UpdateQueueValue() { if (dataQueue.Count > 100) { //先出列 for (int i = 0; i < num; i++) { dataQueue.Dequeue(); } } if (rb1.Checked) { Random r = new Random(); for (int i = 0; i < num; i++) { dataQueue.Enqueue(r.Next(0, 100)); } } if (rb2.Checked) { for (int i = 0; i < num; i++) { //對(duì)curValue只取[0,360]之間的值 curValue = curValue % 360; //對(duì)得到的正玄值,放大50倍,并上移50 dataQueue.Enqueue((50*Math.Sin(curValue*Math.PI / 180))+50); curValue=curValue+10; } } } } }