Skip to content

Commit

Permalink
Merge pull request #1128 from EmanuelFeru/master
Browse files Browse the repository at this point in the history
Implemented Step Signal Plot
  • Loading branch information
swharden committed Jul 24, 2021
2 parents 3ead8c2 + 5c0884b commit d1ace02
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/ScottPlot/Plottable/SignalPlotBase.cs
Expand Up @@ -20,6 +20,7 @@ public abstract class SignalPlotBase<T> : IPlottable, IHasPointsGenericX<double,
public int XAxisIndex { get; set; } = 0;
public int YAxisIndex { get; set; } = 0;
public bool IsVisible { get; set; } = true;
public bool StepDisplay { get; set; } = false;
public float MarkerSize { get; set; } = 5;
public double OffsetX { get; set; } = 0;
public T OffsetY { get; set; } = default;
Expand Down Expand Up @@ -275,6 +276,9 @@ private void RenderLowDensity(PlotDimensions dims, Graphics gfx, int visibleInde
PointF[] pointsArray = linePoints.ToArray();
ValidatePoints(pointsArray);

if (StepDisplay)
pointsArray = GetStepPoints(pointsArray);

if (penLD.Width > 0)
gfx.DrawLines(penLD, pointsArray);

Expand Down Expand Up @@ -309,6 +313,25 @@ private void RenderLowDensity(PlotDimensions dims, Graphics gfx, int visibleInde
}
}

/// <summary>
/// Convert scatter plot points (connected by diagnal lines) to step plot points (connected by right angles)
/// by inserting an extra point between each of the original data points.
/// </summary>
private PointF[] GetStepPoints(PointF[] pointsArray)
{
PointF[] pointsStep = new PointF[pointsArray.Length * 2 - 1];

for (int i = 0; i < pointsArray.Length - 1; i++)
{
pointsStep[i * 2] = pointsArray[i];
pointsStep[i * 2 + 1] = new PointF(pointsArray[i + 1].X, pointsArray[i].Y);
}

pointsStep[pointsStep.Length - 1] = pointsArray[pointsArray.Length - 1];

return pointsStep;
}

private class IntervalMinMax
{
public float x;
Expand Down
19 changes: 19 additions & 0 deletions src/cookbook/ScottPlot.Cookbook/Recipes/Plottable/Signal.cs
Expand Up @@ -86,6 +86,25 @@ public void ExecuteRecipe(Plot plt)
}
}

public class CustomLineStep : IRecipe
{
public string Category => "Plottable: Signal Plot";
public string ID => "signal_step";
public string Title => "Step Display";
public string Description =>
"Signal plots can be styled as step plots where points " +
"are connected by right angles instead of straight lines.";

public void ExecuteRecipe(Plot plt)
{
double[] ys = DataGen.Sin(51);

var sig = plt.AddSignal(ys);
sig.StepDisplay = true;
sig.MarkerSize = 0;
}
}

public class RandomWalk_5millionPoints_Signal : IRecipe
{
public string Category => "Plottable: Signal Plot";
Expand Down

0 comments on commit d1ace02

Please sign in to comment.