Search code examples
c#wpfoxyplot

WPF OxyPlot Zooming issue


In order to have the same scale on both axes, X and Y, I used PlotType.Cartesian which ensures that:

_model = new PlotModel();
_model.PlotType = PlotType.Cartesian;

I also have possibility to zoom in and out charts.

In order to control zooming I need to set AbsoluteMinimum and AbsoluteMaximum on both axes and specify minimum and maximum range.

Issues I have: how to keep the same scale when zooming? Because axes are zooming independently and often one axis is getting out of sync with the other axis (when one reaches its limits and the other still can expand).

Also, how to set appropriate values for both axes, because if I set all minimums and maximums, I expected correpsonding values on the other axes to be set, if I use PlotType.Cartesian, but it does not happen - this is the reason the issue arises, because i can't set appropriate values for both axes.


Solution

  • The closest I could get is:

    • subscribe to Loaded event of PlotView (_model field in this case)

    • in that method get ActualHeight and ActualWidth of PlotView

    Having size of plot area, one can choose min and max of desired axe, then do all calculations required to keep scale the same on both axes. For example:

    double xMin = -500;
    double xMax = 800;
    double xRange = xMax - xMin;
    
    double yRange = xRange / ActualWidth * ActualHeight;
    double yMin = 58;
    double yMax = yMin + yRange;
    
    _model.Axes[0].Minimum = xMin;
    _model.Axes[0].AbsoluteMinimum = xMin;
    
    _model.Axes[0].Maximum = xMax;
    _model.Axes[0].AbsoluteMaximum = xMax;
    
    // Analogically, define limits of Y axe
    

    Also it is important to zoom both axes with the same zooming factor!

    This will guarantee equal scales on both axes and keeping aspect ratio through zooming.