Search code examples
androidxamarinoxyplot

How to know zoom level in Oxyplot -PlotView With DateTimeAxes


I am using oxyplot android/IOS with zoom enabled. I am also using PlotView and DateTimeAxes to display live data.

By default Live data majorstep is set to 1/60. When user zooms in I am setting MajorStep to 1/60/24. Everything is working fine till here.

When user zooms I am not able to determine:

  1. User is zooming in or zooming out.
  2. Which zoom level I am currently in.

Code:

 protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        this.RequestWindowFeature (WindowFeatures.NoTitle);

        plotView = new PlotView(this) {
            Model = myClass.MyModel
        };
        (plotView.Model.Axes[0] as DateTimeAxis).AxisChanged += HandleAxisChanged;
        this.AddContentView (
            plotView, 
            new ViewGroup.LayoutParams (
                ViewGroup.LayoutParams.MatchParent, 
                ViewGroup.LayoutParams.MatchParent));
        LoadGraph();
    }

Axis changed event function below

void  HandleAxisChanged(object sender, AxisChangedEventArgs e) {
        switch (e.ChangeType) 
        {
        case AxisChangeTypes.Zoom:
        ((OxyPlot.Axes.DateTimeAxis)sender).MajorStep = 1.0 / 60 / 24;
        break;
        }
}

Solution

  • I've only used the WPF version of Oxyplot so hopefully the Android/IOS is similar. As far as I've seen, it doesn't have a way to determine zoom in vs out. However you can access the current zoom coordinates with the "ActualMinimum" and "ActualMaximum" fields from your DateTimeAxis like so:

    double ZoomRange = 60; //Tweak to find the range you want the zoom to switch at
    
    if (e.ChangeType == AxisChangeTypes.Zoom)
    {
        var axis = sender as DateTimeAxis;
        if (axis != null)
        {
            var xAxisMin = axis.ActualMinimum;
            var xAxisMax = axis.ActualMaximum;
    
            var delta = xAxisMax - xAxisMin;
    
            if(delta < ZoomRange)
                axis.MajorStep = 1.0/60/24;
            else
                axis.MajorStep = 1.0/60;
        }
    }
    

    From there you may just have to do some math to track the previous zoom state and adjust your MajorStep value based on some hard coded zoom value.

    Hope that helps.