Search code examples
c#wpfaxisaxesoxyplot

Assigning two y-axes to two lineseries in oxyplot (C# WPF)


I've searched low and high for an answer to this question, but I only find older answers related to Windows Forms and not WPF. Nothing is mentioned in the documentation.

I have two lineseries that I plot using oxyplot. However, I am not able to assign each series to a separate axis (I need two due to different scale).

In the examples found it's stated that one should assign key="somename" to the axis definition and YAxisKey="somename" in the line series. However, I am given an error message that these properties does not exist. Any help is greatly appreciated. See xaml code attached below

<Wpf:Plot    
    DockPanel.Dock="Bottom"
    x:Name="OxyPlot1" 
    Title="{Binding Title}" Height="400" Margin="0,0,0,0">

    <Wpf:Plot.Axes>        
        <Wpf:LinearAxis
            x:Name="Psiaxis" Position="Left" Title="Pressure (PSI)"
            TickStyle="Inside" StartPosition="0"/>
        <Wpf:LinearAxis
            x:Name="Gpmaxis" Position="Right" Title="Flow In (GPM)"
            TickStyle="Inside" StartPosition="0"/>
        <Wpf:DateTimeAxis
            Position="Bottom" TickStyle="Inside" IntervalType="Seconds" 
            MinorGridlineStyle="Dash" MajorGridlineStyle="Solid"
            StringFormat="dd:MM:yyyy HH:mm:ss"/>
        </Wpf:Plot.Axes>

        <Wpf:LineSeries 
            x:Name="SelectedPointsPressure"
            Height="100" 
            Width="100" 
            Title="Pressure (psi)"
            ItemsSource="{Binding ToPlotPoints1}"/>

        <Wpf:LineSeries
            x:Name="RegressionPoints"
            Height="100"
            Width="100"
            LineStyle="Solid"
            Color="Blue"
            Title="Linear least squares fit"
            ItemsSource="{Binding ToPlotPoints2}"/>

        <Wpf:LineSeries 
            x:Name="SelectedPointsFlowin"
            Height="100" 
            Width="100" 
            LineStyle="Solid"
            Color="Green"
            Title="Flow In (gpm)"
            ItemsSource="{Binding ToPlotPoints3}"/>
    </Wpf:Plot>

Solution

  • You have to define key property for each Y axis.

    <oxyWpf:LinearAxis x:Name="Psiaxis" Position="Left" 
            Title="Pressure (PSI)" TickStyle="Inside" StartPosition="0"  Key="Psiaxis"   />
    

    And thanks to that you can set YAxisKey

    <oxyWpf:LineSeries
            x:Name="RegressionPoints"
            Height="100"
                YAxisKey="Psiaxis"
            Width="100"
            LineStyle="Solid"
            Color="Blue"
            Title="Linear least squares fit"
            ItemsSource="{Binding PointsSeries2}"/>