图表自动滚动(示波器效果) [英] Chart Auto Scroll (Oscilloscope Effect)

查看:151
本文介绍了图表自动滚动(示波器效果)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,每当我向图表添加点时,它都会压缩所有点.相反,我希望它自动滚动.

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

这里有两个.gifs来解释我的问题

Here are two .gifs to explain what my issue 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);
        }

    }

推荐答案

您可以选择以下选项:

  • 您可以从左侧删除一个点,然后再添加到右侧的每个点(一定数量后)

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

您可以移动x轴MinimumMaximum

您可以将图表设置为缩放和平移然后平移,即移动ScaleView

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

第一个选项很简单,它将使DataPoints的数量保持不变.这可能是好事,也可能是坏事,具体取决于您的需求.

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.

常用参考文献:

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

以下是选项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();

以下是选项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 );

这假设系列中已经有pointMax个点.

This assumes there are pointMax points already in the series.

所有示例都假设您已经有几点.选项1和3还假定x轴的MinimumMaximum均未设置,即它们均为double.NaN.

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:

请注意,选项2和3还假定您具有有效的x值.如果不这样做,则需要使x轴索引并使用点索引代替值.

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.

这篇关于图表自动滚动(示波器效果)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆