如何实现oxyPloy WPF中的Zoom?

问题描述

我想使用oxyploy库在WPF中使用Lineseries图。我一直在使用,但是它不支持需要使用缩放功能的model / PlotModel。在oxyPlot中实现缩放的最佳方法是什么?

解决方法

我总结了一个简单的示例,说明如何更改默认的缩放行为。此示例代码使用鼠标中键解除鼠标滚轮和缩放的绑定,而是将矩形缩放绑定到鼠标左键。此外,还有一个事件处理程序,可在双击时重置轴。相应的XAML表单仅包含一个名为“ myPlot”的oxyPlot:PlotView,而没有其他内容。

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)));
            }
        }
    }
}