Search code examples
c#chartsoxyplot

Axis ActualMaximum and Maximum don't match after zooming or padding (OxyPlot)


I have made an example project to try and fix this bug but no luck. I have made a simple WPF application with a Plot and a Button inside it. The button, when clicked, it sets the X Axis minimum to 100 and maximum to 500. It works when I open the app, but as soon as I pan/zoom the chart it stops working.

When debugging, you can see that the Maximum changes, but the ActualMaximum value stays the same. Same goes for the Minimum and ActualMinimum. Here is my PlotModel code and my MainWindow code. I apologize if I format anything badly, I'll try to fix it as soon as possible.

public class OxyPlotModel : INotifyPropertyChanged
    {

        private OxyPlot.PlotModel plotModel;
        public OxyPlot.PlotModel PlotModel
        {
            get
            {
                return plotModel;
            }
            set
            {
                plotModel = value;
                OnPropertyChanged("PlotModel");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }

public MainWindow()
        {
            InitializeComponent();

            oxyPlotModel = new OxyPlotModel();
            oxyPlotModel.PlotModel = plotModel;
            plotView1.DataContext = oxyPlotModel;

            plotModel.Axes.Clear();

            var yAxis = new OxyPlot.Axes.LinearAxis();
            var xAxis = new OxyPlot.Axes.LinearAxis();
            xAxis.Position = OxyPlot.Axes.AxisPosition.Bottom;

            plotModel.Axes.Add(yAxis);
            plotModel.Axes.Add(xAxis);

            plotModel.Axes[1].Minimum = 0;
            plotModel.Axes[1].Maximum = 700;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            plotModel.Axes[1].Minimum = 100;
            plotModel.Axes[1].Maximum = 500;
            Debug.Print(plotModel.Axes[1].MaximumPadding.ToString());
            plotModel.InvalidatePlot(true);
        }

Solution

  • I fixed it by using Axis.Zoom instead of Maximum and Minimum.