Oxyplot does not display plots in WPF view

by Gabriel   Last Updated August 14, 2019 06:26 AM

I'm working with OxyPlot in WPF. Trying to display PlotModels in a UI. Sometimes the plot is displayed in the UI and sometimes not.

This code is used to initialize the PlotModel by a BackgroundWorker:

ViewModel:

this.pm = setUpModel();
data2plot();

With the methods:

private PlotModel _pm;
public PlotModel pm
{
     get { return _pm; }
     set { _pm= value; RaisePropertyChanged("pm"); }
}


private PlotModel setUpModel()
{
      PlotModel pm = new PlotModel();
      pm.IsLegendVisible = false;
      var xAxis= new CategoryAxis();          
      pm.Axes.Add(xAxis);
      var valueAxis = new LinearAxis();            
      pm.Axes.Add(valueAxis);
      return pm ;
}

//tbl holds the data in the second column
private void data2plot(DataTable tbl)
{
       var series = new OxyPlot.Series.LineSeries();
       int i = 0;
       foreach (DataRow row in tbl.Rows)
       {
             double val = double.Parse(row[1].ToString());
             series.Points.Add(new DataPoint(i, val));
             i++;
       }

       this.pm.Series.Add(series);
}

Code used in View to initialize the model:

<Grid HorizontalAlignment="Left" Height="205" Margin="10,117,0,0" VerticalAlignment="Top" Width="308">
       <oxypl:PlotView x:Name="plot" Model="{Binding pm,UpdateSourceTrigger=PropertyChanged}"  Margin="10" Grid.Row="1">
       </oxypl:PlotView>
</Grid>

with the following references

xmlns:oxypl="http://oxyplot.org/wpf"
DataContext="{Binding Source={StaticResource Locator}, Path=ViewModel}"

The Binding works, so I guess there have to be a problem with my PlotModel.

I don't know if this is relevant, but the DataTable tbl is initialized by the Lazy<T> class (with isThreadSafe=true).

Tags : c# wpf plot oxyplot


Answers 2


When you add to your pm member, the value of pm is not being updated - it's the content of that member that is updated, i.e. the setter is never called, so the RaisePropertyChanged event is never emitted and the view isn't notified of any change. Suggest you build a local set of data and then overwrite pm with a complete set of data at the end, or emit a RaisePropertyChanged event when you're done.

The reason that it is working sometimes I would guess is that the first (and only) time the view reads the content back from the viewmodel you've already populated it, so you see something, but then no updates.

LordWilmore
LordWilmore
August 31, 2016 15:25 PM

You also have to call

pm.InvalidatePlot(true);

after adding to the series, in order to trigger a redraw.

b.holz
b.holz
August 14, 2019 06:24 AM

Related Questions


Updated June 28, 2017 15:26 PM

Updated August 03, 2015 15:11 PM

Updated October 19, 2018 15:26 PM

Updated February 21, 2019 14:26 PM

Updated March 26, 2019 13:26 PM