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

时间:2018-01-04 09:13:50

标签: c# charts timer

我是使用图表编码的新手。但我设法在点击按钮时显示图表。第二次点击将改变" x轴"因为我把它设定为时间。

有没有办法自动更新" X轴" ??据我所知,需要定时器来设置间隔时间。

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

非常感谢任何帮助。感谢。

1 个答案:

答案 0 :(得分:0)

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

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
}