Help: Add point an action button.

Oystein Bjorke 4 years ago 0
This discussion was imported from CodePlex

1CodePlex wrote at 2013-10-19 20:35:

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.

everytimer wrote at 2013-10-19 23:34:

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);
        }

1CodePlex wrote at 2013-10-20 17:09:

But lineSeries1 is local variable and I can't edit it.

everytimer wrote at 2013-10-20 18:22:

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

1CodePlex wrote at 2013-10-20 19:12:

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".

everytimer wrote at 2013-10-20 20:37:

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.
CodePlex