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();
            }
        }
    }

 

posted @ 2018-04-10 09:08  三卷天书  阅读(5770)  评论(0编辑  收藏  举报