如何查看每秒更新的图表中的最后 10 个数据点?

发布于 2024-11-29 23:39:13 字数 929 浏览 1 评论 0原文

我有这样的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星星的轨迹 2024-12-06 23:39:13

将点存储在单独的字典和图表中。然后当你想要最新的点时就可以查询字典。

Dictionary<DateTime, float> points = new Dictionary<DateTime, float>();

然后在调用 AddXY() 之后直接添加此行:

points.Add(d, devices[i].Value);

如果您想让字典与图表保持同步,也请从字典中删除第一个元素:

points.Remove(points.Keys[0]);

要查询字典,您可以可以使用linq: Take() 文档 Skip() Documentation

IEnumerable<KeyValuePair<DateTime, float>> mostRecent = points.Skip(points.Count - 10).Take(10);

或者您可以获得特定的点(假设您想要一分钟前的点) )

float value = points[DateTime.Now.AddMinutes(-1)];

或者您可以循环遍历这些项目:

foreach(KeyValuePair<DateTime, float> point in points)
{
    DateTime time = point.Key;
    float value = point.Value;
}

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

then add this line directly after your call to 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]);

To query the dictionary, you can use linq: Take() Documentation Skip() Documentation

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;
}
终难遇 2024-12-06 23:39:13

你需要把这个:

chart1.ResetAutoValues();

调整X和Y轴比例

You need to put this:

chart1.ResetAutoValues();

to adjust X and Y axis scale

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文