Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Step Signal Plot #1128

Merged
merged 4 commits into from Jul 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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