如何查看每秒更新的图表中的最后 10 个数据点?
我有这样的代码:
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();
}
我的代码的这一部分是计时器的刻度事件,我在其中绘制图表,并且需要在每个刻度处更新它。我不断添加点,当点数达到 MAX_POINTS(10) 时,它会删除第一个点 并在末尾添加一个新的。
问题是当它达到 MAX_POINTS 时,它开始删除末尾的点并且图表不会自动滚动。所有点都将被删除,并且不会添加新点。
请帮助我并说出我需要更改图表才能按照我所说的那样工作。
编辑 1:我正在使用 Windows 窗体。
编辑2:AddXY 和RemoveAt 不是我的,它们来自点集合。
编辑3:我还想知道如何拥有“范围”并查看最后一小时、上周或上个月的数据。
编辑4:我稍微改变了我的问题,我现在想缩放图表以显示最后一小时/天的点
I have this 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();
}
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.
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.
EDIT 1: I am using Windows Forms.
EDIT 2: AddXY and RemoveAt are not mine they are from the points collection.
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.
EDIT 4: I changed my question a bit, I now want to scale the chart to show the points from the last hour/day
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将点存储在单独的字典和图表中。然后当你想要最新的点时就可以查询字典。
然后在调用
AddXY()
之后直接添加此行:如果您想让字典与图表保持同步,也请从字典中删除第一个元素:
要查询字典,您可以可以使用linq: Take() 文档 Skip() Documentation
或者您可以获得特定的点(假设您想要一分钟前的点) )
或者您可以循环遍历这些项目:
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.
then add this line directly after your call to
AddXY()
:and if you want to keep the dictionary in sync with the chart, remove the first element from the dictionary as well:
To query the dictionary, you can use linq: Take() Documentation Skip() Documentation
or you can get a specific point (lets say you want the point from one minute ago)
or you can loop over the items:
你需要把这个:
调整X和Y轴比例
You need to put this:
to adjust X and Y axis scale