[API] Added multi Y ImPlot#plotShaded (#84)

This commit is contained in:
Trevor Flynn authored and GitHub committed 2021-10-10 08:03:35 +03:00
1 parent 751e1a95c3
commit 3744297452
3 files changed
+44 -1

No files matched your search

+3
View File
@@ -12,6 +12,8 @@ public class ExampleImPlot {
private static final Integer[] xs = {0, 1, 2, 3, 4, 5};
private static final Integer[] ys = {0, 1, 2, 3, 4, 5};
private static final Integer[] ys1 = {0, 0, 1, 2, 3, 4};
private static final Integer[] ys2 = {1, 2, 3, 4, 5, 6};
static {
ImPlot.createContext();
@@ -37,6 +39,7 @@ public class ExampleImPlot {
ImGui.checkbox("Show ImPlot Built-In Demo", showDemo);
if (ImPlot.beginPlot("Example Plot")) {
ImPlot.plotShaded("Shaded", xs, ys1, ys2);
ImPlot.plotLine("Line", xs, ys);
ImPlot.plotBars("Bars", xs, ys);
ImPlot.endPlot();
@@ -266,6 +266,21 @@ public final class ImPlot {
}
}
/**
* Make sure to initialize xsOut, ysOut1, and ysOut2 with length xs.length, ys1.length, and ys2.length before passing them, or data may be lost/errors may occur
*/
private static <T extends Number> void convertArrays(final T[] xs, final T[] ys1, final T[] ys2, final double[] xsOut, final double[] ysOut1, final double[] ysOut2) {
if (xs.length != ys1.length || xs.length != ys2.length) {
throw new IllegalArgumentException("Invalid length for arrays");
}
for (int i = 0; i < xs.length; i++) {
xsOut[i] = xs[i].doubleValue();
ysOut1[i] = ys1[i].doubleValue();
ysOut2[i] = ys2[i].doubleValue();
}
}
/**
* Plots a standard 2D line plot.
* Due to conversion from T to double, extremely large 64-bit integer (long) values may lose data!
@@ -346,6 +361,14 @@ public final class ImPlot {
plotShaded(labelID, xs, ys, yRef, 0);
}
/**
* Plots a shaded (filled) region between two lines, or a line and a horizontal reference.
* Due to conversion from T to double, extremely large 64-bit integer (long) values may lose data!
*/
public static <T extends Number> void plotShaded(final String labelID, final T[] xs, final T[] ys1, final T[] ys2) {
plotShaded(labelID, xs, ys1, ys2, 0);
}
/**
* Plots a shaded (filled) region between two lines, or a line and a horizontal reference. Set yRef (default 0) to +/-INFINITY for infinite fill extents.
* Due to conversion from T to double, extremely large 64-bit integer (long) values may lose data!
@@ -358,10 +381,27 @@ public final class ImPlot {
nPlotShaded(labelID, x, y, x.length, yRef, offset);
}
/**
* Plots a shaded (filled) region between two lines, or a line and a horizontal reference.
* Due to conversion from T to double, extremely large 64-bit integer (long) values may lose data!
*/
public static <T extends Number> void plotShaded(final String labelID, final T[] xs, final T[] ys1, final T[] ys2, final int offset) {
final double[] x = new double[xs.length];
final double[] y1 = new double[ys1.length];
final double[] y2 = new double[ys2.length];
convertArrays(xs, ys1, ys2, x, y1, y2);
nPlotShaded(labelID, x, y1, y2, x.length, offset);
}
private static native void nPlotShaded(String labelID, double[] xs, double[] ys, int size, int yRef, int offset); /*
ImPlot::PlotShaded(labelID, xs, ys, size, yRef, offset);
*/
private static native void nPlotShaded(String labelID, double[] xs, double[] ys1, double[] ys2, int size, int offset); /*
ImPlot::PlotShaded(labelID, xs, ys1, ys2, size, offset);
*/
/**
* Plots a vertical bar graph.
* Due to conversion from T to double, extremely large 64-bit integer (long) values may lose data!