Chart control X axis growing and growing and it lo

2019-02-20 05:53发布

问题:

I have application with real time Chart control that received date and display this on my control:

This is my control:

MyObject obj...

Series series = new Series();
series.Color = Color.Blue;
series.ChartType = SeriesChartType.Spline;
series.BorderWidth = 2;
chart1.Series.Add(series);
chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.White;
chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.White;
chart1.ChartAreas[0].AxisX.IsStartedFromZero = true;
chart1.ChartAreas[0].AxisX.IntervalOffsetType = DateTimeIntervalType.Number
;

Timer tick:

private void chartTimer_Tick(object sender, EventArgs e)
{
        series.Points.Add(wf.BitsPerSecond * 0.000001);
        chart1.ResetAutoValues();
}

my problem is that at the beginning this is the graph:

After few minutes the X axis is growing and growing and it looks like the graph stop to moving:

how can i make sure my graph will be look at the beginning ?

回答1:

You keep adding points to the chart, but don't ever remove them. So, when you call chart.ResetAutoValues(), it sets the minimum on the x-axis below the x value of your first point, and the maximum above (or equal to) the x value of your last point. The maximum keeps getting bigger, but the minimum never changes, so the graph looks compressed as time goes on. You can start to remove points once you reach some threshold, like this:

private void chartTimer_Tick(object sender, EventArgs e)
{
    if (series.Points.Count() > 1000) series.Points.RemoveAt(0);
    series.Points.Add(wf.BitsPerSecond * 0.000001);
    chart1.ResetAutoValues();
}


标签: c# charts