Help: Add point an action button.

Oct 19, 2013 at 7:35 PM
Hello,
It is possible add new point from the series?
E.g.
public Form1()
        {
            InitializeComponent();
            var plotModel1 = new PlotModel();
            var lineSeries1 = new LineSeries();
            lineSeries1.Points.Add(new DataPoint(1, 1));
            lineSeries1.Points.Add(new DataPoint(2, 2));
            lineSeries1.Points.Add(new DataPoint(3, 3));
            plotModel1.Series.Add(lineSeries1);
            Plot.Model = plotModel1;
        }

  private void button1_Click(object sender, EventArgs e)
        {
          something...
lineSeries1.Points.Add(new DataPoint(4, 4));

        }
Thank you.
Oct 19, 2013 at 10:34 PM
I don't quite understand your question, but yes. The only thing that you need to do after adding a new point to a LineSeries is to update the PlotModel:
  private void button1_Click(object sender, EventArgs e)
        {
          something...
lineSeries1.Points.Add(new DataPoint(4, 4));
plotModel1.RefreshPlot(true);
        }
Oct 20, 2013 at 4:09 PM
But lineSeries1 is local variable and I can't edit it.
Oct 20, 2013 at 5:22 PM
Edited Oct 20, 2013 at 5:23 PM
Oh, you have to define it in the class scope as a field or a property (it has to be a property if you want to bind it).
            var plotModel1 {get; set,};
            var lineSeries1 = new LineSeries();

public Form1()
        {
            InitializeComponent();
            plotModel1 = new PlotModel();
            lineSeries1.Points.Add(new DataPoint(1, 1));
            lineSeries1.Points.Add(new DataPoint(2, 2));
            lineSeries1.Points.Add(new DataPoint(3, 3));
            plotModel1.Series.Add(lineSeries1);
            //Plot.Model = plotModel1;  --> bind it
            DataContext = this;
        }

  private void button1_Click(object sender, EventArgs e)
        {
          something...
          lineSeries1.Points.Add(new DataPoint(4, 4));
          plotModel1.RefreshPlot(true);
        }
EDIT: I've just realized that you're using WinForms, so ignore the binding part.
Good luck
Oct 20, 2013 at 6:12 PM
When I want to define property in the class scope, I get Error: "The contextual keyword 'var' may only appear within a local variable declaration".
Oct 20, 2013 at 7:37 PM
Then don't use var, use the proper type name, in this case is PlotModel/LineSeries. This kind of questions are not related to OxyPlot, please learn some basics before.