Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
907 views
in Technique[技术] by (71.8m points)

c# - Chart Auto Scroll (Oscilloscope Effect)

My issue is that whenever I add a point to the chart, it compresses all the points. Instead, I want it to auto scroll.

Here are two .gifs to explain what my issue is

What I have now

enter image description here

What I want to achieve

enter image description here

The code I have right now is

    DateTime dt;

    private void Form1_Load(object sender, EventArgs e)
    {
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        dt = DateTime.Now;
        if (checkBox1.Checked)
        {
            chart1.Series["Light"].Points.AddXY(dt.ToShortTimeString(), 1);
        }
        else
        {
            chart1.Series["Light"].Points.AddXY(dt.ToShortTimeString(), 0);
        }

    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have a choice of options:

  • You can remove a point from the left for each point you add to the right (after a certain number)

  • You can shift the x-Axis Minimum and Maximum values

  • You can set the chart to zoom&pan and then pan, i.e. move the ScaleView

The first option is simple and will keep the number of DataPoints constant. This may be good or bad, depending on your needs.

The other two will keep the collection of points and only pan in the chart.

Common references:

ChartArea ca = chart.ChartAreas[0];
Series s = chart.Series[0];

Here is code for the 1st option:

s.Points.AddXY(..);
s.Points.RemoveAt(0);
ca.AxisX.Minimum = double.NaN;
ca.AxisX.Maximum = double.NaN;
ca.RecalculateAxesScale();

Here is code for option 2:

int ix = s.Points.AddXY(..);

ca.AxisX.Maximum  = s.Points[ix].XValue;
ca.AxisX.Minimum += s.Points[ix].XValue - s.Points[ix-1].XValue;
ca.RecalculateAxesScale();

Here is code for option 3:

int ix = s.Points.AddXY(..);
ca.AxisX.Minimum = double.NaN;
ca.AxisX.Maximum = double.NaN;
ca.RecalculateAxesScale();

ca.AxisX.ScaleView.Zoom(s.Points[ix-pointMax ].XValue, s.Points[ix].XValue );

This assumes there are pointMax points already in the series.

All examples assume you have already a few points. Options 1&3 also assume neither Minimum nor Maximum of the x-axis are set, i.e. they are double.NaN.

The last option will let you scroll around the data conveniently.

The 1st one keeps the data points count low but loses all but the last points.

Let's watch all options at work:

enter image description here

Do note that options 2&3 also assume that you have valid x-values. If you don't, you need to make the x-axis indexed and use the point index instead of the values.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...