ScottPlot中的MouseTracker

date
Jan 3, 2022
slug
10031
status
Published
tags
C#
wpf
ScottPlot
summary
type
Post
Nuget安装ScottPlot.WPF,添加WpfPlot控件
notion image
代码:主要是一个几个事件的绑定:
using System.Windows;
using System.Windows.Input;
using ScottPlot;
using ScottPlot.Plottable;

namespace WpfApp2_ScottPlot2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Crosshair Crosshair1;

        public MainWindow()
        {

            InitializeComponent();
            WpfPlot1.Plot.AddSignal(DataGen.RandomWalk(null, 100));
            Crosshair1 = WpfPlot1.Plot.AddCrosshair(0, 0);
            WpfPlot1.Refresh();

        }
        private void button_Click(object sender, RoutedEventArgs e)
        {
        }

        private void WpfPlot1_MouseEnter(object sender, MouseEventArgs e)
        {
            Crosshair1.IsVisible = true;
        }

        private void WpfPlot1_MouseMove(object sender, MouseEventArgs e)
        {
            //int pixelX = (int)e.MouseDevice.GetPosition(WpfPlot1).X;
            //int pixelY = (int)e.MouseDevice.GetPosition(WpfPlot1).Y; //获取相对于WpfPlot1控件的Y坐标。

            (double coordinateX, double coordinateY) = WpfPlot1.GetMouseCoordinates();

            // XPixelLabel.Content = $"{pixelX:0.000}";
            // YPixelLabel.Content = $"{pixelY:0.000}";

            // XCoordinateLabel.Content = $"{wpfPlot1.Plot.GetCoordinateX(pixelX):0.00000000}";
            // YCoordinateLabel.Content = $"{wpfPlot1.Plot.GetCoordinateY(pixelY):0.00000000}";

            Crosshair1.X = coordinateX;
            Crosshair1.Y = coordinateY;

            WpfPlot1.Refresh();
        }
    }
}

© Wen Bo 2021 - 2022