溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#如何實(shí)現(xiàn)chart控件動(dòng)態(tài)曲線繪制

發(fā)布時(shí)間:2022-02-17 09:12:43 來(lái)源:億速云 閱讀:377 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)C#如何實(shí)現(xiàn)chart控件動(dòng)態(tài)曲線繪制,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

具體內(nèi)容如下

思想

實(shí)驗(yàn)室要做一個(gè)動(dòng)態(tài)曲線繪制,網(wǎng)上方法很多,但是缺乏完整代碼和效果圖的整合,往往總是缺少其一,因此整理如下,方便大家編程,節(jié)約時(shí)間。
思路:新建一個(gè)隊(duì)列,利用timer控件,動(dòng)態(tài)的往隊(duì)列中加入數(shù)據(jù),每次觸發(fā)事件,就相當(dāng)于將隊(duì)列中的值全部重新畫(huà)一遍。

我的目的是做四個(gè)點(diǎn)的動(dòng)態(tài)監(jiān)測(cè),所以代碼重復(fù)了四次,其實(shí)應(yīng)該用4個(gè)線程來(lái)做,思路就顯得較為清晰了,這也是可以改進(jìn)的地方。

public partial class 界面_Xtratabcontrol版本_ : Form
    {
        private Queue<double> dataQueue1 = new Queue<double>(100); //30個(gè)就清空一次
        private Queue<double> dataQueue2 = new Queue<double>(100); //30個(gè)就清空一次
        private Queue<double> dataQueue3 = new Queue<double>(100); //30個(gè)就清空一次
        private Queue<double> dataQueue4 = new Queue<double>(100); //30個(gè)就清空一次
        private int stress1 = 0;//設(shè)置一個(gè)壓力值全局變量
        private int stress2 = 0;//設(shè)置一個(gè)壓力值全局變量
        private int stress3 = 0;//設(shè)置一個(gè)壓力值全局變量
        private int stress4 = 0;//設(shè)置一個(gè)壓力值全局變量
        string monthNow = "";
        string monthNext = "";
        string currentTime = "";
        bool isRefresh = false;
        public 界面_Xtratabcontrol版本_()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false; //設(shè)置不自動(dòng)顯示數(shù)據(jù)庫(kù)中未綁定的列
            //設(shè)置隔行背景色
            this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque;
            this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
        }

        private void btnInit_Click(object sender, EventArgs e)
        {
            InitChart1();
            InitChart2();
            InitChart3();
            InitChart4();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            this.timer1.Start();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            this.timer1.Stop();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                UpdateDate(); //根據(jù)當(dāng)前時(shí)間取下一個(gè)數(shù)據(jù),同時(shí)給month賦值
                dataQueue1.Enqueue(stress1); //就是這,不斷往里面加數(shù)據(jù)。
                dataQueue2.Enqueue(stress2);
                dataQueue3.Enqueue(stress3);
                dataQueue4.Enqueue(stress4);
                if (isRefresh)
                {
                    //刷新界面
                    isRefresh = false;
                    InitChart1();
                    InitChart2();
                    InitChart3();
                    InitChart4();
                    dataQueue1.Enqueue(stress1);
                    dataQueue2.Enqueue(stress2);
                    dataQueue3.Enqueue(stress3);
                    dataQueue4.Enqueue(stress4);
                }
                this.chart1.Series[0].Points.Clear();
                this.chart2.Series[0].Points.Clear();
                this.chart3.Series[0].Points.Clear();
                this.chart4.Series[0].Points.Clear();
                for (int i = 0; i < dataQueue1.Count; i++)
                {
                    this.chart1.Series[0].Points.AddXY((i + 1), dataQueue1.ElementAt(i)); 相當(dāng)于每次都是重新畫(huà)一遍
                }
                for (int i = 0; i < dataQueue2.Count; i++)
                {
                    this.chart2.Series[0].Points.AddXY((i + 1), dataQueue2.ElementAt(i)); 相當(dāng)于每次都是重新畫(huà)一遍
                }
                for (int i = 0; i < dataQueue3.Count; i++)
                {
                    this.chart3.Series[0].Points.AddXY((i + 1), dataQueue3.ElementAt(i)); 相當(dāng)于每次都是重新畫(huà)一遍
                }
                for (int i = 0; i < dataQueue4.Count; i++)
                {
                    this.chart4.Series[0].Points.AddXY((i + 1), dataQueue4.ElementAt(i)); 相當(dāng)于每次都是重新畫(huà)一遍
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void InitChart1()
        {
            try
            {
                //定義圖表區(qū)域
                this.chart1.ChartAreas.Clear();
                ChartArea chartArea1 = new ChartArea("C1");
                this.chart1.ChartAreas.Add(chartArea1);
                //this.chart1.Dock = DockStyle.Fill;
                //定義存儲(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 = 30000;
                this.chart1.ChartAreas[0].AxisY.Maximum = 50000;
                this.chart1.ChartAreas[0].AxisX.Minimum = 1;
                this.chart1.ChartAreas[0].AxisX.Maximum = 31;
                this.chart1.ChartAreas[0].AxisX.Interval = 1;
                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 = "1號(hào)監(jiān)測(cè)點(diǎn)";
                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("動(dòng)態(tài) {0} 顯示", rb1.Text);
                    this.chart1.Titles[0].Text = string.Format("1號(hào)監(jiān)測(cè)點(diǎn)");
                    this.chart1.Series[0].ChartType = SeriesChartType.Line;
                }
                if (rb2.Checked)
                {
                    this.chart1.Titles[0].Text = string.Format("動(dòng)態(tài) {0} 顯示", rb1.Text);
                    this.chart1.Series[0].ChartType = SeriesChartType.Spline;
                }
                this.chart1.Series[0].Points.Clear();
                //DBEngine.ConnectDB("orcl", "dt", "6312");
                dataQueue1.Clear();//清空隊(duì)列中所有數(shù)據(jù)
            }
            catch (Exception ex)
            {

            }
        }

        private void InitChart2()
        {
            try
            {
                //定義圖表區(qū)域
                this.chart2.ChartAreas.Clear();
                ChartArea chartArea2 = new ChartArea("C2");
                this.chart2.ChartAreas.Add(chartArea2);
                //this.chart1.Dock = DockStyle.Fill;
                //定義存儲(chǔ)和顯示點(diǎn)的容器
                this.chart2.Series.Clear();
                Series series2 = new Series("S2");
                series2.ChartArea = "C2";
                this.chart2.Series.Add(series2);
                //設(shè)置圖表顯示樣式
                this.chart2.ChartAreas[0].AxisY.Minimum = 30000;
                this.chart2.ChartAreas[0].AxisY.Maximum = 50000;
                this.chart2.ChartAreas[0].AxisX.Minimum = 1;
                this.chart2.ChartAreas[0].AxisX.Maximum = 31;
                this.chart2.ChartAreas[0].AxisX.Interval = 1;
                this.chart2.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Silver;
                this.chart2.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Silver;
                //設(shè)置標(biāo)題
                this.chart2.Titles.Clear();
                this.chart2.Titles.Add("S02");
                this.chart2.Titles[0].Text = "動(dòng)態(tài)折線圖顯示";
                this.chart2.Titles[0].ForeColor = Color.RoyalBlue;
                this.chart2.Titles[0].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F); //標(biāo)題字體
                //設(shè)置圖表顯示樣式
                this.chart2.Series[0].Color = Color.Red;
                if (rb1.Checked)
                {
                    //this.chart2.Titles[0].Text = string.Format("動(dòng)態(tài) {0} 顯示", rb1.Text);
                    this.chart2.Titles[0].Text = string.Format("2號(hào)監(jiān)測(cè)點(diǎn)");
                    this.chart2.Series[0].ChartType = SeriesChartType.Line;
                }
                if (rb2.Checked)
                {
                    this.chart2.Titles[0].Text = string.Format("動(dòng)態(tài) {0} 顯示", rb1.Text);
                    this.chart2.Series[0].ChartType = SeriesChartType.Spline;
                }
                this.chart2.Series[0].Points.Clear();
                //DBEngine.ConnectDB("orcl", "dt", "6312");
                dataQueue2.Clear();//清空隊(duì)列中所有數(shù)據(jù)
            }
            catch (Exception ex)
            {

            }
        }
        private void InitChart3()
        {
            try
            {
                //定義圖表區(qū)域
                this.chart3.ChartAreas.Clear();
                ChartArea chartArea3 = new ChartArea("C3");
                this.chart3.ChartAreas.Add(chartArea3);
                //this.chart1.Dock = DockStyle.Fill;
                //定義存儲(chǔ)和顯示點(diǎn)的容器
                this.chart3.Series.Clear();
                Series series3 = new Series("S3");
                series3.ChartArea = "C3";
                this.chart3.Series.Add(series3);
                //設(shè)置圖表顯示樣式
                this.chart3.ChartAreas[0].AxisY.Minimum = 30000;
                this.chart3.ChartAreas[0].AxisY.Maximum = 50000;
                this.chart3.ChartAreas[0].AxisX.Minimum = 1;
                this.chart3.ChartAreas[0].AxisX.Maximum = 31;
                this.chart3.ChartAreas[0].AxisX.Interval = 1;
                this.chart3.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Silver;
                this.chart3.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Silver;
                //設(shè)置標(biāo)題
                this.chart3.Titles.Clear();
                this.chart3.Titles.Add("S03");
                this.chart3.Titles[0].Text = "動(dòng)態(tài)折線圖顯示";
                this.chart3.Titles[0].ForeColor = Color.RoyalBlue;
                this.chart3.Titles[0].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F); //標(biāo)題字體
                //設(shè)置圖表顯示樣式
                this.chart3.Series[0].Color = Color.Red;
                if (rb1.Checked)
                {
                    //this.chart3.Titles[0].Text = string.Format("動(dòng)態(tài) {0} 顯示", rb1.Text);
                    this.chart3.Titles[0].Text = string.Format("3號(hào)監(jiān)測(cè)點(diǎn)");
                    this.chart3.Series[0].ChartType = SeriesChartType.Line;
                }
                if (rb2.Checked)
                {
                    this.chart3.Titles[0].Text = string.Format("動(dòng)態(tài) {0} 顯示", rb1.Text);
                    this.chart3.Series[0].ChartType = SeriesChartType.Spline;
                }
                this.chart3.Series[0].Points.Clear();
                //DBEngine.ConnectDB("orcl", "dt", "6312");
                dataQueue3.Clear();//清空隊(duì)列中所有數(shù)據(jù)
            }
            catch (Exception ex)
            {

            }
        }
        private void InitChart4()
        {
            try
            {
                //定義圖表區(qū)域
                this.chart4.ChartAreas.Clear();
                ChartArea chartArea4 = new ChartArea("C4");
                this.chart4.ChartAreas.Add(chartArea4);
                //this.chart1.Dock = DockStyle.Fill;
                //定義存儲(chǔ)和顯示點(diǎn)的容器
                this.chart4.Series.Clear();
                Series series4 = new Series("S4");
                series4.ChartArea = "C4";
                this.chart4.Series.Add(series4);
                //設(shè)置圖表顯示樣式
                this.chart4.ChartAreas[0].AxisY.Minimum = 30000;
                this.chart4.ChartAreas[0].AxisY.Maximum = 50000;
                this.chart4.ChartAreas[0].AxisX.Minimum = 1;
                this.chart4.ChartAreas[0].AxisX.Maximum = 31;
                this.chart4.ChartAreas[0].AxisX.Interval = 1;
                this.chart4.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Silver;
                this.chart4.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Silver;
                //設(shè)置標(biāo)題
                this.chart4.Titles.Clear();
                this.chart4.Titles.Add("S04");
                this.chart4.Titles[0].Text = "動(dòng)態(tài)折線圖顯示";
                this.chart4.Titles[0].ForeColor = Color.RoyalBlue;
                this.chart4.Titles[0].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F); //標(biāo)題字體
                //設(shè)置圖表顯示樣式
                this.chart4.Series[0].Color = Color.Red;
                if (rb1.Checked)
                {
                    //this.chart4.Titles[0].Text = string.Format("動(dòng)態(tài) {0} 顯示", rb1.Text);
                    this.chart4.Titles[0].Text = string.Format("4號(hào)監(jiān)測(cè)點(diǎn)");
                    this.chart4.Series[0].ChartType = SeriesChartType.Line;
                }
                if (rb2.Checked)
                {
                    this.chart4.Titles[0].Text = string.Format("動(dòng)態(tài) {0} 顯示", rb1.Text);
                    this.chart4.Series[0].ChartType = SeriesChartType.Spline;
                }
                this.chart4.Series[0].Points.Clear();
                //DBEngine.ConnectDB("orcl", "dt", "6312");
                dataQueue4.Clear();//清空隊(duì)列中所有數(shù)據(jù)
            }
            catch (Exception ex)
            {

            }
        }


        private void UpdateDate()
        {
            //1 2 3 4號(hào)點(diǎn)同時(shí)更新
            try
            {
                //獲取當(dāng)前時(shí)間的batch值,將batch+1的時(shí)間值提取顯示。
                string selectsql = string.Format("select * from stressinfo where operatetime=to_date('{0}','yyyy-mm-dd')", dtp1.Value.ToShortDateString());
                DataTable dtDate = new DataTable();
                dtDate = DBEngine.GetDataTableBySql(selectsql);
                if (dtDate.Rows.Count > 0) //4條
                {
                    string[] getmonthNow = dtp1.Value.ToShortDateString().Split('/'); //有的電腦是'-'
                    monthNow = getmonthNow[1];
                    int currentBatch = DBEngine.ObjToInt(dtDate.Rows[0]["batchnum"]);
                    //int currentNode = DBEngine.ObjToInt(dtDate.Rows[0]["NODE"]); //當(dāng)前節(jié)點(diǎn)和當(dāng)前批次確定唯一記錄
                    currentBatch++;
                    //獲取下一個(gè)顯示的時(shí)間值以及應(yīng)力值
                    string nextsql1 = string.Format("select * from stressinfo where batchnum='{0}' and node=1", currentBatch);
                    DataTable dtNext1 = new DataTable();

                    dtNext1 = DBEngine.GetDataTableBySql(nextsql1);//取得了下一個(gè)批次的所有應(yīng)力監(jiān)測(cè)點(diǎn)數(shù)據(jù)。
                    if (dtNext1.Rows.Count > 0)
                    {
                        stress1 = DBEngine.ObjToInt(dtNext1.Rows[0]["CURRENTSTRESS"]);
                        dtp1.Value = DBEngine.ObjToDateTime(dtNext1.Rows[0]["OPERATETIME"]); //日期顯示(之后應(yīng)該還有各點(diǎn)應(yīng)力的提?。?
                        currentTime = dtp1.Value.ToShortDateString();
                        string[] datetime = currentTime.Split('/');
                        monthNext = datetime[1];
                        if (monthNow != monthNext)
                            isRefresh = true;
                    }
                    else
                    {
                        timer1.Stop();//數(shù)據(jù)到頭了,沒(méi)有數(shù)據(jù)了,batch+1找不到了
                        btnStop.Focus(); //停止鍵焦點(diǎn)顯示
                    }
                    ///第二個(gè)點(diǎn),不用更新數(shù)據(jù)
                    string nextsql2 = string.Format("select * from stressinfo where batchnum='{0}' and node=2", currentBatch);
                    DataTable dtNext2 = new DataTable();
                    dtNext2 = DBEngine.GetDataTableBySql(nextsql2);//取得了下一個(gè)批次的所有應(yīng)力監(jiān)測(cè)點(diǎn)數(shù)據(jù)。
                    if (dtNext2.Rows.Count > 0)
                    {
                        stress2 = DBEngine.ObjToInt(dtNext2.Rows[0]["CURRENTSTRESS"]);
                    }
                    else
                    {
                        timer1.Stop();//數(shù)據(jù)到頭了,沒(méi)有數(shù)據(jù)了,batch+1找不到了
                        btnStop.Focus(); //停止鍵焦點(diǎn)顯示
                    }
                    ///第三個(gè)點(diǎn),不用更新數(shù)據(jù)
                    string nextsql3 = string.Format("select * from stressinfo where batchnum='{0}' and node=3", currentBatch);
                    DataTable dtNext3 = new DataTable();
                    dtNext3 = DBEngine.GetDataTableBySql(nextsql3);//取得了下一個(gè)批次的所有應(yīng)力監(jiān)測(cè)點(diǎn)數(shù)據(jù)。
                    if (dtNext3.Rows.Count > 0)
                    {
                        stress3 = DBEngine.ObjToInt(dtNext3.Rows[0]["CURRENTSTRESS"]);
                    }
                    else
                    {
                        timer1.Stop();//數(shù)據(jù)到頭了,沒(méi)有數(shù)據(jù)了,batch+1找不到了
                        btnStop.Focus(); //停止鍵焦點(diǎn)顯示
                    }
                    ///第四個(gè)點(diǎn),不用更新數(shù)據(jù)
                    string nextsql4 = string.Format("select * from stressinfo where batchnum='{0}' and node=4", currentBatch);
                    DataTable dtNext4 = new DataTable();
                    dtNext4 = DBEngine.GetDataTableBySql(nextsql4);//取得了下一個(gè)批次的所有應(yīng)力監(jiān)測(cè)點(diǎn)數(shù)據(jù)。
                    if (dtNext4.Rows.Count > 0)
                    {
                        stress4 = DBEngine.ObjToInt(dtNext4.Rows[0]["CURRENTSTRESS"]);
                    }
                    else
                    {
                        timer1.Stop();//數(shù)據(jù)到頭了,沒(méi)有數(shù)據(jù)了,batch+1找不到了
                        btnStop.Focus(); //停止鍵焦點(diǎn)顯示
                    }

                }
            }
            catch
            {
            }

        }
}

因?yàn)樯婕暗揭恍I(yè)務(wù),有些代碼沒(méi)有粘,數(shù)據(jù)是和Oracle數(shù)據(jù)庫(kù)進(jìn)行交互的,類(lèi)文件名DBEngine.cs,大家自己做的時(shí)候別忘連接數(shù)據(jù)庫(kù),最終效果圖

C#如何實(shí)現(xiàn)chart控件動(dòng)態(tài)曲線繪制

關(guān)于“C#如何實(shí)現(xiàn)chart控件動(dòng)態(tài)曲線繪制”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI