Skip to content

Commit

Permalink
Plot.PlotFillAboveBelow() #255
Browse files Browse the repository at this point in the history
  • Loading branch information
swharden committed Apr 11, 2020
1 parent bf7a575 commit 1c1b96a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions dev/changelog.md
Expand Up @@ -33,6 +33,7 @@ _ScottPlot uses [semantic](https://semver.org/) (major.minor.patch) versioning.
* The `enableZooming` argument in `WpfPlot.Configure()` and `FormsPlot.Configure()` has been replaced by two arguments `enableRightClickZoom` and `enableScrollWheelZoom` (#338) _Thanks Zach_
* Improved rendering of legend items for polygons and filled plots (#341) _Thanks @SeidChr_
* Improved Linux rendering of legend items which use thick lines: axis spans, fills, polygons, etc. (#340) _Thanks @SeidChr_
* Addded `Plot.PlotFillAboveBelow()` to create a shaded line plot with different colors above/below the baseline. (#255) _Thanks @ckovamees_

## ScottPlot 4.0.28
* `Ticks()` now has arguments for numericStringFormat (X and Y) to make it easy to customize formatting of tick labels (percentage, currency, scientific notation, etc.) using standard [numeric format strings](https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings). Example use is demonstrated in the cookbook. (#336) _Thanks @deiruch_
Expand Down
43 changes: 43 additions & 0 deletions src/ScottPlot/Plot.cs
Expand Up @@ -486,6 +486,49 @@ public void Clear(Predicate<Plottable> plottablesToClear)
return PlotPolygon(bothX, bothY, label, lineWidth, lineColor, fill, fillColor, fillAlpha);
}

public (PlottablePolygon, PlottablePolygon) PlotFillAboveBelow(
double[] xs,
double[] ys,
string labelAbove = null,
string labelBelow = null,
double lineWidth = 1,
Color? lineColor = null,
bool fill = true,
Color? fillColorAbove = null,
Color? fillColorBelow = null,
double fillAlpha = 1,
double baseline = 0
)
{
if (xs.Length != ys.Length)
throw new ArgumentException("xs and ys must all have the same length");

double[] xs2 = Tools.Pad(xs, cloneEdges: true);
double[] ys2 = Tools.Pad(ys, padWithLeft: baseline, padWithRight: baseline);

double[] ys2below = new double[ys2.Length];
double[] ys2above = new double[ys2.Length];
for (int i = 0; i < ys2.Length; i++)
{
if (ys2[i] < 0)
ys2below[i] = ys2[i];
else
ys2above[i] = ys2[i];
}

if (fillColorAbove is null)
fillColorAbove = Color.Green;
if (fillColorBelow is null)
fillColorBelow = Color.Red;
if (lineColor is null)
lineColor = Color.Black;

var polyBelow = PlotPolygon(xs2, ys2below, labelAbove, lineWidth, lineColor, fill, fillColorBelow, fillAlpha);
var polyAbove = PlotPolygon(xs2, ys2above, labelBelow, lineWidth, lineColor, fill, fillColorAbove, fillAlpha);

return (polyBelow, polyAbove);
}

public PlottableFunction PlotFunction(
Func<double, double?> function,
Color? color = null,
Expand Down
15 changes: 15 additions & 0 deletions tests/PlotTypes/Fill.cs
Expand Up @@ -84,5 +84,20 @@ public void Test_Fill_SeveralCurvesWithLegend()

TestTools.SaveFig(plt);
}

[Test]
public void Test_Fill_AboveBelow()
{
Random rand = new Random(0);
var ys = ScottPlot.DataGen.RandomWalk(rand, 1000, offset: -10);
var xs = ScottPlot.DataGen.Consecutive(ys.Length, spacing: 0.025);

var plt = new ScottPlot.Plot(400, 300);
plt.PlotFillAboveBelow(xs, ys, fillAlpha: .5, labelAbove: "above", labelBelow: "below");
plt.Legend(location: ScottPlot.legendLocation.lowerLeft);
plt.AxisAuto(0);

TestTools.SaveFig(plt);
}
}
}

0 comments on commit 1c1b96a

Please sign in to comment.