C# formsPlot1.Plot.Add.Signal(data)的参数功能


在 ScottPlot 中,formsPlot1.Plot.Add.Signal(data) 是一个用于快速绘制信号数据(如时间序列、波形等)的高效方法。它通过优化渲染逻辑,能够处理大量数据点(通常超过 100 万点)而不会显著降低性能。以下是 Add.Signal(data) 的参数和功能详解:
________________________________________
1. Add.Signal(data) 的核心参数
以下是 Add.Signal(data) 的常用重载方法及其参数说明:
基本用法
csharp
复制
var sig = formsPlot1.Plot.Add.Signal(
    data: double[],      // Y 值数据数组
    sampleRate: double,  // 采样率(每秒数据点数)
    color: Color,        // 线条颜色(可选)
    label: string        // 图例标签(可选)
);
参数    类型    说明
data    double[]    Y 值数据数组(必填)。
sampleRate    double    采样率(每秒数据点数)。用于计算 X 轴坐标(默认值为 1)。
color    Color    线条颜色(可选,默认值为 Colors.Blue)。
label    string    图例标签(可选,默认值为空)。
________________________________________
高级用法
csharp
复制
var sig = formsPlot1.Plot.Add.Signal(
    data: double[],      // Y 值数据数组
    sampleRate: double,  // 采样率
    xOffset: double,     // X 轴偏移量
    yOffset: double,     // Y 轴偏移量
    color: Color,        // 线条颜色
    label: string        // 图例标签
);
参数    类型    说明
xOffset    double    X 轴偏移量(默认值为 0)。
yOffset    double    Y 轴偏移量(默认值为 0)。
________________________________________
2. 核心功能与使用场景
(1) 绘制信号数据
•    示例:绘制一个正弦波信号。
csharp
复制
double[] data = ScottPlot.DataGen.Sin(1000); // 生成 1000 个点的正弦波数据
var sig = formsPlot1.Plot.Add.Signal(data, sampleRate: 100); // 采样率 100 Hz
formsPlot1.Refresh(); // 刷新绘图
(2) 自定义颜色和标签
•    示例:绘制红色信号并添加图例。
csharp
复制
var sig = formsPlot1.Plot.Add.Signal(data, sampleRate: 100, color: Colors.Red, label: "Signal 1");
formsPlot1.Plot.Legend(); // 显示图例
formsPlot1.Refresh();
(3) 偏移信号
•    示例:在 X 轴和 Y 轴上偏移信号。
csharp
复制
var sig = formsPlot1.Plot.Add.Signal(data, sampleRate: 100, xOffset: 10, yOffset: 0.5);
formsPlot1.Refresh();
________________________________________
3. 完整代码示例
绘制正弦波信号
csharp
复制
using ScottPlot;
using System.Drawing;

// 初始化绘图
var plt = new Plot(600, 400);

// 生成数据
double[] data = ScottPlot.DataGen.Sin(1000); // 1000 个点的正弦波

// 添加信号
var sig = plt.Add.Signal(data, sampleRate: 100, color: Color.Red, label: "Sine Wave");

// 显示图例
plt.Legend();

// 保存或显示图像
plt.SaveFig("signal_plot.png");
________________________________________
4. 注意事项
1.    性能优化:
o    Add.Signal() 针对大数据集进行了优化,适合绘制超过 100 万点的数据。
o    如果数据量较小(如少于 1000 点),可以使用 Add.Scatter() 或 Add.Line()。
2.    采样率的作用:
o    sampleRate 用于计算 X 轴坐标。例如,sampleRate = 100 时,X 轴表示时间(秒),每个点间隔 0.01 秒。
3.    动态更新数据:
o    如果需要动态更新信号数据,可以直接修改 sig.Ys 并调用 formsPlot1.Refresh()。
o    示例:
csharp
复制
sig.Ys = newData; // 更新数据
formsPlot1.Refresh(); // 刷新绘图
4.    多信号叠加:
o    可以多次调用 Add.Signal() 绘制多个信号,并通过 label 参数区分图例。
________________________________________
5. 常见问题
Q1:如何修改信号线条的宽度?
csharp
复制
sig.LineWidth = 2; // 设置线条宽度
Q2:如何禁用线条,只显示标记?
csharp
复制
sig.LineWidth = 0; // 禁用线条
sig.MarkerSize = 5; // 设置标记大小
Q3:如何处理大数据集?
•    Add.Signal() 已经针对大数据集进行了优化。如果性能仍然不足,可以尝试以下方法:
o    降低采样率。
o    使用 Add.SignalXY() 绘制稀疏数据。
________________________________________
通过合理配置 Add.Signal(data) 的参数,可以在 C# 应用中高效绘制信号数据,并支持动态更新和自定义样式。如果需要进一步探讨具体场景或问题,请提供更多细节!

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值