Search code examples
c#oxyplot

Can not plot only the new values


I am trying to make a plot (chart using oxyplot) that updates for every timer.elapsed. The plot I'm using is OxyPlot. The interval for timer is 500 ms and data gets collected in a for loop as following:

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

Elapsed time and its methods to update the plot is according to following:

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

And then we have InitiateStreamPlotand UpdatePlot as following:

    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;
    }

And

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

        }
    }

The problem is that when the timer elapses there are some data collected in the for loop. and when the timer again is enable, the loop still collecting. That will result in new+old values in the SampleCont . so what I thing should be done instead is that I need to plot from _startIndex to _sampleCount first time. Next time there is a new value on _sampleCount ( it is a changing variable that says how many samples there are at the moment) . so I need to tell program that this time _startIndex should be equal to the old value of _sampleCount and _sampleCount should this time be new _sampleCount - old _sampleCount. and plot it. I don't really know how to tell the plot to do that. becouse _sampleCount get's update every time.


Solution

  • Just use a generic list to store the old _sampleCount values.