Search code examples
c#wpfoxyplot

Oxyplot - WPF invalidate plotview


I am using Oxyplot for my WPF app. I want to capture some data and after that I want to show them in graph. I have this code in XAML:

xmlns:oxy="http://oxyplot.org/wpf"


 <Window.DataContext>
        <local:MainViewModel/>
  </Window.DataContext>
  <oxy:PlotView Title="{Binding Title}" Margin="241,0,0,179" Name="Plot1" Grid.Column="2">
       <oxy:PlotView.Series>
            <oxy:LineSeries ItemsSource="{Binding Points}"/>
        </oxy:PlotView.Series>
  </oxy:PlotView>

And Class:

public class MainViewModel
{
    /// <summary>
    /// 
    /// </summary>
    /// 
    public MainViewModel()
    {
        this.Title = "Sin";

        this.Points = new List<DataPoint>();

        for (int i = 0; i <= 800; i++)
        {
            double x = (Math.PI * i) / 400;
            double y = Math.Sin(x);
            DataPoint p = new DataPoint(x, y);
            Points.Add(p);
        }
        /*this.Points = new List<DataPoint>
         {
                              new DataPoint(0, 4),
                              new DataPoint(10, 13),
                              new DataPoint(20, 15),
                              new DataPoint(30, 16),
                              new DataPoint(40, 12),
                              new DataPoint(50, 12)
                          };*/
    }

    public string Title { get; private set; }

    public IList<DataPoint> Points { get; private set; }

    public void Second()
    {
        this.Points.Clear();
        this.Title = "Test";

        this.Points = new List<DataPoint>();

        for (int i = 0; i <= 800; i++)
        {
            double x = (Math.PI * i) / 400;
            double y = Math.Cos(x);
            DataPoint p = new DataPoint(x, 0.5);
            Points.Add(p);
        }
    }

}

Thing is, that I want after clicking button show plot from "Second()". It is performed by calling method Second() and after Plot1.InvalidatePlot(true), but it does nothing. Where am I doing mistake, please help? Thanks


Solution

  • Change to <oxy:LineSeries ItemsSource="{Binding Points, Mode=OneWay}"/>

    This will ensure changes to Points is propagated to your chart control. And secondly, implement INotifyPropertyChanged in your ViewModel class. Eg;

    IList<DataPoint> _points;
    public IList<DataPoint> Points { get{return _points;}; private set{ _points = value; OnPropertyChanged("Points");} }
    
            private void OnPropertyChanged(string p)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(p));
            }
    
    public void Second()
        {
            this.Points.Clear();
            this.Title = "Test";
    
            this.Points = new List<DataPoint>();
    
            for (int i = 0; i <= 800; i++)
            {
                double x = (Math.PI * i) / 400;
                double y = Math.Cos(x);
                DataPoint p = new DataPoint(x, 0.5);
                Points.Add(p);
            }
            /* suggested change */
            OnPropertyChanged("Points");
        }