2

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

2 Answers 2

1

Currently, the answer might be changed and it has been mentioned here

  • Change the Model property of the PlotView control
  • Call Invalidate on the PlotView control
  • Call Invalidate on the PlotModel

I suggest you to remove "PropertyChanged" and insert this into the updating method.

PlotModel.InvalidatePlot(true);
0

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");
    }
4
  • 1
    Thanks, I added rows mentioned above and also INotify by this code: public class MainViewModel : INotifyPropertyChanged and public event PropertyChangedEventHandler PropertyChanged; but plot is still not refreshed.
    – stanedav
    Oct 17, 2015 at 15:43
  • 1
    @stanedav plot is refreshing 110%. I have downloaded OxyPlot package and ran your sample. It seems you are not aware of how to use Binding mechanism properly. Uploaded a sample, check it dropbox.com/s/pe0ifbivt8pqhbh/OxyPlotGraphs.rar?dl=0
    – AnjumSKhan
    Oct 17, 2015 at 16:46
  • sorry my bad, you are my hero :)
    – stanedav
    Oct 17, 2015 at 16:56
  • @stanedav <oxy:LineSeries ItemsSource="{Binding Points}"/> is ok too, but it uses two-way binding which is not needed in your case.
    – AnjumSKhan
    Oct 17, 2015 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.