Skip to content

Commit

Permalink
[API] Added multi Y ImPlot#plotShaded (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlf30 authored Oct 10, 2021
1 parent 751e1a9 commit 3744297
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
3 changes: 3 additions & 0 deletions example/src/main/java/ExampleImPlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -37,6 +39,7 @@ public static void show(ImBoolean showImPlotWindow) {
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();
Expand Down
40 changes: 40 additions & 0 deletions imgui-binding/src/main/java/imgui/extension/implot/ImPlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,21 @@ private static <T extends Number> void convertArrays(final T[] xs, final T[] ys,
}
}

/**
* 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!
Expand Down Expand Up @@ -346,6 +361,14 @@ public static <T extends Number> void plotShaded(final String labelID, final T[]
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!
Expand All @@ -358,10 +381,27 @@ public static <T extends Number> void plotShaded(final String labelID, final T[]
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!
Expand Down
2 changes: 1 addition & 1 deletion include/implot
Submodule implot updated 7 files
+12 −8 README.md
+46 −0 TODO.md
+1,527 −944 implot.cpp
+118 −24 implot.h
+1,542 −1,269 implot_demo.cpp
+175 −62 implot_internal.h
+235 −196 implot_items.cpp

0 comments on commit 3744297

Please sign in to comment.