Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Plots with shared axes in Oxyplot

Tags:

c#

oxyplot

I want to add multiple plots with shared x-axis using OXYPLOT library. The example code is as follows and it sets 4 different y-axis sharing the same x-axis. However i can only plot data on the 1st x&y axis but not the others. Any kind of suggestion would be appreciated.

    [Example("Untitled")]
    public static PlotModel Untitled()
    {
        var plotModel1 = new PlotModel();
        plotModel1.PlotMargins = new OxyThickness(40,20,40,30);
        var linearAxis1 = new LinearAxis();
        linearAxis1.EndPosition = 0.25;
        linearAxis1.Maximum = 1;
        linearAxis1.Minimum = -1;
        linearAxis1.Title = "C1";
        linearAxis1.Key= "C1";
        plotModel1.Axes.Add(linearAxis1);
        var linearAxis2 = new LinearAxis();
        linearAxis2.EndPosition = 0.5;
        linearAxis2.Maximum = 1;
        linearAxis2.Minimum = -1;
        linearAxis2.Position = AxisPosition.Right;
        linearAxis2.StartPosition = 0.25;
        linearAxis2.Title = "C2";
        linearAxis2.Key= "C2";
        plotModel1.Axes.Add(linearAxis2);
        var linearAxis3 = new LinearAxis();
        linearAxis3.EndPosition = 0.75;
        linearAxis3.Maximum = 1;
        linearAxis3.Minimum = -1;
        linearAxis3.StartPosition = 0.5;
        linearAxis3.Title = "C3";
        linearAxis3.Key= "C3";
        plotModel1.Axes.Add(linearAxis3);
        var linearAxis4 = new LinearAxis();
        linearAxis4.Maximum = 1;
        linearAxis4.Minimum = -1;
        linearAxis4.Position = AxisPosition.Right;
        linearAxis4.StartPosition = 0.75;
        linearAxis4.Title = "C4";
        linearAxis1.Key= "C4";
        plotModel1.Axes.Add(linearAxis4);
        var linearAxis5 = new LinearAxis();
        linearAxis5.Maximum = 100;
        linearAxis5.Minimum = 0;
        linearAxis5.Position = AxisPosition.Bottom;
        linearAxis5.Title = "s";
        linearAxis5.Key= "s";
        plotModel1.Axes.Add(linearAxis5);
        return plotModel1;
    }
like image 517
Muzab Avatar asked Mar 03 '14 17:03

Muzab


2 Answers

Assign the XAxisKey and YAxisKey propertiy to your serises.

PlotModel pm = new PlotModel();

OxyPlot.Series.FunctionSeries s1 = new FunctionSeries(Math.Sin, -10, 10, 0.1, "sin(x)");
s1.YAxisKey = "axesY2";
s1.XAxisKey = "axesX2";
pm.Series.Add(s1);

In your case, the key is "C1", "C2", and "C3", etc.

like image 76
user4290818 Avatar answered Nov 15 '22 08:11

user4290818


As said before, you have to use the YAxisKey and XAxisKey attributes combined with the StartPosition and EndPosition. The position is in percentage (from 0 to 1) so for example, if you want to divide equally the Y axes of graph, you can try code like this:

float percentage = 1f / NumberOfGraphs;
for (int i = 1; i <= NumberOfGraphs; i++) {
    ...
    LinearAxis yAxes = new LinearAxis();
    yAxes.Position = OxyPlot.Axes.AxisPosition.Left;
    yAxes.StartPosition = (i - 1) * percentage;
    yAxes.EndPosition = i * percentage;
    yAxes.Key = "Y" + i;
    ...
    LineSeries lineSerie = new LineSeries();
    lineSerie.YAxisKey = "Y" + i;
    ...
    yourPlotView.Model.Series.Add(lineSerie)
}
like image 42
NonnoAndre Avatar answered Nov 15 '22 08:11

NonnoAndre