我有这个code:
private void timer_Tick(object sender, EventArgs e)
{
timer.Stop();
for (int i = 0; i < TOTAL_SENSORS; i++)
{
DateTime d = DateTime.Now;
devices[i].Value = float.Parse(serialPort.ReadLine());
if (chart1.Series[i].Points.Count > MAX_POINTS)
{
//see the most recent points
}
chart1.Series[i].Points.AddXY(d, devices[i].Value);
}
timer.Start();
}
我的code这部分是计时器的Tick事件在那里我画一个图,我需要更新它的每tick.I不断增加点,当点计数达到MAX_POINTS(10),它消除了第一点和添加了一个新的结尾。
This part of my code is the timer's tick event where i draw a chart and i need to update it every tick.I keep adding points and when the points count reaches MAX_POINTS(10) it removes the first point and adds a new one at the end.
的问题是,当它到达MAX_POINTS它开始除去点的端部和图形不自动滚动。所有点都被删除,并没有新的点得到补充。
The problem is when it reaches MAX_POINTS it starts removing points at the end and the graph doesn't autoscroll. All points get deleted and no new points get added.
请帮助我,说什么我需要更改图表工作,我说。
Please help me and say what I need to change the chart to work as I said.
编辑1:我使用Windows窗体
EDIT 1: I am using Windows Forms.
编辑2:AddXY和RemoveAt不是我的,他们都是从点集合
EDIT 2: AddXY and RemoveAt are not mine they are from the points collection.
编辑3:我也想知道如何拥有一个范围,并看到数据的最后一小时,或在过去的一周上个月
EDIT 3: I also want to know how to have a 'scope' and see the data for the last hour or for the last week or for the last month.
编辑4:我改变了我的问题了一下,我现在要缩放的图表,从最后一个小时/天,显示点
推荐答案
存储在单独的字典中的点和图表。然后,你可以查询字典,当你想要的最新点。
Store the points in a separate dictionary as well as the chart. Then you can just query the dictionary when you want the most recent points.
Dictionary<DateTime, float> points = new Dictionary<DateTime, float>();
然后直接您的来电后,加入这行 AddXY()
:
points.Add(d, devices[i].Value);
如果你想保持字典同步的图表,从词典中删除的第一个元素以及
and if you want to keep the dictionary in sync with the chart, remove the first element from the dictionary as well:
points.Remove(points.Keys[0]);
要查询的字典,你可以使用LINQ:取()文档的跳过()文档
IEnumerable<KeyValuePair<DateTime, float>> mostRecent = points.Skip(points.Count - 10).Take(10);
或者你可以得到一个特定的点(假设你想从一分钟前的点)
or you can get a specific point (lets say you want the point from one minute ago)
float value = points[DateTime.Now.AddMinutes(-1)];
或者你可以遍历所有的项目:
or you can loop over the items:
foreach(KeyValuePair<DateTime, float> point in points)
{
DateTime time = point.Key;
float value = point.Value;
}
相关推荐
最新文章