c#winform圖表控件使用示例


公司有個需求,需要做嵌入式開發,跟硬件通信,把數據實時展示到winform中,網上查了資料,先寫下個demo備用,到時候接入socket通信就完成了,具體效果如圖

 

實現的原理是把最開始的數據去掉,加入新的數據,接着不停的綁定曲線數據,就能達到曲線實時展示的效果了

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lb_shebei.Text = "等待設備連接";
            c1.ChartAreas[0].Axes[0].MajorGrid.Enabled = false;//X軸上網格
            c1.ChartAreas[0].Axes[1].MajorGrid.LineDashStyle = ChartDashStyle.Dash;   //y軸網格類型 短橫線
            c1.ChartAreas[0].Axes[1].MajorGrid.LineColor = Color.Gray;//Y軸網格線顏色
            c1.ChartAreas[0].Axes[0].MajorTickMark.Enabled = false;//  x軸上突出的小點
            c1.ChartAreas[0].AxisX.Enabled = AxisEnabled.False;//設置x軸不顯示
            c1.ChartAreas[0].Axes[1].MajorTickMark.Enabled = false;//y軸上突出的小點
            c1.ChartAreas[0].Axes[1].IsInterlaced = false;  //顯示交錯帶 
            //c1.ChartAreas[0].Axes[0].LabelStyle.Format = "#年";                      //設置X軸顯示樣式
            c1.Series[0].IsValueShownAsLabel = true;//曲線點是否顯示值
            c1.Legends[0].Docking = Docking.Bottom;//調整圖例位置
            c1.Legends[0].Alignment = StringAlignment.Center;//調整圖例位置
            c1.Series[0].ChartType = SeriesChartType.Spline;//圖表類型
            c1.Series[0].MarkerStyle = MarkerStyle.None;  //標記點類型
            c1.Series[0].XValueType = ChartValueType.Time;//x軸坐標類型
            c1.Series[0].Name = "應力監測";
            //c1.Series[0].IsValueShownAsLabel = true;//顯示數值
            
            Thread t1 = new Thread(MyLine);
            t1.IsBackground = true;
            t1.Start();
        }
        List<DateTime> xlist = new List<DateTime>();
        List<int> ylist = new List<int>();
        void BindData()
        {
            Random rd = new Random();
            DateTime dt = DateTime.Now;
            ylist.Add(rd.Next(-2, 5));
            xlist.Add(dt);
            if (xlist.Count > 50)
            {
                ylist.Remove(ylist[0]);
                xlist.Remove(xlist[0]);
            }
        }
        public void MyLine()
        {
            while (true)
            {
                BindData();
                BindZp(xlist, ylist);
                Thread.Sleep(1000);
            }
        }
        delegate void SetXCallback(List<DateTime> x, List<int> y);
        void BindZp(List<DateTime> x, List<int> y)
        {
            if (c1.InvokeRequired)
            {
                SetXCallback d = new SetXCallback(BindZp);
                this.Invoke(d, new object[] { x, y });
            }
            else
            {
                c1.Series[0].Points.DataBindXY(x, y);//綁定數據
            }
        }
        private void btn_out_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("正在數據傳輸,確定要退出嗎?", "系統提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                Application.Exit();
                this.Dispose();
                this.Close();
            }
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM