0

I'm Creating a program which creates a plot after btnCalculate_Click using oxyplot. What can I do so that whenever I change a textbox value and click on btnCalculate it can refresh the plot? I also have btnPrint and on click it should clear the plot?

public void btnCalculate_Click(object sender, EventArgs e)
{

    Pko = float.Parse(textBox5.Text);

    //Plotting Using Oxyplots
    OxyPlot.WindowsForms.PlotView pv = new PlotView();
    pv.Location = new Point(650, 0);
    pv.Size = new Size(900, 815);
    this.Controls.Add(pv);

    pv.Model = new PlotModel { Title = "Program" };
    pv.Model.InvalidatePlot(true);   

    //Pko line from surface to depth
    LineSeries Pkoline = new LineSeries();
    Pkoline.Color = OxyColors.Black;
    Pkoline.LineStyle = LineStyle.Solid;
    Pkoline.StrokeThickness = 1;
    Pkoline.Points.Add(new DataPoint(Pko, 0));
    Pkoline.Points.Add(new DataPoint(100, 200));
    pv.Model.Series.Add()
}


private void btnClear_Click(object sender, EventArgs e)
{

}
VinihRuiy
  • 3
  • 1
  • 3

1 Answers1

1

First of all you need to define variable for plot to call it in different event handlers. Clearing the plot is just about clearing series collection

private readonly PlotView _pv;


public Form1()
{
    InitializeComponent();
//moved initialization from btnCalculate_Click
    _pv = new PlotView();
    this.Controls.Add(_pv);
    _pv.Location = new Point(0, 0);
    _pv.Size = new Size(500, 500);
    _pv.Model = new PlotModel {Title = "Program"};
    _pv.Model.InvalidatePlot(true);
}

private void btnCalculate_Click(object sender, EventArgs e)
{
    // keep old code Except _pv initialization   

    _pv.Model.Series.Add(Pkoline);//typo in old code
}

private void clearBtn_Click(object sender, EventArgs e)
{
    _pv.Model.InvalidatePlot(true);
    _pv.Model.Series.Clear();        
}
Pribina
  • 750
  • 1
  • 7
  • 14
  • Thanks for your feedback. However, the plot is blank except for the title on clicking btnCalculate and after clicking ClearBtn I can only see plot axis. – VinihRuiy Jun 13 '19 at 14:14
  • Thanks Pribina. I've figured it out!! This code comes right after btnCalculate. _pv.Model.InvalidatePlot(true); _pv.Model.Series.Clear(); – VinihRuiy Jun 13 '19 at 14:24
  • `_pv.Model.InvalidatePlot(true);` will cause to refresh(redraw) plot, `_pv.Model.Series.Clear();` will clear data shown in plot (yes axis stays), please consider to accept it as solution if its solved your issue – Pribina Jun 13 '19 at 14:54
  • Yes it has solved my issue. Now everytime I change any text box value and click btnCalculate it will have to replot with the new parameters. – VinihRuiy Jun 13 '19 at 15:47