无法仅绘制新值

时间:2018-02-08 13:42:55

标签: c# oxyplot

我正在尝试制作一个为每个timer.elapsed更新的情节(使用oxyplot的图表)。我使用的情节是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 - {{ 1}} old。并绘制它。我真的不知道如何告诉情节这样做。因为_sampleCount每次都得到更新。

1 个答案:

答案 0 :(得分:0)

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