不能只绘制新值

Can not plot only the new values

我正在尝试绘制一个图(使用 oxyplot 的图表),每个 timer.elapsed 都会更新一次。我使用的情节是 OxyPlot。计时器的间隔为 500 毫秒,数据收集在 for loop 中,如下所示:

for (uint i = _startIndex; i < (_startIndex + _sampleCount); i++)
{
    SampleCont[i] = adc_to_mv(appBuffersPinned[0].Target[i], inputRanges[SelRangeIndex]);
    double power = SampleIntervalr * Math.Pow(10, -9);
    sampleTimeCont[i] = (double)(i * power);
}

经过的时间及其更新剧情的方法如下:

private void OnTimedEvent(object sender, ElapsedEventArgs e)
{
    ss = "Timer elapsed" + Environment.NewLine;
    InsertText(ss);
    if (!sendit)
    {
       // if it's not a plot created plese initiate the plot first
       InitiateStreamPlot();
    }
    else
    {
        UpdatePlot();
    }
}

然后我们有 InitiateStreamPlotUpdatePlot 如下:

    private void InitiateStreamPlot()
    {
        myModel = new PlotModel { Title = "Voltage level" };
        ss = "New streaming plot is starting" + Environment.NewLine;

        series1 = new LineSeries
        {
            MarkerType = MarkerType.Circle,
            StrokeThickness = 1,
            MarkerSize = 1,
            Smooth = true,
            Title = "Voltage level",
            CanTrackerInterpolatePoints = false,

        };

        linearAxis1 = new LinearAxis { Position = AxisPosition.Bottom, Title = "Time in nanosec" };
        linearAxis2 = new LinearAxis { Position = AxisPosition.Left, Title = "Voltage" };

        myModel.Axes.Add(linearAxis1);
        myModel.Axes.Add(linearAxis2);

        for (int i = 0; i < SampleCont.Length; i++)
        {
            series1.Points.Add(new OxyPlot.DataPoint(sampleTimeCont[i], SampleCont[i]));
        }

        myModel.Series.Add(series1);
        plotView1.Model = myModel;
        sendit = true;
    }

    /// <summary>
    /// Updating the chart.Invoke is required for not blocking UI thread
    /// since we use a timer we do calculations and reading in a other thread
    /// than UI thread.and for getting back those values and show them on UI
    /// thread invoking is required.
    /// </summary>
    public void UpdatePlot()
    {

        if (plotView1.InvokeRequired)
        {
            plotView1.Invoke((MethodInvoker)UpdatePlot);
            //lbPoints.Invoke((MethodInvoker)UpdatePlot);

        }
        else
        {
            //while (series1.Points.Count > 0)
            //{

            //}
            if (series1.Points.Count > 0)
                series1.Points.Clear();

            if (myModel.Series.Count > 0)
                myModel.Series.Clear();


            string num = series1.Points.Count.ToString();
            lbPoints.Text = series1.Points.Count.ToString();
            //myModel.Series.Add(series1);

            for (int i = 0; i < SampleCont.Length; i++)
            {
                series1.Points.Add(new OxyPlot.DataPoint(sampleTimeCont[i], SampleCont[i]));
            }

            myModel.Series.Add(series1);

            //plotView1.Refresh();
            plotView1.InvalidatePlot(true);

        }
    }

问题是当计时器结束时,for 循环中收集了一些数据。当定时器再次启用时,循环仍在收集。这将导致 SampleCont 中的新值+旧值。所以我应该做的是,我需要第一次从 _startIndex 绘制到 _sampleCount。下一次 _sampleCount 上有一个新值(它是一个不断变化的变量,表示当前有多少样本)。所以我需要告诉程序这次 _startIndex 应该等于 _sampleCount 的旧值,而 _sampleCount 这次应该是 new _sampleCount - old _sampleCount。并绘制它。我真的不知道如何让情节做到这一点。因为 _sampleCount 每次都会更新。

只需使用通用列表来存储旧的 _sampleCount 值。