效果图
控件获取方法
Nugget
ScottPlot.WinForms
参考代码
Form1.Designer.cs
namespace ScottplotDemo
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.formsPlot1 = new ScottPlot.FormsPlot();
this.cbZoomable = new System.Windows.Forms.CheckBox();
this.cbLockHorizontal = new System.Windows.Forms.CheckBox();
this.cbLockVertical = new System.Windows.Forms.CheckBox();
this.CrosshaircheckBox = new System.Windows.Forms.CheckBox();
this.PixelLabel = new System.Windows.Forms.Label();
this.XCoordinateLabel = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// formsPlot1
//
this.formsPlot1.Location = new System.Drawing.Point(5, 79);
this.formsPlot1.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2);
this.formsPlot1.Name = "formsPlot1";
this.formsPlot1.Size = new System.Drawing.Size(785, 428);
this.formsPlot1.TabIndex = 0;
this.formsPlot1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.formsPlot1_MouseMove);
//
// cbZoomable
//
this.cbZoomable.AutoSize = true;
this.cbZoomable.Location = new System.Drawing.Point(12, 30);
this.cbZoomable.Name = "cbZoomable";
this.cbZoomable.Size = new System.Drawing.Size(96, 16);
this.cbZoomable.TabIndex = 1;
this.cbZoomable.Text = "锁定鼠标缩放";
this.cbZoomable.UseVisualStyleBackColor = true;
this.cbZoomable.CheckedChanged += new System.EventHandler(this.cbZoomable_CheckedChanged);
//
// cbLockHorizontal
//
this.cbLockHorizontal.AutoSize = true;
this.cbLockHorizontal.Location = new System.Drawing.Point(119, 30);
this.cbLockHorizontal.Name = "cbLockHorizontal";
this.cbLockHorizontal.Size = new System.Drawing.Size(96, 16);
this.cbLockHorizontal.TabIndex = 1;
this.cbLockHorizontal.Text = "锁定水平缩放";
this.cbLockHorizontal.UseVisualStyleBackColor = true;
this.cbLockHorizontal.CheckedChanged += new System.EventHandler(this.cbLockHorizontal_CheckedChanged);
//
// cbLockVertical
//
this.cbLockVertical.AutoSize = true;
this.cbLockVertical.Location = new System.Drawing.Point(119, 52);
this.cbLockVertical.Name = "cbLockVertical";
this.cbLockVertical.Size = new System.Drawing.Size(96, 16);
this.cbLockVertical.TabIndex = 1;
this.cbLockVertical.Text = "锁定垂直缩放";
this.cbLockVertical.UseVisualStyleBackColor = true;
this.cbLockVertical.CheckedChanged += new System.EventHandler(this.cbLockVertical_CheckedChanged);
//
// CrosshaircheckBox
//
this.CrosshaircheckBox.AutoSize = true;
this.CrosshaircheckBox.Location = new System.Drawing.Point(248, 30);
this.CrosshaircheckBox.Name = "CrosshaircheckBox";
this.CrosshaircheckBox.Size = new System.Drawing.Size(72, 16);
this.CrosshaircheckBox.TabIndex = 1;
this.CrosshaircheckBox.Text = "十字光标";
this.CrosshaircheckBox.UseVisualStyleBackColor = true;
this.CrosshaircheckBox.CheckedChanged += new System.EventHandler(this.cbLockHorizontal_CheckedChanged);
//
// PixelLabel
//
this.PixelLabel.AutoSize = true;
this.PixelLabel.Location = new System.Drawing.Point(3, 527);
this.PixelLabel.Name = "PixelLabel";
this.PixelLabel.Size = new System.Drawing.Size(29, 12);
this.PixelLabel.TabIndex = 2;
this.PixelLabel.Text = "null";
//
// XCoordinateLabel
//
this.XCoordinateLabel.AutoSize = true;
this.XCoordinateLabel.Location = new System.Drawing.Point(246, 527);
this.XCoordinateLabel.Name = "XCoordinateLabel";
this.XCoordinateLabel.Size = new System.Drawing.Size(29, 12);
this.XCoordinateLabel.TabIndex = 2;
this.XCoordinateLabel.Text = "null";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 548);
this.Controls.Add(this.XCoordinateLabel);
this.Controls.Add(this.PixelLabel);
this.Controls.Add(this.cbLockVertical);
this.Controls.Add(this.CrosshaircheckBox);
this.Controls.Add(this.cbLockHorizontal);
this.Controls.Add(this.cbZoomable);
this.Controls.Add(this.formsPlot1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private ScottPlot.FormsPlot formsPlot1;
private System.Windows.Forms.CheckBox cbZoomable;
private System.Windows.Forms.CheckBox cbLockHorizontal;
private System.Windows.Forms.CheckBox cbLockVertical;
private System.Windows.Forms.CheckBox CrosshaircheckBox;
private System.Windows.Forms.Label PixelLabel;
private System.Windows.Forms.Label XCoordinateLabel;
}
}
Form1
using ScottPlot;
using ScottPlot.Plottable;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ScottplotDemo
{
public partial class Form1 : Form
{
private readonly Crosshair Crosshair;
public Form1()
{
InitializeComponent();
var plt = formsPlot1.Plot;
// sample data
//X轴数据
double[] xs = DataGen.Consecutive(5100);
//Y轴数据
double[] sin = DataGen.Sin(5100);
double[] cos = DataGen.Cos(5100);
// 2条曲线
plt.AddScatter(xs, sin);
plt.AddScatter(xs, cos);
// plot参数设置
plt.Title("标题");
plt.XLabel("X轴");
plt.YLabel("Y轴");
//添加十字光标线
Crosshair = this.formsPlot1.Plot.AddCrosshair(0.0, 0.0);
formsPlot1.Refresh();
}
private void cbZoomable_CheckedChanged(object sender, EventArgs e)
{
//鼠标右键拖拽缩放
formsPlot1.Configuration.RightClickDragZoom = !cbZoomable.Checked;
//鼠标管轮缩放
formsPlot1.Configuration.ScrollWheelZoom = !cbZoomable.Checked;
}
private void cbLockHorizontal_CheckedChanged(object sender, EventArgs e)
{
//锁定水平缩放
formsPlot1.Configuration.LockHorizontalAxis = cbLockHorizontal.Checked;
}
private void cbLockVertical_CheckedChanged(object sender, EventArgs e)
{
//锁定垂直缩放
formsPlot1.Configuration.LockVerticalAxis = cbLockVertical.Checked;
}
private void formsPlot1_MouseMove(object sender, MouseEventArgs e)
{
if (CrosshaircheckBox.Checked)
{
ValueTuple<double, double> mouseCoordinates = this.formsPlot1.GetMouseCoordinates();
double coordinateX = mouseCoordinates.Item1;
double coordinateY = mouseCoordinates.Item2;
PixelLabel.Text = $"{e.X.ToString("0.00000")},{e.Y.ToString("0.00000")}";
XCoordinateLabel.Text = $"{coordinateX.ToString("0.00000000")},{coordinateY.ToString("0.00000000")}";
Crosshair.X = coordinateX;
Crosshair.Y = coordinateY;
}
//this.XPixelLabel.Location = new System.Drawing.Point(e.X, e.Y);
//如果设置十字光标线为不显示数值就不显示了
if (!CrosshaircheckBox.Checked)
{
this.PixelLabel.Text = "";
this.XCoordinateLabel.Text = "";
}
this.formsPlot1.Refresh(false, false);
}
}
}
注意事项
ScottPlot开源库的依赖库,避免与原有项目中依赖库冲突
如:“System.Threading.dll”等都是.net常用库