How to view the last 10 data points in a chart that updates every second?

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 a timer event in which I draw a chart and I need to update it every time. I keep adding points, and when the number of points 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 to delete points at the end, and the chart does not autoscroll. All points are deleted and no new points are added.

Please help me and tell me what I need to change the schedule to work, as I said.

EDIT 1: I am using Windows Forms.

EDIT 2: AddXY and RemoveAt are not mine from the point collection.

3: , "" .

4: , , /

+5
2

, . , .

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

AddXY():

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

, , :

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

, linq: Take() 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;
}
+8

:

chart1.ResetAutoValues();

X Y

+5

All Articles