C# formsPlot1.Plot.Add.Signal()用法


在C#中使用ScottPlot库的formsPlot1.Plot.Add.Signal()方法时,你可以通过以下步骤来绘制信号图。Add.Signal()方法用于绘制一维数组中的数据,通常用于显示连续信号。
基本用法
1.    安装ScottPlot库:
首先,确保你已经通过NuGet安装了ScottPlot库。你可以在NuGet包管理器中搜索ScottPlot.WinForms并安装。
2.    创建WinForms项目:
在Visual Studio中创建一个Windows Forms应用程序项目。
3.    添加ScottPlot控件:
在工具箱中找到FormsPlot控件,并将其拖放到你的窗体上。默认情况下,控件名称可能是formsPlot1。
4.    编写代码:
在窗体的代码文件中(如Form1.cs),编写代码来绘制信号图。
示例代码
csharp
复制
using System;
using System.Windows.Forms;
using ScottPlot;

namespace ScottPlotExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // 创建一些示例数据
            double[] data = ne

using ScottPlot; using ScottPlot.Plottables; using System; using System.Drawing; using System.Linq; using System.Windows.Forms; using System.Drawing; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } DataPoint StartingDragPosition = DataPoint.None; double StartingDragOffset = 0; Marker HighlightedPointMarker; bool ctl = false; AxisLine PlottableBeingDragged = null; Marker MyHighlightMarker; private AxisLine vLine; private AxisLine vLine1; bool vertical = false; // 当前选中的竖直线 private AxisLine selectedLine; private VerticalLine vlineFollow; private void Form1_Load(object sender, EventArgs e) { double[] data = Generate.RandomWalk(100); formsPlot1.Plot.Add.Signal(data); vLine = formsPlot1.Plot.Add.VerticalLine(1.0); vLine.LineWidth = 2; vLine.Color = ScottPlot.Colors.Green; vLine.IsDraggable = true; vLine1 = formsPlot1.Plot.Add.VerticalLine(8.0); vLine1.LineWidth = 2; vLine1.Color = ScottPlot.Colors.Blue; vLine1.IsDraggable = true; selectedLine = vLine; // 初始化 vlineFollow(一次即可) vlineFollow = formsPlot1.Plot.Add.VerticalLine(0); vlineFollow.Color = ScottPlot.Colors.Red; vlineFollow.LineWidth = 2; vlineFollow.IsVisible = false; formsPlot1.Refresh(); HighlightedPointMarker = formsPlot1.Plot.Add.Marker(0, 0); HighlightedPointMarker.IsVisible = false; HighlightedPointMarker.Size = 15; HighlightedPointMarker.LineWidth = 2; HighlightedPointMarker.Shape = MarkerShape.OpenCircle; formsPlot1.MouseDown += FormsPlot1_MouseDown; formsPlot1.MouseUp += FormsPlot1_MouseUp; formsPlot1.MouseMove += FormsPlot1_MouseMove; MyHighlightMarker = formsPlot1.Plot.Add.Marker(0, 0); MyHighlightMarker.Shape = MarkerShape.OpenCircle; MyHighlightMarker.Size = 17; MyHighlightMarker.LineWidth = 2; } private void FormsPlot1_MouseDown(object sender, MouseEventArgs e) { var lineUnderMouse = GetLineUnderMouse(e.X, e.Y); if (lineUnderMouse != null) { PlottableBeingDragged = lineUnderMouse; formsPlot1.UserInputProcessor.Disable(); // disable panning while dragging vertical = true; } } private void FormsPlot1_MouseUp(object sender, MouseEventArgs e) { PlottableBeingDragged = null; formsPlot1.UserInputProcessor.Enable(); // enable panning again formsPlot1.Refresh(); vertical = false; } private void FormsPlot1_MouseMove(object sender, MouseEventArgs e) { // this rectangle is the area around the mouse in coordinate units CoordinateRect rect = formsPlot1.Plot.GetCoordinateRect(e.X, e.Y, radius: 10); if (PlottableBeingDragged is null) { // set cursor based on what's beneath the plottable var lineUnderMouse = GetLineUnderMouse(e.X, e.Y); if (lineUnderMouse is null) Cursor = Cursors.Default; else if (lineUnderMouse.IsDraggable && lineUnderMouse is VerticalLine) Cursor = Cursors.SizeWE; else if (lineUnderMouse.IsDraggable && lineUnderMouse is HorizontalLine) Cursor = Cursors.SizeNS; if (vertical == false) { (var signalUnderMouse, DataPoint dp) = GetSignalUnderMouse(formsPlot1.Plot, e.X, e.Y); Cursor = signalUnderMouse is null ? Cursors.Arrow : Cursors.SizeWE; HighlightedPointMarker.IsVisible = (signalUnderMouse != null); vlineFollow.IsVisible = (signalUnderMouse != null); if (signalUnderMouse != null) { HighlightedPointMarker.Location = dp.Coordinates; HighlightedPointMarker.Color = signalUnderMouse.Color; HighlightedPointMarker.LegendText = $"Index {dp.Index} at {dp.Coordinates}"; vlineFollow.X = dp.Coordinates.X; Text = $"Index {dp.Index} at {dp.Coordinates}"; formsPlot1.Refresh(); } } vlineFollow.IsVisible = false; } else { vlineFollow.IsVisible = false; PlottableBeingDragged = selectedLine; // 这里 selectedLine 是按钮选择的线 // update the position of the plottable being dragged if (PlottableBeingDragged is HorizontalLine hl) { hl.Y = rect.VerticalCenter; hl.Text = $"{hl.Y:0.00}"; } else if (PlottableBeingDragged is VerticalLine vl) { vl.X = rect.HorizontalCenter; vl.Text = $"{vl.X:0.00}"; } formsPlot1.Refresh(); } } /// <summary> /// Returns the SignalXY object and data point beneath the mouse, /// or null if nothing is beneath the mouse. /// </summary> private AxisLine GetLineUnderMouse(float x, float y) { CoordinateRect rect = formsPlot1.Plot.GetCoordinateRect(x, y, radius: 10); foreach (AxisLine axLine in formsPlot1.Plot.GetPlottables<AxisLine>().Reverse()) { if (axLine.IsUnderMouse(rect)) return axLine; } return null; } private static (Signal signal, DataPoint point) GetSignalUnderMouse(Plot plot, double x, double y) { Pixel mousePixel = new Pixel(x, y); Coordinates mouseLocation = plot.GetCoordinates(mousePixel); foreach (Signal signal in plot.GetPlottables<Signal>().Reverse()) { DataPoint nearest = signal.GetNearest(mouseLocation, plot.LastRender,5); if (nearest.IsReal) { return (signal, nearest); } } return (null, DataPoint.None); } private void button2_Click(object sender, EventArgs e) { selectedLine = vLine; } private void button3_Click(object sender, EventArgs e) { selectedLine = vLine1; } } } 看一下,这是我的整体代码,我现在就希望这个数据线上的点,当我鼠标放到上面的时候,能显示出xy坐标,
最新发布
10-11
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值