Skip to content

Commit

Permalink
added support for draggable axis line limits
Browse files Browse the repository at this point in the history
  • Loading branch information
swharden committed Jun 28, 2019
1 parent 21fd691 commit 6af45af
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
5 changes: 3 additions & 2 deletions demos/ScottPlotDraggableMarkers/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,21 @@ private void Form1_Load(object sender, EventArgs e)
scottPlotUC1.plt.AxisAuto();
scottPlotUC1.Render();
UpdateMessage();
BtnAddVline_Click(null, null);
}

private void BtnAddHline_Click(object sender, EventArgs e)
{
double position = rand.NextDouble() * 2 - 1;
scottPlotUC1.plt.PlotHLine(position, draggable: true);
scottPlotUC1.plt.PlotHLine(position, draggable: true, dragLimitLower: -1, dragLimitUpper: 1);
scottPlotUC1.Render();
UpdateMessage();
}

private void BtnAddVline_Click(object sender, EventArgs e)
{
double position = rand.NextDouble() * 50;
scottPlotUC1.plt.PlotVLine(position, draggable: true);
scottPlotUC1.plt.PlotVLine(position, draggable: true, dragLimitLower: 0, dragLimitUpper: 49);
scottPlotUC1.Render();
UpdateMessage();
}
Expand Down
5 changes: 5 additions & 0 deletions src/ScottPlot/MouseTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public void MouseMove(Point eLocation)
axLine.position = newPosition.X;
else
axLine.position = newPosition.Y;

if (axLine.position < axLine.dragLimitLower)
axLine.position = axLine.dragLimitLower;
else if (axLine.position > axLine.dragLimitUpper)
axLine.position = axLine.dragLimitUpper;
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/ScottPlot/Plot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,22 +240,22 @@ public void PlotBar(double[] xs, double[] ys, double? barWidth = null, double xO
/// <summary>
/// Plot a vertical line at the given X position
/// </summary>
public void PlotVLine(double x, Color? color = null, double lineWidth = 1, string label = null, bool draggable = false)
public void PlotVLine(double x, Color? color = null, double lineWidth = 1, string label = null, bool draggable = false, double dragLimitLower = double.NegativeInfinity, double dragLimitUpper = double.PositiveInfinity)
{
if (color == null)
color = settings.GetNextColor();
PlottableAxLine axLine = new PlottableAxLine(x, vertical: true, color: (Color)color, lineWidth: lineWidth, label: label, draggable: draggable);
PlottableAxLine axLine = new PlottableAxLine(x, vertical: true, color: (Color)color, lineWidth: lineWidth, label: label, draggable: draggable, dragLimitLower: dragLimitLower, dragLimitUpper: dragLimitUpper);
settings.plottables.Add(axLine);
}

/// <summary>
/// Plot a horizontal line at the given Y position
/// </summary>
public void PlotHLine(double x, Color? color = null, double lineWidth = 1, string label = null, bool draggable = false)
public void PlotHLine(double x, Color? color = null, double lineWidth = 1, string label = null, bool draggable = false, double dragLimitLower = double.NegativeInfinity, double dragLimitUpper = double.PositiveInfinity)
{
if (color == null)
color = settings.GetNextColor();
PlottableAxLine axLine = new PlottableAxLine(x, vertical: false, color: (Color)color, lineWidth: lineWidth, label: label, draggable: draggable);
PlottableAxLine axLine = new PlottableAxLine(x, vertical: false, color: (Color)color, lineWidth: lineWidth, label: label, draggable: draggable, dragLimitLower: dragLimitLower, dragLimitUpper: dragLimitUpper);
settings.plottables.Add(axLine);
}

Expand Down
9 changes: 7 additions & 2 deletions src/ScottPlot/PlottableAxLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ public class PlottableAxLine : Plottable
public string orientation;
public Pen pen;
public bool draggable;
public double dragLimitLower;
public double dragLimitUpper;

public PlottableAxLine(double position, bool vertical, Color color, double lineWidth, string label, bool draggable)
public PlottableAxLine(double position, bool vertical, Color color, double lineWidth, string label,
bool draggable, double dragLimitLower, double dragLimitUpper)
{
this.position = position;
this.vertical = vertical;
this.color = color;
this.label = label;
this.draggable = draggable;
this.color = color;
this.dragLimitLower = dragLimitLower;
this.dragLimitUpper = dragLimitUpper;
orientation = (vertical) ? "vertical" : "horizontal";
pen = new Pen(color, (float)lineWidth);
pointCount = 1;
Expand Down

0 comments on commit 6af45af

Please sign in to comment.