Search code examples
c#oxyplot

Refresh and Clear Oxyplot Model


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)
{

}

Solution

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