Skip to content

Commit 3744297

Browse files
authored
[API] Added multi Y ImPlot#plotShaded (#84)
1 parent 751e1a9 commit 3744297

3 files changed

Lines changed: 44 additions & 1 deletion

File tree

‎example/src/main/java/ExampleImPlot.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class ExampleImPlot {
1212

1313
private static final Integer[] xs = {0, 1, 2, 3, 4, 5};
1414
private static final Integer[] ys = {0, 1, 2, 3, 4, 5};
15+
private static final Integer[] ys1 = {0, 0, 1, 2, 3, 4};
16+
private static final Integer[] ys2 = {1, 2, 3, 4, 5, 6};
1517

1618
static {
1719
ImPlot.createContext();
@@ -37,6 +39,7 @@ public static void show(ImBoolean showImPlotWindow) {
3739
ImGui.checkbox("Show ImPlot Built-In Demo", showDemo);
3840

3941
if (ImPlot.beginPlot("Example Plot")) {
42+
ImPlot.plotShaded("Shaded", xs, ys1, ys2);
4043
ImPlot.plotLine("Line", xs, ys);
4144
ImPlot.plotBars("Bars", xs, ys);
4245
ImPlot.endPlot();

‎imgui-binding/src/main/java/imgui/extension/implot/ImPlot.java‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,21 @@ private static <T extends Number> void convertArrays(final T[] xs, final T[] ys,
266266
}
267267
}
268268

269+
/**
270+
* 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
271+
*/
272+
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) {
273+
if (xs.length != ys1.length || xs.length != ys2.length) {
274+
throw new IllegalArgumentException("Invalid length for arrays");
275+
}
276+
277+
for (int i = 0; i < xs.length; i++) {
278+
xsOut[i] = xs[i].doubleValue();
279+
ysOut1[i] = ys1[i].doubleValue();
280+
ysOut2[i] = ys2[i].doubleValue();
281+
}
282+
}
283+
269284
/**
270285
* Plots a standard 2D line plot.
271286
* Due to conversion from T to double, extremely large 64-bit integer (long) values may lose data!
@@ -346,6 +361,14 @@ public static <T extends Number> void plotShaded(final String labelID, final T[]
346361
plotShaded(labelID, xs, ys, yRef, 0);
347362
}
348363

364+
/**
365+
* Plots a shaded (filled) region between two lines, or a line and a horizontal reference.
366+
* Due to conversion from T to double, extremely large 64-bit integer (long) values may lose data!
367+
*/
368+
public static <T extends Number> void plotShaded(final String labelID, final T[] xs, final T[] ys1, final T[] ys2) {
369+
plotShaded(labelID, xs, ys1, ys2, 0);
370+
}
371+
349372
/**
350373
* 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.
351374
* Due to conversion from T to double, extremely large 64-bit integer (long) values may lose data!
@@ -358,10 +381,27 @@ public static <T extends Number> void plotShaded(final String labelID, final T[]
358381
nPlotShaded(labelID, x, y, x.length, yRef, offset);
359382
}
360383

384+
/**
385+
* Plots a shaded (filled) region between two lines, or a line and a horizontal reference.
386+
* Due to conversion from T to double, extremely large 64-bit integer (long) values may lose data!
387+
*/
388+
public static <T extends Number> void plotShaded(final String labelID, final T[] xs, final T[] ys1, final T[] ys2, final int offset) {
389+
final double[] x = new double[xs.length];
390+
final double[] y1 = new double[ys1.length];
391+
final double[] y2 = new double[ys2.length];
392+
convertArrays(xs, ys1, ys2, x, y1, y2);
393+
394+
nPlotShaded(labelID, x, y1, y2, x.length, offset);
395+
}
396+
361397
private static native void nPlotShaded(String labelID, double[] xs, double[] ys, int size, int yRef, int offset); /*
362398
ImPlot::PlotShaded(labelID, xs, ys, size, yRef, offset);
363399
*/
364400

401+
private static native void nPlotShaded(String labelID, double[] xs, double[] ys1, double[] ys2, int size, int offset); /*
402+
ImPlot::PlotShaded(labelID, xs, ys1, ys2, size, offset);
403+
*/
404+
365405
/**
366406
* Plots a vertical bar graph.
367407
* Due to conversion from T to double, extremely large 64-bit integer (long) values may lose data!

0 commit comments

Comments
 (0)