Created
December 12, 2023 15:23
-
-
Save alexandersiemert/ccea8a3d6ba03cfaa238c40f16833691 to your computer and use it in GitHub Desktop.
Dragging Span in ScottPlot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using ScottPlot; | |
using ScottPlot.Plottable; | |
using System.Windows.Forms; | |
namespace WinFormsApp | |
{ | |
public partial class Form1 : Form | |
{ | |
readonly HSpan MySpan; | |
bool IsDraggingBody; | |
bool IsDraggingLeftEdge; | |
bool IsDraggingRightEdge; | |
bool IsDragging => IsDraggingBody || IsDraggingLeftEdge || IsDraggingRightEdge; | |
double MouseDownX; | |
double OriginalSpanX1; | |
double OriginalSpanX2; | |
public Form1() | |
{ | |
InitializeComponent(); | |
formsPlot1.Plot.AddSignal(DataGen.Sin(51)); | |
formsPlot1.Plot.AddSignal(DataGen.Cos(51)); | |
MySpan = formsPlot1.Plot.AddHorizontalSpan(10, 20); | |
// dont use the built-in dragging system | |
MySpan.DragEnabled = false; | |
// we will create our own dragging actions | |
formsPlot1.MouseDown += FormsPlot1_MouseDown; | |
formsPlot1.MouseMove += FormsPlot1_MouseMove; | |
formsPlot1.MouseUp += FormsPlot1_MouseUp; | |
formsPlot1.Refresh(); | |
} | |
private void FormsPlot1_MouseMove(object sender, MouseEventArgs e) | |
{ | |
if (!IsDragging) | |
return; | |
(double currentX, _) = formsPlot1.GetMouseCoordinates(); | |
double deltaX = currentX - MouseDownX; | |
if (IsDraggingBody) | |
{ | |
MySpan.X1 = OriginalSpanX1 + deltaX; | |
MySpan.X2 = OriginalSpanX2 + deltaX; | |
} | |
else if (IsDraggingLeftEdge) | |
{ | |
MySpan.X1 = OriginalSpanX1 + deltaX; | |
MySpan.X2 = OriginalSpanX2 - deltaX; | |
} | |
else if (IsDraggingRightEdge) | |
{ | |
MySpan.X1 = OriginalSpanX1 - deltaX; | |
MySpan.X2 = OriginalSpanX2 + deltaX; | |
} | |
formsPlot1.Refresh(); | |
} | |
private void FormsPlot1_MouseDown(object sender, MouseEventArgs e) | |
{ | |
(MouseDownX, _) = formsPlot1.GetMouseCoordinates(); | |
OriginalSpanX1 = MySpan.X1; | |
OriginalSpanX2 = MySpan.X2; | |
float snapPixels = 5; | |
double snapUnits = formsPlot1.Plot.XAxis.Dims.UnitsPerPx * snapPixels; | |
double distanceFromLeftEdge = Math.Abs(MouseDownX - MySpan.X1); | |
double distanceFromRightEdge = Math.Abs(MouseDownX - MySpan.X2); | |
if (distanceFromLeftEdge < snapUnits) | |
{ | |
IsDraggingLeftEdge = true; | |
} | |
else if (distanceFromRightEdge < snapUnits) | |
{ | |
IsDraggingRightEdge = true; | |
} | |
else if (MouseDownX >= MySpan.X1 && MouseDownX <= MySpan.X2) | |
{ | |
IsDraggingBody = true; | |
} | |
if (IsDragging) | |
formsPlot1.Configuration.LeftClickDragPan = false; | |
} | |
private void FormsPlot1_MouseUp(object sender, MouseEventArgs e) | |
{ | |
IsDraggingLeftEdge = false; | |
IsDraggingRightEdge = false; | |
IsDraggingBody = false; | |
formsPlot1.Configuration.LeftClickDragPan = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment