我想使用Timer使用数据库中的数据制作实时图表。 现在,在每个刻度上,我的代码都会创建新的图形,而不是仅添加数据。
我认为应该只添加新数据,而不要将其绑定到计时器中。但是怎么做呢?自从今天早上以来,我仍然对此感到困惑
这是我的代码
图表代码:
void getchartinfo()
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(@"Data Source=192.168.200.146\SQLEXPRESS;Initial Catalog=dc_plc_tmp_7;uid=sa;password=supervisor;connection timeout=1;"))
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT dc_date as Date,cnt_2 as Output from d_prod_counter where dc_date between '2018-09-14 06:30:00' and '2018-09-15 06:29:00'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
catch
{
con.Close();
}
}
DateTime[] x = new DateTime[dt.Rows.Count];
Int32[] y = new Int32[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
x[i] = Convert.ToDateTime(dt.Rows[i][0]);
y[i] = Convert.ToInt32(dt.Rows[i][1]);
}
EmployeeChartInfo.Series[0].Points.DataBindXY(x, y);
EmployeeChartInfo.Series[0].ChartType = SeriesChartType.SplineArea;
EmployeeChartInfo.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
//EmployeeChartInfo.Legends[0].Enabled = true;
EmployeeChartInfo.DataBind();
}
计时器代码:
protected void TimerChart_Tick(object sender, EventArgs e)
{
getchartinfo();
}
页面代码:
<asp:UpdatePanel ID="UpdatePanel17" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Chart ID="EmployeeChartInfo" runat="server" Width="614px" Height="532px">
<Series>
<asp:Series Name="Output" Legend="Legend1"></asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1"></asp:ChartArea>
</ChartAreas>
<Legends>
<asp:Legend Name="Legend1">
<Position Height="8.026756" Width="29.4314384" X="67.56856" Y="3" />
</asp:Legend>
</Legends>
</asp:Chart>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="TimerChart" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>