0

I want to use a LineSeries graph in WPF using the oxyploy library. I have been using but this does not support a model/PlotModel which I need to use the zoom functionality. What is the best way to implement the zoom in oxyPlot?

  • OxyPlot should support zooming by default. You can either zoom by scrolling the mouse wheel or drag a zooming rectangle by keeping the middle mouse button pressed. Do you have the intention to change/override this standard behavior? – Döharrrck Aug 21 '20 at 13:04

1 Answers1

1

I have summarized a quick example how to change the default zooming behavior. This example code unbinds the mouse wheel and zooming using middle mouse button and binds rectangle zooming to the left mouse button instead. In addition there is an event handler to reset the axes on double-click. The corresponding XAML form contains just an oxyPlot:PlotView named "myPlot" and nothing else.

using OxyPlot;
using OxyPlot.Series;
using System;
using System.Collections.Generic;
using System.Windows;

namespace PlotTest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            PlotModel pm = new PlotModel();

            var s1 = new LineSeries();
            AddPoints(s1.Points, 10_000);
            pm.Series.Add(s1);

            // Unbind the default implementation
            myPlot.ActualController.UnbindMouseWheel();
            myPlot.ActualController.UnbindMouseDown(OxyMouseButton.Middle);

            // Bind your implementation
            myPlot.ActualController.BindMouseDown(OxyMouseButton.Left, PlotCommands.ZoomRectangle);

            // Could also be done using WPF command pattern
            myPlot.MouseDoubleClick += MyPlot_MouseDoubleClick;

            // Ok, this is not how you would do the binding
            // to the view in real life
            myPlot.Model = pm; 
        }

        private void MyPlot_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            myPlot.Model.ResetAllAxes();
            myPlot.InvalidatePlot(false);
        }

        private static void AddPoints(ICollection<DataPoint> points, int n)
        {
            for (int i = 0; i < n; i++)
            {
                double x = Math.PI * 10 * i / (n - 1);
                points.Add(new DataPoint(x * Math.Cos(x), x * Math.Sin(x)));
            }
        }
    }
}
Döharrrck
  • 687
  • 1
  • 4
  • 15
  • Thanks @Đøharrrck Got there in the end. I needed to add MyModel.InvalidatePlot(true); to the code to allow it to plot the graph with – Greg Harrison Aug 23 '20 at 19:53