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

Bos*_*sak 5 .net c# collections charts datapoint

我有这个代码:

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();
    }
Run Code Online (Sandbox Code Playgroud)

我的代码的这一部分是计时器的刻度事件,我绘制图表,我需要每次滴答都更新它.我继续添加点数,当点数达到MAX_POINTS(10)时,它删除第一个点在第一个点添加一个新点.结束.

问题是,当它达到MAX_POINTS时,它开始在结束时删除点,图表不会自动滚动.所有积分都会被删除,并且不会添加新积分.

请帮助我说出我需要改变图表的工作方式,正如我所说的那样.

编辑1:我正在使用Windows窗体.

编辑2:AddXY和RemoveAt不是我的,他们来自积分集合.

编辑3:我也想知道如何拥有"范围"并查看过去一小时或上周或上个月的数据.

编辑4:我稍微改变了我的问题,我现在想缩放图表以显示最后一小时/天的点数

Kyl*_*man 9

将点存储在单独的字典和图表中.然后,您可以在需要最新点时查询字典.

Dictionary<DateTime, float> points = new Dictionary<DateTime, float>();
Run Code Online (Sandbox Code Playgroud)

然后在致电后直接添加此行AddXY():

points.Add(d, devices[i].Value);
Run Code Online (Sandbox Code Playgroud)

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

points.Remove(points.Keys[0]);
Run Code Online (Sandbox Code Playgroud)

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

IEnumerable<KeyValuePair<DateTime, float>> mostRecent = points.Skip(points.Count - 10).Take(10);
Run Code Online (Sandbox Code Playgroud)

或者你可以得到一个具体点(假设你想从一分钟前得到点)

float value = points[DateTime.Now.AddMinutes(-1)];
Run Code Online (Sandbox Code Playgroud)

或者你可以遍历项目:

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


小智 5

你需要把这个:

chart1.ResetAutoValues();
Run Code Online (Sandbox Code Playgroud)

调整X和Y轴刻度