繁体   English   中英

如何每5秒自动更新图表的X轴?

[英]How to auto update chart's X-axis every 5 sec?

我对使用图表编码非常陌生。但是我设法通过单击按钮来显示图表。 第二次单击将更改“ x轴”,因为我将其设置为时间。

有没有一种方法可以自动为“ X轴”每5个更新一次? 据我所知,需要计时器来设置间隔时间。

private void timer1_Tick(object sender, EventArgs e)
{
    timer1.Start();
    this.Time.Text = System.DateTime.Now.ToString("hh:mm:ss tt");
}

private void GetData_Click(object sender, EventArgs e)
{
    sqlite_con1.Open();
    try
    {
        sqlite_cmd = sqlite_con1.CreateCommand();
        sqlite_cmd.CommandText = "SELECT * FROM Temperature where id=12";
        sqlite_reader = sqlite_cmd.ExecuteReader();             
        while (sqlite_reader.Read()) 
        {
           this.chart1.Series["SAT"].Points.AddXY(Time.Text, sqlite_reader["Temp"]);      
        }
    }  
    catch (Exception ex)
    {   
        MessageBox.Show(ex.Message);   
    }
    sqlite_con1.Close();
 }

任何帮助深表感谢。 谢谢。

您必须设置计时器时间,并在特定时间将其属性和事件处理程序设置为计时器

Timer t = new Timer();
t.Interval = 5000; // for five second interval
timer1.Enabled = true;
timer1.Tick += new System.EventHandler(timer1_Tick);

然后必须在其中调用图表绑定方法

private void timer1_Tick(object sender, EventArgs e)
{
   timer1.Start();
   this.Time.Text = System.DateTime.Now.ToString("hh:mm:ss tt");
   //here --- which you are calling on GetData_Click event by making  
   //separate method and call it here also
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM