Search code examples
c#plotoxyplot

Set position and dimentions of OXYPLOT C#


I am try to learn usage of oxyplot. When I show a plot on the form, the plot covers all the form area (shown below). I want to place it on spesific coordinates with dimentions.

How can I do that?

My code:

 private void Plot()
{

    PlotView pv = new PlotView();
    pv.Dock = DockStyle.Fill;
    this.Controls.Add(pv);

    LinearAxis XAxis = new LinearAxis { Position = OxyPlot.Axes.AxisPosition.Bottom };
    LinearAxis YAxis = new LinearAxis();
    PlotModel pm = new PlotModel();
    pm.Axes.Add(XAxis);
    pm.Axes.Add(YAxis);
    pm.Background = OxyColor.FromRgb(0, 0, 255);
    pv.Model = pm;
    pv.Model.Series.Add(GetFunction());
}

private FunctionSeries GetFunction()
    {
        FunctionSeries fs = new FunctionSeries();
        for(int x=-10;x<=10;x++)
            for(int y=-10;y<10;y++)
            {
                DataPoint data = new DataPoint(x, y);
                fs.Points.Add(data);
            }
        return fs;
    }

The Result:

enter image description here


Solution

  • You should create the PlotView on xaml, bind PlotView Model property to your PlotModel on code behind, and then, treat it as a normal Framework Element:

    <oxy:PlotView Model="{Binding pm}" Height="200" Width="200"
                  HorizontalAlignment="Right" Margin="20"/>