[英].NET Real Time Data Chart
我想制作一個實時數據圖表,顯示在asp.net或winForms中的最后10分鍾。
我添加了該系列,但無法添加數據點。 我搜索了很多,但失敗了。
順便說一下,我使用基礎設施。
下面的代碼生成隨機數,並在圖表中顯示該數字。 它有效,但我能看到的只是最后一個數字。 不會持續10分鍾。
private void Form1_Load(object sender, EventArgs e)
{
Timer timer = new Timer();
timer.Interval = (1 * 1000); // 1 secs
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
private void timer_Tick(object sender, EventArgs e)
{
Random r = new Random();
int rnd=r.Next(1, 150);
DataTable dt = new DataTable();
dt.Columns.Add("Value", typeof(int));
dt.Columns.Add("Date", typeof(DateTime));
dt.Rows.Add(rnd, DateTime.ParseExact(DateTime.Now.ToLongTimeString(), "HH:mm:ss", null));
NumericTimeSeries series = new NumericTimeSeries();
series.DataBind(dt, "Date", "Value");
NumericTimeDataPoint ndp1 = new NumericTimeDataPoint(DateTime.Now, rnd, "ilk", false);
NumericTimeDataPoint ndp2 = new NumericTimeDataPoint(DateTime.Now, 5.0, "iki", false);
series.Points.Add(ndp1);
series.Points.Add(ndp2);
ultraChart2.Data.SwapRowsAndColumns = true;
ultraChart2.DataSource = dt;
}
如果我正確理解您的問題,
在每個刻度上,您將替換數據而不是添加數據。
嘗試在Form1_Load處理程序中初始化一次序列,然后在每個刻度上向其添加新值,而無需創建新的DataTable,將其重新綁定到序列,依此類推。
為了清楚起見,
您的timer_Tick處理程序應僅包含4行代碼:
private void timer_Tick(object sender, EventArgs e)
{
NumericTimeDataPoint ndp1 = new NumericTimeDataPoint(DateTime.Now, rnd, "ilk", false);
NumericTimeDataPoint ndp2 = new NumericTimeDataPoint(DateTime.Now, 5.0, "iki", false);
_series.Points.Add(ndp1);
_series.Points.Add(ndp2);
}
將_series聲明為私有成員,從Form1_Load處理程序對其進行初始化,您應該一切順利。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.