OneOfTheWeirdFishes
OneOfTheWeirdFishes

Reputation: 1

OxyPlot Refresh Issue

I am using a OxyPlot to Real time changing data.

I do this using a Dispatcher Timer in a WPF C# app.

            plotTimer.Interval = TimeSpan.FromMilliseconds(1000);
            plotTimer.Tick += PlotTimer_Tick;
            plotTimer.Start();

SensorPlotModel is a class of primarily the OxyPlot plotmodel with some properties to help keep track of which sensors it is for.

                                    <oxy:PlotView Grid.Row="2" Grid.RowSpan="5" Grid.Column="1" Grid.ColumnSpan="4" Model="{Binding graphPlotModel}" MinHeight="250" MinWidth="1200"/>

The plotModel is then bound in this way to the xaml, where graphPlotModel is a property of the dataContext.

The graph works well when the sensor value is changing. When the value is not changing, the graph seems to be updated with the values, however, the zoom is not changing meaning that the past values can be seen but not the new ones.

I suspect Oxyplot is optimising by not zooming to the new values.

Please let me know if there a problem in the code.

FYI, the graphs are initialised as so:

DateTimeAxis dateAxis = new DateTimeAxis();
            dateAxis.Position = AxisPosition.Bottom;
            dateAxis.StringFormat = "mm:ss";
            dateAxis.MajorGridlineStyle = LineStyle.Solid;
            dateAxis.MinorGridlineStyle = LineStyle.Dot;
            dateAxis.MinimumMajorStep = 1;
            dateAxis.IntervalType = DateTimeIntervalType.Minutes;
            //dateAxis.MaximumRange = 10;
            //dateAxis.IntervalLength = 5;
            plotModel.Axes.Add(dateAxis);

            LinearAxis valueAxis = new LinearAxis();
            valueAxis.Position = AxisPosition.Left;
            valueAxis.StartPosition = 0;
            valueAxis.MajorGridlineStyle = LineStyle.Solid;
            valueAxis.MinorGridlineStyle = LineStyle.Dot;
            valueAxis.Maximum = max;
            valueAxis.Minimum = min;


            plotModel.Axes.Add(valueAxis);

Upvotes: 0

Views: 390

Answers (1)

Anu Viswan
Anu Viswan

Reputation: 18153

I hope I understand your question here correctly. You could re-adjust the Axis.Minimum and Axis.Maximum as the new data comes in. For example,

var series = SensorPlotModel.Series.OfType<LineSeries>().First();

var dateTimeAxis = SensorPlotModel.Axes.OfType<DateTimeAxis>().First();

// set Initial axis Range 
if (!series.Points.Any())  
{
    dateTimeAxis.Minimum = DateTimeAxis.ToDouble(DateTime.Now);
    dateTimeAxis.Maximum = DateTimeAxis.ToDouble(DateTime.Now.AddSeconds(MaxSecondsToShow));
}

foreach (var newItem in newItems)
{
    if(newItem is SensorData sensorData)
    {
        series.Points.Add(new DataPoint(DateTimeAxis.ToDouble(sensorData.TimeStamp), sensorData.Data));
    }
}

// Reset the axis range when axis is out of range
if (DateTimeAxis.ToDouble(DateTime.Now) > dateTimeAxis.Maximum) 
{
    dateTimeAxis.Minimum = DateTimeAxis.ToDouble(DateTime.Now.AddSeconds(-1 * MaxSecondsToShow));
    dateTimeAxis.Maximum = DateTimeAxis.ToDouble(DateTime.Now);
    dateTimeAxis.Reset();
}

SensorPlotModel.InvalidatePlot(true);

NotifyOfPropertyChange(nameof(SensorPlotModel));

Upvotes: 0

Related Questions