Render OxyPlot graph in Windows Form

Brandon Miller picture Brandon Miller · Jan 18, 2013 · Viewed 10.9k times · Source

I would like to dock an OxyPlot graph in my windows form and graph the function y = 2x - 7. I have downloaded OxyPlot and added the references to my project. I use the following code to add the plot to my form:

public partial class GraphForm : Form
{
    public OxyPlot.WindowsForms.Plot Plot;

    public Graph()
    {
        InitializeComponent();

        Plot = new OxyPlot.WindowsForms.Plot();
        Plot.Model = new PlotModel();
        Plot.Dock = DockStyle.Fill;
        this.Controls.Add(Plot);

        Plot.Model.PlotType = PlotType.XY;
        Plot.Model.Background = OxyColor.FromRgb(255, 255, 255);
        Plot.Model.TextColor = OxyColor.FromRgb(0, 0, 0);
    }
}

With this code I see the white background, the control has been created, but it's only a white background. I've looked around the members of the OxyPlot.Plot class but I couldn't find a way to plat my equation. How can I plot my equation in the graph?

Answer

sa_ddam213 picture sa_ddam213 · Jan 18, 2013

You need to add some data to display, you add this to the Models Series property.

Line (X,Y) graph example.

    public Graph()
    {
        InitializeComponent();

        Plot = new OxyPlot.WindowsForms.Plot();
        Plot.Model = new PlotModel();
        Plot.Dock = DockStyle.Fill;
        this.Controls.Add(Plot);

        Plot.Model.PlotType = PlotType.XY;
        Plot.Model.Background = OxyColor.FromRGB(255, 255, 255);
        Plot.Model.TextColor = OxyColor.FromRGB(0, 0, 0);

        // Create Line series
        var s1 = new LineSeries { Title = "LineSeries", StrokeThickness = 1 };
        s1.Points.Add(new DataPoint(2,7));
        s1.Points.Add(new DataPoint(7, 9));
        s1.Points.Add(new DataPoint(9, 4));

        // add Series and Axis to plot model
        Plot.Model.Series.Add(s1);
        Plot.Model.Axes.Add(new LinearAxis(AxisPosition.Bottom, 0.0, 10.0));
        Plot.Model.Axes.Add(new LinearAxis(AxisPosition.Left, 0.0, 10.0));

    }

This Example:

enter image description here