From ecb8c5e1497c892c84f6b9322ff4bc77f1640d41 Mon Sep 17 00:00:00 2001 From: vimsh Date: Tue, 19 Jan 2016 23:49:25 -0500 Subject: [PATCH] Added ability to specify color array for each point - this will allow to have a gradient coloring from low to high points (see main function in GridPlot3D.java as other plots' samples just use random colors rather than gradient) --- src/main/java/org/math/plot/Plot2DPanel.java | 104 +++++++++++++ src/main/java/org/math/plot/Plot3DPanel.java | 90 +++++++++++ src/main/java/org/math/plot/PlotPanel.java | 2 + .../org/math/plot/canvas/Plot2DCanvas.java | 89 ++++++++++- .../org/math/plot/canvas/Plot3DCanvas.java | 76 ++++++++++ .../java/org/math/plot/plots/BarPlot.java | 42 +++++- .../java/org/math/plot/plots/BoxPlot2D.java | 16 +- .../java/org/math/plot/plots/BoxPlot3D.java | 36 ++++- .../java/org/math/plot/plots/CloudPlot2D.java | 41 ++++- .../java/org/math/plot/plots/CloudPlot3D.java | 46 +++++- .../org/math/plot/plots/DensityLayerPlot.java | 30 +++- .../plot/plots/GaussianDensityLayerPlot.java | 40 +++-- .../java/org/math/plot/plots/GridPlot3D.java | 141 ++++++++++++++++-- .../org/math/plot/plots/HistogramPlot2D.java | 38 ++++- .../org/math/plot/plots/HistogramPlot3D.java | 44 +++++- .../java/org/math/plot/plots/LayerPlot.java | 2 +- .../java/org/math/plot/plots/LinePlot.java | 82 +++++++--- src/main/java/org/math/plot/plots/Plot.java | 41 +++-- .../math/plot/plots/QuantileLayerPlot.java | 29 +++- .../java/org/math/plot/plots/ScatterPlot.java | 54 ++++++- .../org/math/plot/plots/StaircasePlot.java | 51 +++++-- .../org/math/plot/plots/VectorLayerPlot.java | 21 ++- 22 files changed, 1008 insertions(+), 107 deletions(-) diff --git a/src/main/java/org/math/plot/Plot2DPanel.java b/src/main/java/org/math/plot/Plot2DPanel.java index 4a9d9d3..f2257a0 100644 --- a/src/main/java/org/math/plot/Plot2DPanel.java +++ b/src/main/java/org/math/plot/Plot2DPanel.java @@ -74,14 +74,26 @@ public int addScatterPlot(String name, Color color, double[][] XY) { return ((Plot2DCanvas) plotCanvas).addScatterPlot(name, color, XY); } + public int addScatterPlot(String name, Color[] color, double[][] XY) { + return ((Plot2DCanvas) plotCanvas).addScatterPlot(name, color, XY); + } + public int addScatterPlot(String name, Color color, double[] Y) { return ((Plot2DCanvas) plotCanvas).addScatterPlot(name, color, Y); } + public int addScatterPlot(String name, Color[] color, double[] Y) { + return ((Plot2DCanvas) plotCanvas).addScatterPlot(name, color, Y); + } + public int addScatterPlot(String name, Color color, double[] X, double[] Y) { return ((Plot2DCanvas) plotCanvas).addScatterPlot(name, color, X,Y); } + public int addScatterPlot(String name, Color[] color, double[] X, double[] Y) { + return ((Plot2DCanvas) plotCanvas).addScatterPlot(name, color, X,Y); + } + public int addScatterPlot(String name, double[][] XY) { return addScatterPlot(name, getNewColor(), XY); } @@ -121,14 +133,26 @@ public int addLinePlot(String name, Color color, double[][] XY) { return ((Plot2DCanvas) plotCanvas).addLinePlot(name, color, XY); } + public int addLinePlot(String name, Color[] color, double[][] XY) { + return ((Plot2DCanvas) plotCanvas).addLinePlot(name, color, XY); + } + public int addLinePlot(String name, Color color, double[] Y) { return ((Plot2DCanvas) plotCanvas).addLinePlot(name, color, Y); } + public int addLinePlot(String name, Color[] color, double[] Y) { + return ((Plot2DCanvas) plotCanvas).addLinePlot(name, color, Y); + } + public int addLinePlot(String name, Color color, double[] X, double[] Y) { return ((Plot2DCanvas) plotCanvas).addLinePlot(name, color, X,Y); } + public int addLinePlot(String name, Color[] color, double[] X, double[] Y) { + return ((Plot2DCanvas) plotCanvas).addLinePlot(name, color, X,Y); + } + public int addLinePlot(String name, double[][] XY) { return addLinePlot(name, getNewColor(), XY); } @@ -168,14 +192,26 @@ public int addBarPlot(String name, Color color, double[][] XY) { return ((Plot2DCanvas) plotCanvas).addBarPlot(name, color, XY); } + public int addBarPlot(String name, Color[] color, double[][] XY) { + return ((Plot2DCanvas) plotCanvas).addBarPlot(name, color, XY); + } + public int addBarPlot(String name, Color color, double[] Y) { return ((Plot2DCanvas) plotCanvas).addBarPlot(name, color, Y); } + public int addBarPlot(String name, Color[] color, double[] Y) { + return ((Plot2DCanvas) plotCanvas).addBarPlot(name, color, Y); + } + public int addBarPlot(String name, Color color, double[] X, double[] Y) { return ((Plot2DCanvas) plotCanvas).addBarPlot(name, color, X,Y); } + public int addBarPlot(String name, Color[] color, double[] X, double[] Y) { + return ((Plot2DCanvas) plotCanvas).addBarPlot(name, color, X,Y); + } + public int addBarPlot(String name, double[][] XY) { return addBarPlot(name, getNewColor(), XY); } @@ -215,14 +251,26 @@ public int addStaircasePlot(String name, Color color, double[][] XY) { return ((Plot2DCanvas) plotCanvas).addStaircasePlot(name, color, XY); } + public int addStaircasePlot(String name, Color[] color, double[][] XY) { + return ((Plot2DCanvas) plotCanvas).addStaircasePlot(name, color, XY); + } + public int addStaircasePlot(String name, Color color, double[] Y) { return ((Plot2DCanvas) plotCanvas).addStaircasePlot(name, color, Y); } + public int addStaircasePlot(String name, Color[] color, double[] Y) { + return ((Plot2DCanvas) plotCanvas).addStaircasePlot(name, color, Y); + } + public int addStaircasePlot(String name, Color color, double[] X,double[] Y) { return ((Plot2DCanvas) plotCanvas).addStaircasePlot(name, color, X,Y); } + public int addStaircasePlot(String name, Color[] color, double[] X,double[] Y) { + return ((Plot2DCanvas) plotCanvas).addStaircasePlot(name, color, X,Y); + } + public int addStaircasePlot(String name, double[][] XY) { return addStaircasePlot(name, getNewColor(), XY); } @@ -256,6 +304,10 @@ public int addBoxPlot(String name, Color color, double[][] XY, double[][] dXdY) return ((Plot2DCanvas) plotCanvas).addBoxPlot(name, color, XY, dXdY); } + public int addBoxPlot(String name, Color[] color, double[][] XY, double[][] dXdY) { + return ((Plot2DCanvas) plotCanvas).addBoxPlot(name, color, XY, dXdY); + } + public int addBoxPlot(String name, double[][] XY, double[][] dXdY) { return addBoxPlot(name, getNewColor(), XY, dXdY); } @@ -279,6 +331,10 @@ public int addBoxPlot(String name, Color color, double[][] XYdXdY) { return ((Plot2DCanvas) plotCanvas).addBoxPlot(name, color, XYdXdY); } + public int addBoxPlot(String name, Color[] color, double[][] XYdXdY) { + return ((Plot2DCanvas) plotCanvas).addBoxPlot(name, color, XYdXdY); + } + public int addBoxPlot(String name, double[][] XYdXdY) { return addBoxPlot(name, getNewColor(), XYdXdY); } @@ -303,6 +359,10 @@ public int addHistogramPlot(String name, Color color, double[][] XY, double[] dX return ((Plot2DCanvas) plotCanvas).addHistogramPlot(name, color, XY, dX); } + public int addHistogramPlot(String name, Color[] color, double[][] XY, double[] dX) { + return ((Plot2DCanvas) plotCanvas).addHistogramPlot(name, color, XY, dX); + } + public int addHistogramPlot(String name, double[][] XY, double[] dX) { return addHistogramPlot(name, getNewColor(), XY, dX); } @@ -311,6 +371,10 @@ public int addHistogramPlot(String name, Color color, double[][] XYdX) { return ((Plot2DCanvas) plotCanvas).addHistogramPlot(name, color, XYdX); } + public int addHistogramPlot(String name, Color[] color, double[][] XYdX) { + return ((Plot2DCanvas) plotCanvas).addHistogramPlot(name, color, XYdX); + } + /** * Adds a histogram plot to the current plot panel. Each data point is as * vertical bar which width can be set. @@ -342,6 +406,10 @@ public int addHistogramPlot(String name, Color color, double[] sample, int n) { return ((Plot2DCanvas) plotCanvas).addHistogramPlot(name, color, sample, n); } + public int addHistogramPlot(String name, Color[] color, double[] sample, int n) { + return ((Plot2DCanvas) plotCanvas).addHistogramPlot(name, color, sample, n); + } + public int addHistogramPlot(String name, double[] X, int n) { return addHistogramPlot(name, getNewColor(), X, n); } @@ -360,6 +428,10 @@ public int addHistogramPlot(String name, Color color, double[] sample, double... return ((Plot2DCanvas) plotCanvas).addHistogramPlot(name, color, sample, bounds); } + public int addHistogramPlot(String name, Color[] color, double[] sample, double... bounds) { + return ((Plot2DCanvas) plotCanvas).addHistogramPlot(name, color, sample, bounds); + } + public int addHistogramPlot(String name, double[] X, double... bounds) { return addHistogramPlot(name, getNewColor(), X, bounds); } @@ -368,6 +440,10 @@ public int addHistogramPlot(String name, Color color, double[] X, double min, do return ((Plot2DCanvas) plotCanvas).addHistogramPlot(name, color, X, min, max, n); } + public int addHistogramPlot(String name, Color[] color, double[] X, double min, double max, int n) { + return ((Plot2DCanvas) plotCanvas).addHistogramPlot(name, color, X, min, max, n); + } + public int addHistogramPlot(String name, double[] X, double min, double max, int n) { return addHistogramPlot(name, getNewColor(), X, min, max, n); } @@ -376,6 +452,10 @@ public int addCloudPlot(String name, Color color, double[][] sampleXY, int nX, i return ((Plot2DCanvas) plotCanvas).addCloudPlot(name, color, sampleXY, nX,nY); } + public int addCloudPlot(String name, Color[] color, double[][] sampleXY, int nX, int nY) { + return ((Plot2DCanvas) plotCanvas).addCloudPlot(name, color, sampleXY, nX,nY); + } + public int addCloudPlot(String name, double[][] sampleXY, int nX, int nY) { return addCloudPlot(name, getNewColor(), sampleXY, nX,nY); } @@ -399,4 +479,28 @@ public int addPlot(String type, String name, Color color, double[]... XY) { } } + @Override + public int addPlot(String type, String name, Color[] color, double[]... XY) { + if (type.equalsIgnoreCase(SCATTER)) { + return addScatterPlot(name, color, XY); + } else if (type.equalsIgnoreCase(LINE)) { + return addLinePlot(name, color, XY); + } else if (type.equalsIgnoreCase(BAR)) { + return addBarPlot(name, color, XY); + } else if (type.equalsIgnoreCase(STAIRCASE)) { + return addStaircasePlot(name, color, XY); + } else if (type.equalsIgnoreCase(HISTOGRAM)) { + return addHistogramPlot(name, color, XY); + } else if (type.equalsIgnoreCase(BOX)) { + return addBoxPlot(name, color, XY); + } else { + throw new IllegalArgumentException("Plot type is unknown : " + type); + } + } + + @Override + public int addPlot(String type, String name, Color[][] color, double[]... XY) { + throw new IllegalArgumentException("Plot type is unknown : " + type); + } + } diff --git a/src/main/java/org/math/plot/Plot3DPanel.java b/src/main/java/org/math/plot/Plot3DPanel.java index 01267fd..d959800 100644 --- a/src/main/java/org/math/plot/Plot3DPanel.java +++ b/src/main/java/org/math/plot/Plot3DPanel.java @@ -64,11 +64,19 @@ public Plot3DPanel(String legendOrientation) { public int addScatterPlot(String name, Color color, double[][] XY) { return ((Plot3DCanvas) plotCanvas).addScatterPlot(name, color, XY); } + + public int addScatterPlot(String name, Color[] color, double[][] XY) { + return ((Plot3DCanvas) plotCanvas).addScatterPlot(name, color, XY); + } public int addScatterPlot(String name, Color color, double[] X, double[] Y, double[] Z) { return ((Plot3DCanvas) plotCanvas).addScatterPlot(name, color, X, Y, Z); } + public int addScatterPlot(String name, Color[] color, double[] X, double[] Y, double[] Z) { + return ((Plot3DCanvas) plotCanvas).addScatterPlot(name, color, X, Y, Z); + } + public int addScatterPlot(String name, double[][] XY) { return addScatterPlot(name, getNewColor(), XY); } @@ -101,11 +109,19 @@ public int addScatterPlot(String name, double[] X, double[] Y, double[] Z) { public int addLinePlot(String name, Color color, double[][] XY) { return ((Plot3DCanvas) plotCanvas).addLinePlot(name, color, XY); } + + public int addLinePlot(String name, Color[] color, double[][] XY) { + return ((Plot3DCanvas) plotCanvas).addLinePlot(name, color, XY); + } public int addLinePlot(String name, Color color, double[] X, double[] Y, double[] Z) { return ((Plot3DCanvas) plotCanvas).addLinePlot(name, color, X, Y, Z); } + public int addLinePlot(String name, Color[] color, double[] X, double[] Y, double[] Z) { + return ((Plot3DCanvas) plotCanvas).addLinePlot(name, color, X, Y, Z); + } + public int addLinePlot(String name, double[][] XY) { return addLinePlot(name, getNewColor(), XY); } @@ -138,11 +154,19 @@ public int addLinePlot(String name, double[] X, double[] Y, double[] Z) { public int addBarPlot(String name, Color color, double[][] XY) { return ((Plot3DCanvas) plotCanvas).addBarPlot(name, color, XY); } + + public int addBarPlot(String name, Color[] color, double[][] XY) { + return ((Plot3DCanvas) plotCanvas).addBarPlot(name, color, XY); + } public int addBarPlot(String name, Color color, double[] X, double[] Y, double[] Z) { return ((Plot3DCanvas) plotCanvas).addBarPlot(name, color, X, Y, Z); } + public int addBarPlot(String name, Color[] color, double[] X, double[] Y, double[] Z) { + return ((Plot3DCanvas) plotCanvas).addBarPlot(name, color, X, Y, Z); + } + public int addBarPlot(String name, double[][] XY) { return addBarPlot(name, getNewColor(), XY); } @@ -155,6 +179,10 @@ public int addBoxPlot(String name, Color c, double[][] XY, double[][] dX) { return ((Plot3DCanvas) plotCanvas).addBoxPlot(name, c, XY, dX); } + public int addBoxPlot(String name, Color[] c, double[][] XY, double[][] dX) { + return ((Plot3DCanvas) plotCanvas).addBoxPlot(name, c, XY, dX); + } + public int addBoxPlot(String name, double[][] XY, double[][] dX) { return addBoxPlot(name, getNewColor(), XY, dX); } @@ -163,6 +191,10 @@ public int addBoxPlot(String name, Color c, double[][] XYdX) { return ((Plot3DCanvas) plotCanvas).addBoxPlot(name, c, Array.getColumnsRangeCopy(XYdX, 0, 2), Array.getColumnsRangeCopy(XYdX, 3, 5)); } + public int addBoxPlot(String name, Color[] c, double[][] XYdX) { + return ((Plot3DCanvas) plotCanvas).addBoxPlot(name, c, Array.getColumnsRangeCopy(XYdX, 0, 2), Array.getColumnsRangeCopy(XYdX, 3, 5)); + } + public int addBoxPlot(String name, double[][] XYdX) { return addBoxPlot(name, getNewColor(), XYdX); } @@ -171,6 +203,10 @@ public int addHistogramPlot(String name, Color c, double[][] XY, double[][] dX) return ((Plot3DCanvas) plotCanvas).addHistogramPlot(name, c, XY, dX); } + public int addHistogramPlot(String name, Color[] c, double[][] XY, double[][] dX) { + return ((Plot3DCanvas) plotCanvas).addHistogramPlot(name, c, XY, dX); + } + public int addHistogramPlot(String name, double[][] XY, double[][] dX) { return addHistogramPlot(name, getNewColor(), XY, dX); } @@ -179,6 +215,10 @@ public int addHistogramPlot(String name, Color c, double[][] XYdX) { return ((Plot3DCanvas) plotCanvas).addHistogramPlot(name, c, Array.getColumnsRangeCopy(XYdX, 0, 2), Array.getColumnsRangeCopy(XYdX, 3, 4)); } + public int addHistogramPlot(String name, Color[] c, double[][] XYdX) { + return ((Plot3DCanvas) plotCanvas).addHistogramPlot(name, c, Array.getColumnsRangeCopy(XYdX, 0, 2), Array.getColumnsRangeCopy(XYdX, 3, 4)); + } + public int addHistogramPlot(String name, double[][] XYdX) { return addHistogramPlot(name, getNewColor(), XYdX); } @@ -187,6 +227,10 @@ public int addHistogramPlot(String name, Color c, double[][] XY, int nX, int nY) return ((Plot3DCanvas) plotCanvas).addHistogramPlot(name, c, XY, nX, nY); } + public int addHistogramPlot(String name, Color[] c, double[][] XY, int nX, int nY) { + return ((Plot3DCanvas) plotCanvas).addHistogramPlot(name, c, XY, nX, nY); + } + public int addHistogramPlot(String name, double[][] XY, int nX, int nY) { return addHistogramPlot(name, getNewColor(), XY, nX, nY); } @@ -195,6 +239,10 @@ public int addHistogramPlot(String name, Color c, double[][] XY, double[] bounds return ((Plot3DCanvas) plotCanvas).addHistogramPlot(name, c, XY, boundsX, boundsY); } + public int addHistogramPlot(String name, Color[] c, double[][] XY, double[] boundsX, double[] boundsY) { + return ((Plot3DCanvas) plotCanvas).addHistogramPlot(name, c, XY, boundsX, boundsY); + } + public int addHistogramPlot(String name, double[][] XY, double[] boundsX, double[] boundsY) { return addHistogramPlot(name, getNewColor(), XY, boundsX, boundsY); } @@ -203,6 +251,10 @@ public int addHistogramPlot(String name, Color c, double[][] XY, double minX, do return ((Plot3DCanvas) plotCanvas).addHistogramPlot(name, c, XY, minX, maxX, nX, minY, maxY, nY); } + public int addHistogramPlot(String name, Color[] c, double[][] XY, double minX, double maxX, int nX, double minY, double maxY, int nY) { + return ((Plot3DCanvas) plotCanvas).addHistogramPlot(name, c, XY, minX, maxX, nX, minY, maxY, nY); + } + public int addHistogramPlot(String name, double[][] XY, double minX, double maxX, int nX, double minY, double maxY, int nY) { return addHistogramPlot(name, getNewColor(), XY, minX, maxX, nX, minY, maxY, nY); } @@ -211,6 +263,10 @@ public int addGridPlot(String name, Color c, double[] X, double[] Y, double[][] return ((Plot3DCanvas) plotCanvas).addGridPlot(name, c, X, Y, Z); } + public int addGridPlot(String name, Color[][] c, double[] X, double[] Y, double[][] Z) { + return ((Plot3DCanvas) plotCanvas).addGridPlot(name, c, X, Y, Z); + } + public int addGridPlot(String name, double[] X, double[] Y, double[][] Z) { return addGridPlot(name, getNewColor(), X, Y, Z); } @@ -219,6 +275,10 @@ public int addGridPlot(String name, Color c, double[][] XYZMatrix) { return ((Plot3DCanvas) plotCanvas).addGridPlot(name, c, XYZMatrix); } + public int addGridPlot(String name, Color[][] c, double[][] XYZMatrix) { + return ((Plot3DCanvas) plotCanvas).addGridPlot(name, c, XYZMatrix); + } + public int addGridPlot(String name, double[][] XYZMatrix) { return addGridPlot(name, getNewColor(), XYZMatrix); } @@ -227,6 +287,10 @@ public int addCloudPlot(String name, Color color, double[][] sampleXYZ, int nX, return ((Plot3DCanvas) plotCanvas).addCloudPlot(name, color, sampleXYZ, nX, nY, nZ); } + public int addCloudPlot(String name, Color[] color, double[][] sampleXYZ, int nX, int nY, int nZ) { + return ((Plot3DCanvas) plotCanvas).addCloudPlot(name, color, sampleXYZ, nX, nY, nZ); + } + public int addCloudPlot(String name, double[][] sampleXYZ, int nX, int nY, int nZ) { return addCloudPlot(name, getNewColor(), sampleXYZ, nX, nY, nZ); } @@ -250,6 +314,32 @@ public int addPlot(String type, String name, Color c, double[]... XY) { } } + @Override + public int addPlot(String type, String name, Color[] c, double[]... XY) { + if (type.equalsIgnoreCase(SCATTER)) { + return addScatterPlot(name, c, XY); + } else if (type.equalsIgnoreCase(LINE)) { + return addLinePlot(name, c, XY); + } else if (type.equalsIgnoreCase(BAR)) { + return addBarPlot(name, c, XY); + } else if (type.equalsIgnoreCase(HISTOGRAM)) { + return addHistogramPlot(name, c, XY); + } else if (type.equalsIgnoreCase(BOX)) { + return addBoxPlot(name, c, XY); + } else { + throw new IllegalArgumentException("Plot type is unknown : " + type); + } + } + + @Override + public int addPlot(String type, String name, Color[][] c, double[]... XY) { + if (type.equalsIgnoreCase(BOX)) { + return addGridPlot(name, c, XY); + } else { + throw new IllegalArgumentException("Plot type is unknown : " + type); + } + } + public void rotate(double theta, double phi) { ((Plot3DCanvas) plotCanvas).rotate(theta, phi); repaint(); diff --git a/src/main/java/org/math/plot/PlotPanel.java b/src/main/java/org/math/plot/PlotPanel.java index ac169d0..a6e990e 100644 --- a/src/main/java/org/math/plot/PlotPanel.java +++ b/src/main/java/org/math/plot/PlotPanel.java @@ -388,6 +388,8 @@ public int addPlot(String type, String name, double[]... v) { } public abstract int addPlot(String type, String name, Color c, double[]... v); + public abstract int addPlot(String type, String name, Color[] c, double[]... v); + public abstract int addPlot(String type, String name, Color[][] c, double[]... v); public void setPlot(int I, Plot p) { plotCanvas.setPlot(I, p); diff --git a/src/main/java/org/math/plot/canvas/Plot2DCanvas.java b/src/main/java/org/math/plot/canvas/Plot2DCanvas.java index ad195e3..fcf1f10 100755 --- a/src/main/java/org/math/plot/canvas/Plot2DCanvas.java +++ b/src/main/java/org/math/plot/canvas/Plot2DCanvas.java @@ -63,91 +63,178 @@ private static double[][] convertXY(double[]... XY) { public int addScatterPlot(String name, Color c, double[] Y) { return addPlot(new ScatterPlot(name, c, convertY(Y))); } + + public int addScatterPlot(String name, Color[] c, double[] Y) { + return addPlot(new ScatterPlot(name, c, convertY(Y))); + } public int addScatterPlot(String name, Color c, double[][] XY) { return addPlot(new ScatterPlot(name, c, convertXY(XY))); } + public int addScatterPlot(String name, Color[] c, double[][] XY) { + return addPlot(new ScatterPlot(name, c, convertXY(XY))); + } + public int addScatterPlot(String name, Color c, double[] X, double[] Y) { return addPlot(new ScatterPlot(name, c, convertXY(X,Y))); } + + public int addScatterPlot(String name, Color[] c, double[] X, double[] Y) { + return addPlot(new ScatterPlot(name, c, convertXY(X,Y))); + } public int addLinePlot(String name, Color c, double[] Y) { return addPlot(new LinePlot(name, c, convertY(Y))); } + + public int addLinePlot(String name, Color[] c, double[] Y) { + return addPlot(new LinePlot(name, c, convertY(Y))); + } public int addLinePlot(String name, Color c, double[][] XY) { return addPlot(new LinePlot(name, c, convertXY(XY))); } + public int addLinePlot(String name, Color[] c, double[][] XY) { + return addPlot(new LinePlot(name, c, convertXY(XY))); + } + public int addLinePlot(String name, Color c, double[] X, double[] Y) { return addPlot(new LinePlot(name, c, convertXY(X,Y))); } + + public int addLinePlot(String name, Color[] c, double[] X, double[] Y) { + return addPlot(new LinePlot(name, c, convertXY(X,Y))); + } public int addBarPlot(String name, Color c, double[] Y) { return addPlot(new BarPlot(name, c, convertY(Y))); } + + public int addBarPlot(String name, Color[] c, double[] Y) { + return addPlot(new BarPlot(name, c, convertY(Y))); + } public int addBarPlot(String name, Color c, double[][] XY) { return addPlot(new BarPlot(name, c, convertXY(XY))); } + public int addBarPlot(String name, Color[] c, double[][] XY) { + return addPlot(new BarPlot(name, c, convertXY(XY))); + } + public int addBarPlot(String name, Color c, double[] X, double[] Y) { return addPlot(new BarPlot(name, c, convertXY(X,Y))); } + + public int addBarPlot(String name, Color[] c, double[] X, double[] Y) { + return addPlot(new BarPlot(name, c, convertXY(X,Y))); + } public int addStaircasePlot(String name, Color c, double[] Y) { return addPlot(new StaircasePlot(name, c, convertY(Y))); } + + public int addStaircasePlot(String name, Color[] c, double[] Y) { + return addPlot(new StaircasePlot(name, c, convertY(Y))); + } public int addStaircasePlot(String name, Color c, double[][] XY) { return addPlot(new StaircasePlot(name, c, convertXY(XY))); } + public int addStaircasePlot(String name, Color[] c, double[][] XY) { + return addPlot(new StaircasePlot(name, c, convertXY(XY))); + } + public int addStaircasePlot(String name, Color c, double[] X, double[] Y) { return addPlot(new StaircasePlot(name, c, convertXY(X,Y))); } - + public int addStaircasePlot(String name, Color[] c, double[] X, double[] Y) { + return addPlot(new StaircasePlot(name, c, convertXY(X,Y))); + } + public int addBoxPlot(String name, Color c, double[][] XY, double[][] dX) { return addPlot(new BoxPlot2D(XY, dX, c, name)); } + + public int addBoxPlot(String name, Color[] c, double[][] XY, double[][] dX) { + return addPlot(new BoxPlot2D(XY, dX, c, name)); + } public int addBoxPlot(String name, Color c, double[][] XYdX) { return addPlot(new BoxPlot2D(getColumnsRangeCopy(XYdX, 0, 1), getColumnsRangeCopy(XYdX, 2, 3), c, name)); } + public int addBoxPlot(String name, Color[] c, double[][] XYdX) { + return addPlot(new BoxPlot2D(getColumnsRangeCopy(XYdX, 0, 1), getColumnsRangeCopy(XYdX, 2, 3), c, name)); + } + public int addHistogramPlot(String name, Color c, double[][] XY, double[] dX) { return addPlot(new HistogramPlot2D(name, c, XY, dX)); } + public int addHistogramPlot(String name, Color[] c, double[][] XY, double[] dX) { + return addPlot(new HistogramPlot2D(name, c, XY, dX)); + } + public int addHistogramPlot(String name, Color c, double[][] XY, double dX) { return addPlot(new HistogramPlot2D(name, c, XY, dX)); } + public int addHistogramPlot(String name, Color[] c, double[][] XY, double dX) { + return addPlot(new HistogramPlot2D(name, c, XY, dX)); + } + public int addHistogramPlot(String name, Color c, double[][] XYdX) { return addPlot(new HistogramPlot2D(name, c, getColumnsRangeCopy(XYdX, 0, 1), getColumnCopy(XYdX, 2))); } + public int addHistogramPlot(String name, Color[] c, double[][] XYdX) { + return addPlot(new HistogramPlot2D(name, c, getColumnsRangeCopy(XYdX, 0, 1), getColumnCopy(XYdX, 2))); + } + public int addHistogramPlot(String name, Color c, double[] X, int n) { double[][] XY = histogram_classes(X, n); return addPlot(new HistogramPlot2D(name, c, XY, XY[1][0] - XY[0][0])); } + public int addHistogramPlot(String name, Color[] c, double[] X, int n) { + double[][] XY = histogram_classes(X, n); + return addPlot(new HistogramPlot2D(name, c, XY, XY[1][0] - XY[0][0])); + } + public int addHistogramPlot(String name, Color c, double[] X, double... bounds) { double[][] XY = histogram_classes(X, bounds); return addPlot(new HistogramPlot2D(name, c, XY, XY[1][0] - XY[0][0])); } + public int addHistogramPlot(String name, Color[] c, double[] X, double... bounds) { + double[][] XY = histogram_classes(X, bounds); + return addPlot(new HistogramPlot2D(name, c, XY, XY[1][0] - XY[0][0])); + } + public int addHistogramPlot(String name, Color c, double[] X, double min, double max, int n) { double[][] XY = histogram_classes(X, min, max, n); return addPlot(new HistogramPlot2D(name, c, XY, XY[1][0] - XY[0][0])); } + public int addHistogramPlot(String name, Color[] c, double[] X, double min, double max, int n) { + double[][] XY = histogram_classes(X, min, max, n); + return addPlot(new HistogramPlot2D(name, c, XY, XY[1][0] - XY[0][0])); + } + public int addCloudPlot(String name, Color c, double[][] sampleXY, int nX, int nY) { double[][] XYh = histogram_classes_2D(sampleXY, nX, nY); return addPlot(new CloudPlot2D(name, c, XYh, XYh[1][0] - XYh[0][0], XYh[nX][1] - XYh[0][1])); } + + public int addCloudPlot(String name, Color[] c, double[][] sampleXY, int nX, int nY) { + double[][] XYh = histogram_classes_2D(sampleXY, nX, nY); + return addPlot(new CloudPlot2D(name, c, XYh, XYh[1][0] - XYh[0][0], XYh[nX][1] - XYh[0][1])); + } public static void main(String[] args) { /* diff --git a/src/main/java/org/math/plot/canvas/Plot3DCanvas.java b/src/main/java/org/math/plot/canvas/Plot3DCanvas.java index adbff74..a828e94 100644 --- a/src/main/java/org/math/plot/canvas/Plot3DCanvas.java +++ b/src/main/java/org/math/plot/canvas/Plot3DCanvas.java @@ -70,61 +70,120 @@ public int addScatterPlot(String name, Color c, double[][] XYZ) { return addPlot(new ScatterPlot(name, c, convertXYZ(XYZ))); } + public int addScatterPlot(String name, Color[] c, double[][] XYZ) { + return addPlot(new ScatterPlot(name, c, convertXYZ(XYZ))); + } + public int addScatterPlot(String name, Color c, double[] X, double[] Y, double[] Z) { return addPlot(new ScatterPlot(name, c, convertXYZ(X, Y, Z))); } + public int addScatterPlot(String name, Color[] c, double[] X, double[] Y, double[] Z) { + return addPlot(new ScatterPlot(name, c, convertXYZ(X, Y, Z))); + } + public int addLinePlot(String name, Color c, double[][] XYZ) { return addPlot(new LinePlot(name, c, convertXYZ(XYZ))); } + public int addLinePlot(String name, Color[] c, double[][] XYZ) { + return addPlot(new LinePlot(name, c, convertXYZ(XYZ))); + } + public int addLinePlot(String name, Color c, double[] X, double[] Y, double[] Z) { return addPlot(new LinePlot(name, c, convertXYZ(X, Y, Z))); } + public int addLinePlot(String name, Color[] c, double[] X, double[] Y, double[] Z) { + return addPlot(new LinePlot(name, c, convertXYZ(X, Y, Z))); + } + public int addBarPlot(String name, Color c, double[][] XYZ) { return addPlot(new BarPlot(name, c, convertXYZ(XYZ))); } + public int addBarPlot(String name, Color[] c, double[][] XYZ) { + return addPlot(new BarPlot(name, c, convertXYZ(XYZ))); + } + public int addBarPlot(String name, Color c, double[] X, double[] Y, double[] Z) { return addPlot(new BarPlot(name, c, convertXYZ(X, Y, Z))); } + public int addBarPlot(String name, Color[] c, double[] X, double[] Y, double[] Z) { + return addPlot(new BarPlot(name, c, convertXYZ(X, Y, Z))); + } + public int addBoxPlot(String name, Color c, double[][] XY, double[][] dX) { return addPlot(new BoxPlot3D(XY, dX, c, name)); } + public int addBoxPlot(String name, Color[] c, double[][] XY, double[][] dX) { + return addPlot(new BoxPlot3D(XY, dX, c, name)); + } + public int addBoxPlot(String name, Color c, double[][] XYdX) { return addPlot(new BoxPlot3D(getColumnsRangeCopy(XYdX, 0, 2), getColumnsRangeCopy(XYdX, 3, 5), c, name)); } + public int addBoxPlot(String name, Color[] c, double[][] XYdX) { + return addPlot(new BoxPlot3D(getColumnsRangeCopy(XYdX, 0, 2), getColumnsRangeCopy(XYdX, 3, 5), c, name)); + } + public int addHistogramPlot(String name, Color c, double[][] XY, double[][] dX) { return addPlot(new HistogramPlot3D(name, c, XY, dX)); } + public int addHistogramPlot(String name, Color[] c, double[][] XY, double[][] dX) { + return addPlot(new HistogramPlot3D(name, c, XY, dX)); + } + public int addHistogramPlot(String name, Color c, double[][] XYdX) { return addPlot(new HistogramPlot3D(name, c, getColumnsRangeCopy(XYdX, 0, 2), getColumnsRangeCopy(XYdX, 3, 4))); } + public int addHistogramPlot(String name, Color[] c, double[][] XYdX) { + return addPlot(new HistogramPlot3D(name, c, getColumnsRangeCopy(XYdX, 0, 2), getColumnsRangeCopy(XYdX, 3, 4))); + } + public int addHistogramPlot(String name, Color c, double[][] XY, int nX, int nY) { double[][] XYZ = histogram_classes_2D(XY, nX, nY); return addPlot(new HistogramPlot3D(name, c, XYZ, XYZ[1][0] - XYZ[0][0], XYZ[nX][1] - XYZ[0][1])); } + public int addHistogramPlot(String name, Color[] c, double[][] XY, int nX, int nY) { + double[][] XYZ = histogram_classes_2D(XY, nX, nY); + return addPlot(new HistogramPlot3D(name, c, XYZ, XYZ[1][0] - XYZ[0][0], XYZ[nX][1] - XYZ[0][1])); + } + public int addHistogramPlot(String name, Color c, double[][] XY, double[] boundsX, double[] boundsY) { double[][] XYZ = histogram_classes_2D(XY, boundsX, boundsY); return addPlot(new HistogramPlot3D(name, c, XYZ, XYZ[1][0] - XYZ[0][0], XYZ[boundsX.length - 1][1] - XYZ[0][1])); } + public int addHistogramPlot(String name, Color[] c, double[][] XY, double[] boundsX, double[] boundsY) { + double[][] XYZ = histogram_classes_2D(XY, boundsX, boundsY); + return addPlot(new HistogramPlot3D(name, c, XYZ, XYZ[1][0] - XYZ[0][0], XYZ[boundsX.length - 1][1] - XYZ[0][1])); + } + public int addHistogramPlot(String name, Color c, double[][] XY, double minX, double maxX, int nX, double minY, double maxY, int nY) { double[][] XYZ = histogram_classes_2D(XY, minX, maxX, nX, minY, maxY, nY); return addPlot(new HistogramPlot3D(name, c, XYZ, XYZ[1][0] - XYZ[0][0], XYZ[nX][1] - XYZ[0][1])); } + public int addHistogramPlot(String name, Color[] c, double[][] XY, double minX, double maxX, int nX, double minY, double maxY, int nY) { + double[][] XYZ = histogram_classes_2D(XY, minX, maxX, nX, minY, maxY, nY); + return addPlot(new HistogramPlot3D(name, c, XYZ, XYZ[1][0] - XYZ[0][0], XYZ[nX][1] - XYZ[0][1])); + } + public int addGridPlot(String name, Color c, double[] X, double[] Y, double[][] Z) { return addPlot(new GridPlot3D(name, c, X, Y, Z)); } + public int addGridPlot(String name, Color[][] c, double[] X, double[] Y, double[][] Z) { + return addPlot(new GridPlot3D(name, c, X, Y, Z)); + } + public int addGridPlot(String name, Color c, double[][] XYZMatrix) { double[] X = new double[XYZMatrix[0].length - 1]; System.arraycopy(XYZMatrix[0], 1, X, 0, XYZMatrix[0].length - 1); @@ -137,11 +196,28 @@ public int addGridPlot(String name, Color c, double[][] XYZMatrix) { return addGridPlot(name, c, X, Y, Z); } + public int addGridPlot(String name, Color[][] c, double[][] XYZMatrix) { + double[] X = new double[XYZMatrix[0].length - 1]; + System.arraycopy(XYZMatrix[0], 1, X, 0, XYZMatrix[0].length - 1); + double[] Y = new double[XYZMatrix.length - 1]; + for (int i = 0; i < Y.length; i++) { + Y[i] = XYZMatrix[i + 1][0]; + } + double[][] Z = getSubMatrixRangeCopy(XYZMatrix, 1, XYZMatrix.length - 1, 1, XYZMatrix[0].length - 1); + + return addGridPlot(name, c, X, Y, Z); + } + public int addCloudPlot(String name, Color c, double[][] sampleXYZ, int nX, int nY, int nZ) { double[][] XYZh = histogram_classes_3D(sampleXYZ, nX, nY, nZ); return addPlot(new CloudPlot3D(name, c, XYZh, XYZh[1][0] - XYZh[0][0], XYZh[nX][1] - XYZh[0][1], XYZh[nX][2] - XYZh[0][2])); } + public int addCloudPlot(String name, Color[] c, double[][] sampleXYZ, int nX, int nY, int nZ) { + double[][] XYZh = histogram_classes_3D(sampleXYZ, nX, nY, nZ); + return addPlot(new CloudPlot3D(name, c, XYZh, XYZh[1][0] - XYZh[0][0], XYZh[nX][1] - XYZh[0][1], XYZh[nX][2] - XYZh[0][2])); + } + public void mouseDragged(MouseEvent e) { //System.out.println("PlotCanvas.mouseDragged"); if (ActionMode == ROTATION) { diff --git a/src/main/java/org/math/plot/plots/BarPlot.java b/src/main/java/org/math/plot/plots/BarPlot.java index f474626..cfd02aa 100644 --- a/src/main/java/org/math/plot/plots/BarPlot.java +++ b/src/main/java/org/math/plot/plots/BarPlot.java @@ -16,26 +16,38 @@ public BarPlot(String n, Color c, boolean[][] _pattern, double[][] _XY) { super(n, c, _pattern, _XY); } + public BarPlot(String n, Color[] c, boolean[][] _pattern, double[][] _XY) { + super(n, c, _pattern, _XY); + } + public BarPlot(String n, Color c, int _type, int _radius, double[][] _XY) { super(n, c, _type, _radius, _XY); } + public BarPlot(String n, Color[] c, int _type, int _radius, double[][] _XY) { + super(n, c, _type, _radius, _XY); + } + public BarPlot(String n, Color c, double[][] _XY) { super(n, c, _XY); } - public void plot(AbstractDrawer draw, Color c) { + public BarPlot(String n, Color[] c, double[][] _XY) { + super(n, c, _XY); + } + + public void plot(AbstractDrawer draw, Color[] c) { if (!visible) return; if (draw_dot) super.plot(draw, c); - draw.setColor(c); draw.setLineType(AbstractDrawer.CONTINOUS_LINE); for (int i = 0; i < XY.length; i++) { double[] axeprojection = Array.copy(XY[i]); axeprojection[axeprojection.length - 1] = draw.canvas.base.baseCoords[0][axeprojection.length - 1]; + draw.setColor(c.length == 1 ? c[0] : c[i]); draw.drawLine(XY[i], axeprojection); } } @@ -67,5 +79,31 @@ public static void main(String[] args) { p.setLegendOrientation(PlotPanel.SOUTH); new FrameView(p).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + + Color[] c = new Color[10]; + + p2 = new Plot2DPanel(); + double[][] XYZ = new double[10][2]; + for (int j = 0; j < XYZ.length; j++) { + XYZ[j][0] = /*1 + */Math.random(); + XYZ[j][1] = /*100 * */Math.random(); + c[j] = new Color((int)(Math.random() * 0x1000000)); + } + p2.addBarPlot("toto", c, XYZ); + p2.setLegendOrientation(PlotPanel.SOUTH); + new FrameView(p2).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + p = new Plot3DPanel(); + XYZ = new double[10][3]; + for (int j = 0; j < XYZ.length; j++) { + XYZ[j][0] = /*1 +*/Math.random(); + XYZ[j][1] = /*100 **/Math.random(); + XYZ[j][2] = /*0.0001 **/Math.random(); + c[j] = new Color((int)(Math.random() * 0x1000000)); + } + p.addBarPlot("toto", c, XYZ); + p.setLegendOrientation(PlotPanel.SOUTH); + new FrameView(p).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } \ No newline at end of file diff --git a/src/main/java/org/math/plot/plots/BoxPlot2D.java b/src/main/java/org/math/plot/plots/BoxPlot2D.java index 2f0a160..c2b8c3e 100644 --- a/src/main/java/org/math/plot/plots/BoxPlot2D.java +++ b/src/main/java/org/math/plot/plots/BoxPlot2D.java @@ -15,6 +15,10 @@ public class BoxPlot2D extends Plot { double[][] XY; public BoxPlot2D(double[][] _XY, double[][] w, Color c, String n) { + this(_XY, w, new Color[] { c }, n); + } + + public BoxPlot2D(double[][] _XY, double[][] w, Color[] c, String n) { super(n, c); XY = _XY; widths = w; @@ -42,14 +46,22 @@ public BoxPlot2D(double[][] _XY, double[][] w, Color c, String n) { } - public void plot(AbstractDrawer draw, Color c) { + public void plot(AbstractDrawer draw, Color[] c) { if (!visible) { return; } + + boolean monoColor = false; + if (c.length == 1) { + monoColor = true; + } + else if (c.length != XY.length) { + throw new IllegalArgumentException("Color array length must match length of data array. "); + } - draw.setColor(c); draw.setLineType(AbstractDrawer.CONTINOUS_LINE); for (int i = 0; i < XY.length; i++) { + draw.setColor(monoColor ? c[0] : c[i]); draw.drawLine(new double[]{Xmin[i], Ymin[i]}, new double[]{Xmax[i], Ymin[i]}); draw.drawLine(new double[]{Xmax[i], Ymin[i]}, new double[]{Xmax[i], Ymax[i]}); draw.drawLine(new double[]{Xmax[i], Ymax[i]}, new double[]{Xmin[i], Ymax[i]}); diff --git a/src/main/java/org/math/plot/plots/BoxPlot3D.java b/src/main/java/org/math/plot/plots/BoxPlot3D.java index 42a1909..a074bcd 100644 --- a/src/main/java/org/math/plot/plots/BoxPlot3D.java +++ b/src/main/java/org/math/plot/plots/BoxPlot3D.java @@ -21,6 +21,10 @@ public class BoxPlot3D extends Plot { double[][] XY; public BoxPlot3D(double[][] _XY, double[][] w, Color c, String n) { + this(_XY, w, new Color[] { c }, n); + } + + public BoxPlot3D(double[][] _XY, double[][] w, Color[] c, String n) { super(n, c); XY = _XY; widths = w; @@ -51,14 +55,22 @@ public BoxPlot3D(double[][] _XY, double[][] w, Color c, String n) { } } - public void plot(AbstractDrawer draw, Color c) { + public void plot(AbstractDrawer draw, Color[] c) { if (!visible) { return; } + + boolean monoColor = false; + if (c.length == 1) { + monoColor = true; + } + else if (c.length != XY.length) { + throw new IllegalArgumentException("Color array length must match length of data array length. "); + } - draw.setColor(c); draw.setLineType(AbstractDrawer.CONTINOUS_LINE); for (int i = 0; i < XY.length; i++) { + draw.setColor(monoColor ? c[0] : c[i]); draw.drawLine(new double[]{Xmin[i], Ymin[i], Zmin[i]}, new double[]{Xmax[i], Ymin[i], Zmin[i]}); draw.drawLine(new double[]{Xmax[i], Ymin[i], Zmin[i]}, new double[]{Xmax[i], Ymax[i], Zmin[i]}); draw.drawLine(new double[]{Xmax[i], Ymax[i], Zmin[i]}, new double[]{Xmin[i], Ymax[i], Zmin[i]}); @@ -133,7 +145,25 @@ public static void main(String[] args) { } int receiverPlotDataIndex = plotpanel.addBoxPlot("Receivers", Color.orange, receiverXYZ); } - + plotpanel.setLegendOrientation(PlotPanel.SOUTH); + new FrameView(plotpanel).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + + plotpanel = new Plot3DPanel(); + for (int i = 0; i < 1; i++) { + double[][] receiverXYZ = new double[100][6]; + Color[] c = new Color[100]; + for (int j = 0; j < receiverXYZ.length; j++) { + receiverXYZ[j][0] = /*1 + */ Math.random(); + receiverXYZ[j][1] = /*100 * */ Math.random(); + receiverXYZ[j][2] = /*100 * */ Math.random(); + receiverXYZ[j][3] = /*1 + */ Math.random() / 10; + receiverXYZ[j][4] = /*100 * */ Math.random() / 10; + receiverXYZ[j][5] = /*100 * */ Math.random() / 10; + c[j] = new Color((int)(Math.random() * 0x1000000)); + } + int receiverPlotDataIndex = plotpanel.addBoxPlot("Receivers", c, receiverXYZ); + } plotpanel.setLegendOrientation(PlotPanel.SOUTH); new FrameView(plotpanel).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } diff --git a/src/main/java/org/math/plot/plots/CloudPlot2D.java b/src/main/java/org/math/plot/plots/CloudPlot2D.java index fe86cae..d899990 100644 --- a/src/main/java/org/math/plot/plots/CloudPlot2D.java +++ b/src/main/java/org/math/plot/plots/CloudPlot2D.java @@ -25,6 +25,10 @@ public class CloudPlot2D extends Plot { boolean fill_shape = true; public CloudPlot2D(String n, Color c, double[][] _XYcard, double wX, double wY) { + this(n, new Color[] { c }, _XYcard, wX, wY); + } + + public CloudPlot2D(String n, Color[] c, double[][] _XYcard, double wX, double wY) { super(n, c); splitXYf(_XYcard); width_constant = new double[]{wX, wY}; @@ -64,18 +68,26 @@ private void build() { } } - public void plot(AbstractDrawer draw, Color c) { + public void plot(AbstractDrawer draw, Color[] c) { if (!visible) { return; } + + boolean monoColor = false; + if (c.length == 1) { + monoColor = true; + } + else if (c.length != XY.length) { + throw new IllegalArgumentException("Color array length must match length of data array. "); + } draw.canvas.includeInBounds(SW[0]); draw.canvas.includeInBounds(NE[XY.length - 1]); - draw.setColor(c); draw.setLineType(AbstractDrawer.CONTINOUS_LINE); for (int i = 0; i < XY.length; i++) { if (f[i] > 0) { + draw.setColor(monoColor ? c[0] : c[i]); draw.fillPolygon(f[i], NW[i], NE[i], SE[i], SW[i]); } } @@ -133,5 +145,30 @@ public static void main(String[] args) { p.setLegendOrientation(PlotPanel.SOUTH); new FrameView(p).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + + Color[] c = new Color[5 * 5]; + for (int i = 0; i < c.length; i++) { + c[i] = new Color((int)(Math.random() * 0x1000000)); + } + + p = new Plot2DPanel(); + + cloud = new double[100][2]; + for (int i = 0; i < cloud.length; i++) { + cloud[i][0] = Math.random() + Math.random(); + cloud[i][1] = Math.random() + Math.random(); + } + p.addCloudPlot("cloud", c, cloud, 5, 5); + + cloud2 = new double[100][2]; + for (int i = 0; i < cloud2.length; i++) { + cloud2[i][0] = 2 + Math.random() + Math.random(); + cloud2[i][1] = 2 + Math.random() + Math.random(); + } + p.addCloudPlot("cloud2", c, cloud2, 5, 5); + + p.setLegendOrientation(PlotPanel.SOUTH); + new FrameView(p).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } diff --git a/src/main/java/org/math/plot/plots/CloudPlot3D.java b/src/main/java/org/math/plot/plots/CloudPlot3D.java index 414f7dd..320ae33 100644 --- a/src/main/java/org/math/plot/plots/CloudPlot3D.java +++ b/src/main/java/org/math/plot/plots/CloudPlot3D.java @@ -29,6 +29,10 @@ public class CloudPlot3D extends Plot { boolean fill_shape = true; public CloudPlot3D(String n, Color c, double[][] _XYcard, double wX, double wY, double wZ) { + this(n, new Color[] { c }, _XYcard, wX, wY, wZ); + } + + public CloudPlot3D(String n, Color[] c, double[][] _XYcard, double wX, double wY, double wZ) { super(n, c); splitXYf(_XYcard); width_constant = new double[]{wX, wY, wZ}; @@ -77,18 +81,26 @@ private void build() { } } - public void plot(AbstractDrawer draw, Color c) { + public void plot(AbstractDrawer draw, Color[] c) { if (!visible) { return; } + + boolean monoColor = false; + if (c.length == 1) { + monoColor = true; + } + else if (c.length != XY.length) { + throw new IllegalArgumentException("Color array length must match length of data array. "); + } draw.canvas.includeInBounds(botSW[0]); draw.canvas.includeInBounds(topNE[XY.length - 1]); - draw.setColor(c); draw.setLineType(AbstractDrawer.CONTINOUS_LINE); for (int i = 0; i < XY.length; i++) { if (f[i] > 0) { + draw.setColor(monoColor ? c[0] : c[i]); draw.fillPolygon(f[i], topNW[i], topNE[i], topSE[i], topSW[i]); draw.fillPolygon(f[i], botNW[i], botNE[i], botSE[i], botSW[i]); @@ -156,5 +168,35 @@ public static void main(String[] args) { p.setLegendOrientation(PlotPanel.SOUTH); new FrameView(p).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + + + + Color[] c = new Color[3 * 3 * 3]; + for (int i = 0; i < c.length; i++) { + c[i] = new Color((int)(Math.random() * 0x1000000)); + } + + p = new Plot3DPanel(); + + //triangular random cloud (as sum of two uniform random numbers) + cloud = new double[100][3]; + for (int i = 0; i < cloud.length; i++) { + cloud[i][0] = Math.random() + Math.random(); + cloud[i][1] = Math.random() + Math.random(); + cloud[i][2] = Math.random() + Math.random(); + } + p.addCloudPlot("cloud", c, cloud, 3, 3, 3); + + cloud2 = new double[100][3]; + for (int i = 0; i < cloud.length; i++) { + cloud2[i][0] = 2 + Math.random() + Math.random(); + cloud2[i][1] = 2 + Math.random() + Math.random(); + cloud2[i][2] = 2 + Math.random() + Math.random(); + } + p.addCloudPlot("cloud2", c, cloud2, 3, 3, 3); + + p.setLegendOrientation(PlotPanel.SOUTH); + new FrameView(p).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } diff --git a/src/main/java/org/math/plot/plots/DensityLayerPlot.java b/src/main/java/org/math/plot/plots/DensityLayerPlot.java index e11c4c9..d847e44 100644 --- a/src/main/java/org/math/plot/plots/DensityLayerPlot.java +++ b/src/main/java/org/math/plot/plots/DensityLayerPlot.java @@ -45,12 +45,10 @@ public int getAxe() { return axis; } - public void plot(AbstractDrawer draw, Color c) { + public void plot(AbstractDrawer draw, Color[] c) { if (!plot.visible) return; - draw.setColor(c); - draw.setLineType(AbstractDrawer.CONTINOUS_LINE); draw.setLineWidth(WIDTH); if (constant_Q == null) @@ -65,10 +63,11 @@ public void plot(AbstractDrawer draw, Color c) { double[] d2 = Array.getRowCopy(plot.getData(), i); for (int j = 0; j < Q[i].length - 2; j++) { + Color c0 = (c.length > 1 ? c[i] : c[0]); d1[axis] = d0[axis] + ((Q[i][j] + Q[i][j + 1]) / 2); d2[axis] = d0[axis] + ((Q[i][j + 1] + Q[i][j + 2]) / 2); - Color c1 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255.0 * (norm / (Q[i][j + 1] - Q[i][j])))); - Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255.0 * (norm / (Q[i][j + 2] - Q[i][j + 1])))); + Color c1 = new Color(c0.getRed(), c0.getGreen(), c0.getBlue(), (int) (255.0 * (norm / (Q[i][j + 1] - Q[i][j])))); + Color c2 = new Color(c0.getRed(), c0.getGreen(), c0.getBlue(), (int) (255.0 * (norm / (Q[i][j + 2] - Q[i][j + 1])))); draw.setGradient(d1, c1, d2, c2); draw.drawLine(d1, d2); } @@ -85,10 +84,11 @@ public void plot(AbstractDrawer draw, Color c) { double[] d2 = Array.getRowCopy(plot.getData(), i); for (int j = 0; j < constant_Q.length - 2; j++) { + Color c0 = (c.length > 1 ? c[i] : c[0]); d1[axis] = d0[axis] + (constant_Q[j] + constant_Q[j + 1]) / 2; d2[axis] = d0[axis] + (constant_Q[j + 1] + constant_Q[j + 2]) / 2; - Color c1 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255.0 * (norm / (constant_Q[j + 1] - constant_Q[j])))); - Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255.0 * (norm / (constant_Q[j + 2] - constant_Q[j + 1])))); + Color c1 = new Color(c0.getRed(), c0.getGreen(), c0.getBlue(), (int) (255.0 * (norm / (constant_Q[j + 1] - constant_Q[j])))); + Color c2 = new Color(c0.getRed(), c0.getGreen(), c0.getBlue(), (int) (255.0 * (norm / (constant_Q[j + 2] - constant_Q[j + 1])))); draw.setGradient(d1, c1, d2, c2); draw.drawLine(d1, d2); } @@ -125,5 +125,21 @@ public static void main(String[] args) { //p2.getPlot(1).addLayer(new DensityLayerPlot(p2.getPlot(1), 1, new double[] { -.1, 0, .1 })); new FrameView(p2).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + + Color[] c = new Color[10]; + p2 = new Plot2DPanel(); + double[][] XYZ = new double[10][2]; + for (int j = 0; j < XYZ.length; j++) { + XYZ[j][0] = /*1 + */Math.random(); + XYZ[j][1] = /*100 * */10 * Math.random(); + c[j] = new Color((int)(Math.random() * 0x1000000)); + } + + p2.addScatterPlot("toto", c, XYZ); + p2.getPlot(0).addQuantiles(1, new double[] {/*-3,-2,*/-4, -2, -0.5, 0, 0.5, 2, 4 /*,2,3*/}); + //p2.getPlot(1).addLayer(new DensityLayerPlot(p2.getPlot(1), 1, new double[] { -.1, 0, .1 })); + + new FrameView(p2).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } \ No newline at end of file diff --git a/src/main/java/org/math/plot/plots/GaussianDensityLayerPlot.java b/src/main/java/org/math/plot/plots/GaussianDensityLayerPlot.java index 87b3796..d690604 100644 --- a/src/main/java/org/math/plot/plots/GaussianDensityLayerPlot.java +++ b/src/main/java/org/math/plot/plots/GaussianDensityLayerPlot.java @@ -73,20 +73,19 @@ public int getAxe() { return axis; } - public void plot(AbstractDrawer draw, Color c) { + public void plot(AbstractDrawer draw, Color[] c) { if (!plot.visible) return; - draw.setColor(c); - draw.setLineType(AbstractDrawer.CONTINOUS_LINE); draw.setLineWidth(WIDHT); if (constant_sigma == 0) for (int i = 0; i < plot.getData().length; i++) { - gradC_0sigma = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255.0 * (gausspdf_sigma[i][0]))); - gradC_1sigma = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255.0 * (gausspdf_sigma[i][1]))); - gradC_2sigma = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255.0 * (gausspdf_sigma[i][2]))); - gradC_3sigma = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255.0 * (gausspdf_sigma[i][3]))); + Color c0 = (c.length > 1 ? c[i] : c[0]); + gradC_0sigma = new Color(c0.getRed(), c0.getGreen(), c0.getBlue(), (int) (255.0 * (gausspdf_sigma[i][0]))); + gradC_1sigma = new Color(c0.getRed(), c0.getGreen(), c0.getBlue(), (int) (255.0 * (gausspdf_sigma[i][1]))); + gradC_2sigma = new Color(c0.getRed(), c0.getGreen(), c0.getBlue(), (int) (255.0 * (gausspdf_sigma[i][2]))); + gradC_3sigma = new Color(c0.getRed(), c0.getGreen(), c0.getBlue(), (int) (255.0 * (gausspdf_sigma[i][3]))); double[] d = Array.getRowCopy(plot.getData(), i); double[] d2 = Array.getRowCopy(plot.getData(), i); @@ -121,12 +120,12 @@ public void plot(AbstractDrawer draw, Color c) { draw.drawLine(d2, d); } else { - gradC_0sigma = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255.0 * (gausspdf_sigma[0][0]))); - gradC_1sigma = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255.0 * (gausspdf_sigma[0][1]))); - gradC_2sigma = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255.0 * (gausspdf_sigma[0][2]))); - gradC_3sigma = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255.0 * (gausspdf_sigma[0][3]))); - for (int i = 0; i < plot.getData().length; i++) { + Color c0 = (c.length > 1 ? c[i] : c[0]); + gradC_0sigma = new Color(c0.getRed(), c0.getGreen(), c0.getBlue(), (int) (255.0 * (gausspdf_sigma[0][0]))); + gradC_1sigma = new Color(c0.getRed(), c0.getGreen(), c0.getBlue(), (int) (255.0 * (gausspdf_sigma[0][1]))); + gradC_2sigma = new Color(c0.getRed(), c0.getGreen(), c0.getBlue(), (int) (255.0 * (gausspdf_sigma[0][2]))); + gradC_3sigma = new Color(c0.getRed(), c0.getGreen(), c0.getBlue(), (int) (255.0 * (gausspdf_sigma[0][3]))); double[] d = Array.getRowCopy(plot.getData(), i); double[] d2 = Array.getRowCopy(plot.getData(), i); @@ -195,5 +194,22 @@ public static void main(String[] args) { p2.getPlot(1).addGaussQuantiles(1, 0.1); new FrameView(p2).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + + Color[] c = new Color[10]; + p2 = new Plot2DPanel(); + double[][] XYZ = new double[10][2]; + sXYZ = new double[10]; + for (int j = 0; j < XYZ.length; j++) { + XYZ[j][0] = /*1 + */Math.random(); + XYZ[j][1] = /*100 * */Math.random(); + sXYZ[j] = /*100 * */Math.random(); + c[j] = new Color((int)(Math.random() * 0x1000000)); + } + + p2.addScatterPlot("toto", c, XYZ); + p2.getPlot(0).addGaussQuantiles(0, sXYZ); + + new FrameView(p2).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } \ No newline at end of file diff --git a/src/main/java/org/math/plot/plots/GridPlot3D.java b/src/main/java/org/math/plot/plots/GridPlot3D.java index 2128f18..9142700 100644 --- a/src/main/java/org/math/plot/plots/GridPlot3D.java +++ b/src/main/java/org/math/plot/plots/GridPlot3D.java @@ -4,6 +4,7 @@ package org.math.plot.plots; import java.awt.*; +import java.util.TreeSet; import org.math.plot.*; import org.math.plot.render.*; @@ -15,35 +16,63 @@ public class GridPlot3D extends Plot { double[] Y; double[][] Z; private double[][] XYZ_list; + private Color[][] colors = null; public boolean draw_lines = true; public boolean fill_shape = true; public GridPlot3D(String n, Color c, double[] _X, double[] _Y, double[][] _Z) { - super(n, c); + this(n, new Color[][] { new Color[] { c } }, _X, _Y, _Z); + } + + public GridPlot3D(String n, Color[][] c, double[] _X, double[] _Y, double[][] _Z) { + super(n, c[0][0]); + colors = c; X = _X; Y = _Y; Z = _Z; buildXYZ_list(); } - public void plot(AbstractDrawer draw, Color c) { + public void plot(AbstractDrawer draw, Color[] c) { if (!visible) { return; } - - draw.setColor(c); - + + boolean monoColor = false; + if (colors.length == 1) { + monoColor = true; + } + else if (colors.length != Y.length || colors[0].length != X.length) { + throw new IllegalArgumentException("Color array length must match length of data arrays."); + } + + if (monoColor) { + draw.setColor(c[0]); + } + + double [] cP; + double [] nP; if (draw_lines) { draw.setLineType(AbstractDrawer.CONTINOUS_LINE); for (int i = 0; i < X.length; i++) { for (int j = 0; j < Y.length - 1; j++) { - draw.drawLine(new double[]{X[i], Y[j], Z[j][i]}, new double[]{X[i], Y[j + 1], Z[j + 1][i]}); + cP = new double[]{X[i], Y[j], Z[j][i]}; + nP = new double[]{X[i], Y[j + 1], Z[j + 1][i]}; + if (!monoColor) { + draw.setGradient(cP, colors[j][i], nP, colors[j + 1][i]); + } + draw.drawLine(cP, nP); } } for (int j = 0; j < Y.length; j++) { for (int i = 0; i < X.length - 1; i++) { - draw.drawLine(new double[]{X[i], Y[j], Z[j][i]}, new double[]{X[i + 1], Y[j], Z[j][i + 1]}); + cP = new double[]{X[i], Y[j], Z[j][i]}; + nP = new double[]{X[i + 1], Y[j], Z[j][i + 1]}; + if (!monoColor) { + draw.setGradient(cP, colors[j][i], nP, colors[j][i + 1]); + } + draw.drawLine(cP, nP); } } } else { @@ -51,18 +80,56 @@ public void plot(AbstractDrawer draw, Color c) { draw.setDotRadius(AbstractDrawer.DEFAULT_DOT_RADIUS); for (int i = 0; i < X.length; i++) { for (int j = 0; j < Y.length; j++) { + if (!monoColor) { + draw.setColor(colors[j][i]); + } draw.drawDot(new double[]{X[i], Y[j], Z[j][i]}); } } } if (fill_shape) { - for (int j = 0; j < Y.length - 1; j++) { - for (int i = 0; i < X.length - 1; i++) { - draw.fillPolygon(0.2f, new double[]{X[i], Y[j], Z[j][i]}, new double[]{X[i + 1], Y[j], Z[j][i + 1]}, new double[]{X[i + 1], Y[j + 1], - Z[j + 1][i + 1]}, new double[]{X[i], Y[j + 1], Z[j + 1][i]}); - } - } + if (monoColor) { + for (int j = 0; j < Y.length - 1; j++) { + for (int i = 0; i < X.length - 1; i++) { + draw.fillPolygon(0.2f, new double[]{X[i], Y[j], Z[j][i]}, new double[]{X[i + 1], Y[j], Z[j][i + 1]}, new double[]{X[i + 1], Y[j + 1], + Z[j + 1][i + 1]}, new double[]{X[i], Y[j + 1], Z[j + 1][i]}); + } + } + } + else { + for (int j = 0; j < Y.length - 1; j++) { + for (int i = 0; i < X.length - 1; i++) { + cP = new double[]{X[i], Y[j], Z[j][i]}; + nP = new double[]{X[i], Y[j + 1], Z[j + 1][i]}; + draw.setGradient(cP, colors[j][i], nP, colors[j + 1][i]); + draw.fillPolygon(0.2f, cP, new double[]{X[i + 1], Y[j], Z[j][i + 1]}, new double[]{X[i + 1], Y[j + 1], + Z[j + 1][i + 1]}, nP); + + cP = new double[]{X[i], Y[j], Z[j][i]}; + nP = new double[]{X[i + 1], Y[j], Z[j][i + 1]}; + draw.setGradient(cP, colors[j][i], nP, colors[j][i + 1]); + draw.fillPolygon(0.2f, cP, nP, new double[]{X[i + 1], Y[j + 1], + Z[j + 1][i + 1]}, new double[]{X[i], Y[j + 1], Z[j + 1][i]}); + } + } + for (int j = 1; j < Y.length; j++) { + for (int i = 1; i < X.length; i++) { + cP = new double[]{X[i], Y[j], Z[j][i]}; + nP = new double[]{X[i], Y[j - 1], Z[j - 1][i]}; + draw.setGradient(cP, colors[j][i], nP, colors[j - 1][i]); + draw.fillPolygon(0.2f, cP, new double[]{X[i - 1], Y[j], Z[j][i - 1]}, new double[]{X[i - 1], Y[j - 1], + Z[j - 1][i - 1]}, nP); + + cP = new double[]{X[i], Y[j], Z[j][i]}; + nP = new double[]{X[i - 1], Y[j], Z[j][i - 1]}; + draw.setGradient(cP, colors[j][i], nP, colors[j][i - 1]); + draw.fillPolygon(0.2f, cP, nP, new double[]{X[i - 1], Y[j - 1], + Z[j - 1][i - 1]}, new double[]{X[i], Y[j - 1], Z[j - 1][i]}); + } + } + + } } } @@ -88,12 +155,12 @@ public void setData(double[][] _Z) { public double[][] getData() { return XYZ_list; } - +/* @Override public double[][] getBounds() { return new double[][]{{Array.min(X), Array.min(Y), Array.min(Array.min(Z))}, {Array.max(X), Array.max(Y), Array.max(Array.min(Z))}}; } - +*/ public void setDataZ(double[][] _Z) { setData(_Z); } @@ -165,5 +232,49 @@ public static void main(String[] args) { p.setLegendOrientation(PlotPanel.SOUTH); new FrameView(p); + + + + + TreeSet uv = new TreeSet(); + Color[][] c = new Color[m][n]; + X = new double[n]; + Y = new double[m]; + Z = new double[m][n]; + p = new Plot3DPanel(); + for (int i = 0; i < X.length; i++) { + X[i] = 3 + i / (double) X.length; + for (int j = 0; j < Y.length; j++) { + Y[j] = 5 + j / (double) Y.length; + Z[j][i] = Math.random(); + uv.add(Z[j][i]); + } + } + Color[] gc = new Color[uv.size()]; + Color low = Color.blue; + Color high = Color.red; + float[] hsv1 = Color.RGBtoHSB(low.getRed(), low.getGreen(), low.getBlue(), null); + float[] hsv2 = Color.RGBtoHSB(high.getRed(), high.getGreen(), high.getBlue(), null); + int a1 = low.getAlpha(); + float h1 = hsv1[0] ; + float s1 = hsv1[1]; + float v1 = hsv1[2]; + float da = high.getAlpha()- a1; + float dh = hsv2[0] - h1; + float ds = hsv2[1]- s1; + float dv = hsv2[2] - v1; + for (int i = 0; i < gc.length; ++i) { + float rel = i / (float)(gc.length - 1); + int rgb = Color.HSBtoRGB(h1 + dh * rel, s1 + ds * rel, v1 + dv * rel); + rgb +=(((int)(a1 + da * rel)) << 24); + gc[i] = new Color(rgb); + } + for (int i = 0; i < X.length; i++) { + for (int j = 0; j < Y.length; j++) + c[j][i] = gc[uv.headSet(Z[j][i]).size()]; + } + p.addGridPlot("toto", c, X, Y, Z); + p.setLegendOrientation(PlotPanel.SOUTH); + new FrameView(p); } } \ No newline at end of file diff --git a/src/main/java/org/math/plot/plots/HistogramPlot2D.java b/src/main/java/org/math/plot/plots/HistogramPlot2D.java index ba052d8..ce73788 100644 --- a/src/main/java/org/math/plot/plots/HistogramPlot2D.java +++ b/src/main/java/org/math/plot/plots/HistogramPlot2D.java @@ -24,12 +24,25 @@ public HistogramPlot2D(String n, Color c, double[][] _XY, double w) { this(n, c, _XY, w, 0.5, 1); } + public HistogramPlot2D(String n, Color[] c, double[][] _XY, double w) { + this(n, c, _XY, w, 0.5, 1); + } + public HistogramPlot2D(String n, Color c, double[][] _XY, double[] w) { this(n, c, _XY, w, 0.5, 1); } + public HistogramPlot2D(String n, Color[] c, double[][] _XY, double[] w) { + this(n, c, _XY, w, 0.5, 1); + } + // TODO Histogram group plots public HistogramPlot2D(String n, Color c, double[][] _XY, double w, double _offsetCenter_perWidth, double _factorWidth) { + this(n, new Color[] { c }, _XY, w, _offsetCenter_perWidth, _factorWidth); + } + + // TODO Histogram group plots + public HistogramPlot2D(String n, Color[] c, double[][] _XY, double w, double _offsetCenter_perWidth, double _factorWidth) { super(n, c); XY = _XY; width_constant = w; @@ -42,6 +55,10 @@ public HistogramPlot2D(String n, Color c, double[][] _XY, double w, double _offs } public HistogramPlot2D(String n, Color c, double[][] _XY, double[] w, double _offsetCenter_perWidth, double _factorWidth) { + this(n, new Color[] { c }, _XY, w, _offsetCenter_perWidth, _factorWidth); + } + + public HistogramPlot2D(String n, Color[] c, double[][] _XY, double[] w, double _offsetCenter_perWidth, double _factorWidth) { super(n, c); XY = _XY; widths = w; @@ -111,14 +128,22 @@ private void build() { * bottomRight[datas.length - 1] = new double[] { datas[datas.length - 1][0] + * (datas[datas.length - 1][0] - datas[datas.length - 2][0]) / 2, 0 }; } */ - public void plot(AbstractDrawer draw, Color c) { + public void plot(AbstractDrawer draw, Color[] c) { if (!visible) { return; } + + boolean monoColor = false; + if (c.length == 1) { + monoColor = true; + } + else if (c.length != XY.length) { + throw new IllegalArgumentException("Color array length must match length of data array. "); + } - draw.setColor(c); draw.setLineType(AbstractDrawer.CONTINOUS_LINE); for (int i = 0; i < XY.length; i++) { + draw.setColor(monoColor ? c[0] : c[i]); draw.drawLine(bottomLeft[i], topLeft[i]); draw.drawLine(topLeft[i], topRight[i]); draw.drawLine(topRight[i], bottomRight[i]); @@ -196,6 +221,15 @@ public static void main(String[] args) { } Plot2DPanel p = new Plot2DPanel("SOUTH"); p.addHistogramPlot("test", X, 10); + new FrameView(p); + + + Color[] c = new Color[10]; + for (int i = 0; i < c.length; i++) { + c[i] = new Color((int)(Math.random() * 0x1000000)); + } + p = new Plot2DPanel("SOUTH"); + p.addHistogramPlot("test", c, X, 10); new FrameView(p); } } \ No newline at end of file diff --git a/src/main/java/org/math/plot/plots/HistogramPlot3D.java b/src/main/java/org/math/plot/plots/HistogramPlot3D.java index 4fe664d..51e47bc 100644 --- a/src/main/java/org/math/plot/plots/HistogramPlot3D.java +++ b/src/main/java/org/math/plot/plots/HistogramPlot3D.java @@ -22,6 +22,10 @@ public class HistogramPlot3D extends Plot { boolean fill_shape = true; public HistogramPlot3D(String n, Color c, double[][] _XY, double[][] w) { + this(n, new Color[] { c }, _XY, w); + } + + public HistogramPlot3D(String n, Color[] c, double[][] _XY, double[][] w) { super(n, c); XY = _XY; widths = w; @@ -30,6 +34,10 @@ public HistogramPlot3D(String n, Color c, double[][] _XY, double[][] w) { } public HistogramPlot3D(String n, Color c, double[][] _XY, double wX, double wY) { + this(n, new Color[] { c }, _XY, wX, wY); + } + + public HistogramPlot3D(String n, Color[] c, double[][] _XY, double wX, double wY) { super(n, c); XY = _XY; width_constant = new double[]{wX, wY}; @@ -38,6 +46,10 @@ public HistogramPlot3D(String n, Color c, double[][] _XY, double wX, double wY) } public HistogramPlot3D(String n, Color c, double[][] _XY, double[] w) { + this(n, new Color[] { c }, _XY, w); + } + + public HistogramPlot3D(String n, Color[] c, double[][] _XY, double[] w) { super(n, c); XY = _XY; width_constant = w; @@ -87,17 +99,25 @@ private void build() { } } - public void plot(AbstractDrawer draw, Color c) { + public void plot(AbstractDrawer draw, Color[] c) { if (!visible) { return; } + + boolean monoColor = false; + if (c.length == 1) { + monoColor = true; + } + else if (c.length != XY.length) { + throw new IllegalArgumentException("Color array length must match length of data array length. "); + } draw.canvas.includeInBounds(bottomSW[0]); draw.canvas.includeInBounds(topNE[XY.length - 1]); - draw.setColor(c); draw.setLineType(AbstractDrawer.CONTINOUS_LINE); for (int i = 0; i < XY.length; i++) { + draw.setColor(monoColor ? c[0] : c[i]); if (topNW[i][2] != bottomNW[i][2]) { draw.drawLine(topNW[i], topNE[i]); draw.drawLine(topNE[i], topSE[i]); @@ -117,10 +137,13 @@ public void plot(AbstractDrawer draw, Color c) { if (fill_shape) { draw.fillPolygon(0.2f, topNW[i], topNE[i], topSE[i], topSW[i]); //draw.fillPolygon(bottomNW[i], bottomNE[i], bottomSE[i], bottomSW[i]); - /*draw.fillPolygon(topNW[i], topNE[i], bottomNE[i], bottomNW[i]); - draw.fillPolygon(topSW[i], topSE[i], bottomSE[i], bottomSW[i]); - draw.fillPolygon(topNE[i], topSE[i], bottomSE[i], bottomNE[i]); - draw.fillPolygon(topNW[i], topSW[i], bottomSW[i], bottomNW[i]);*/ + if (!monoColor) + { + draw.fillPolygon(0.2f, topNW[i], topNE[i], bottomNE[i], bottomNW[i]); + draw.fillPolygon(0.2f, topSW[i], topSE[i], bottomSE[i], bottomSW[i]); + draw.fillPolygon(0.2f, topNE[i], topSE[i], bottomSE[i], bottomNE[i]); + draw.fillPolygon(0.2f, topNW[i], topSW[i], bottomSW[i], bottomNW[i]); + } } } } @@ -192,6 +215,15 @@ public static void main(String[] args) { } Plot3DPanel p = new Plot3DPanel("SOUTH"); p.addHistogramPlot("test", XY, 4, 6); + new FrameView(p); + + + Color[] c = new Color[4 * 6]; + for (int i = 0; i < c.length; i++) { + c[i] = new Color((int)(Math.random() * 0x1000000)); + } + p = new Plot3DPanel("SOUTH"); + p.addHistogramPlot("test", c, XY, 4, 6); new FrameView(p); } } \ No newline at end of file diff --git a/src/main/java/org/math/plot/plots/LayerPlot.java b/src/main/java/org/math/plot/plots/LayerPlot.java index 413a956..d312678 100644 --- a/src/main/java/org/math/plot/plots/LayerPlot.java +++ b/src/main/java/org/math/plot/plots/LayerPlot.java @@ -12,7 +12,7 @@ public abstract class LayerPlot extends Plot { Plot plot; public LayerPlot(String name, Plot p) { - super(name, p.color); + super(name, p.colors); plot = p; } diff --git a/src/main/java/org/math/plot/plots/LinePlot.java b/src/main/java/org/math/plot/plots/LinePlot.java index cf1c309..f38fd9c 100644 --- a/src/main/java/org/math/plot/plots/LinePlot.java +++ b/src/main/java/org/math/plot/plots/LinePlot.java @@ -15,50 +15,98 @@ public LinePlot(String n, Color c, boolean[][] _pattern, double[][] _XY) { super(n, c, _pattern, _XY); } + public LinePlot(String n, Color[] c, boolean[][] _pattern, double[][] _XY) { + super(n, c, _pattern, _XY); + } + public LinePlot(String n, Color c, int _type, int _radius, double[][] _XY) { super(n, c, _type, _radius, _XY); } + public LinePlot(String n, Color[] c, int _type, int _radius, double[][] _XY) { + super(n, c, _type, _radius, _XY); + } + public LinePlot(String n, Color c, double[][] _XY) { super(n, c, _XY); } - public void plot(AbstractDrawer draw, Color c) { + public LinePlot(String n, Color[] c, double[][] _XY) { + super(n, c, _XY); + } + + public void plot(AbstractDrawer draw, Color[] c) { if (!visible) return; if (draw_dot) super.plot(draw, c); - draw.setColor(c); draw.setLineType(AbstractDrawer.CONTINOUS_LINE); - for (int i = 0; i < XY.length - 1; i++) + for (int i = 0; i < XY.length - 1; i++) { + if (c.length == 1) { + draw.setColor(c[0]); + } + else { + draw.setGradient(XY[i], c[i], XY[i + 1], c[i + 1]); + } draw.drawLine(XY[i], XY[i + 1]); + } } public static void main(String[] args) { Plot2DPanel p2 = new Plot2DPanel(); - double[][] XYZ = new double[100][2]; - for (int j = 0; j < XYZ.length; j++) { - XYZ[j][0] = 2*Math.PI*(double)j/XYZ.length; - XYZ[j][1] = Math.sin(XYZ[j][0]); - } - p2.addLinePlot("sin" , XYZ); + double[][] XYZ = new double[100][2]; + for (int j = 0; j < XYZ.length; j++) { + XYZ[j][0] = 2*Math.PI*(double)j/XYZ.length; + XYZ[j][1] = Math.sin(XYZ[j][0]); + } + p2.addLinePlot("sin" , XYZ); - p2.setLegendOrientation(PlotPanel.SOUTH); new FrameView(p2).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Plot3DPanel p = new Plot3DPanel(); - XYZ = new double[100][3]; - for (int j = 0; j < XYZ.length; j++) { - XYZ[j][0] = 2*Math.PI*(double)j/XYZ.length; - XYZ[j][1] = Math.sin(XYZ[j][0]); - XYZ[j][2] = Math.sin(XYZ[j][0])*Math.cos(XYZ[j][1]); - } - p.addLinePlot("toto" , XYZ); + XYZ = new double[100][3]; + for (int j = 0; j < XYZ.length; j++) { + XYZ[j][0] = 2*Math.PI*(double)j/XYZ.length; + XYZ[j][1] = Math.sin(XYZ[j][0]); + XYZ[j][2] = Math.sin(XYZ[j][0])*Math.cos(XYZ[j][1]); + } + p.addLinePlot("toto" , XYZ); + + p.setLegendOrientation(PlotPanel.SOUTH); + new FrameView(p).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + + Color[] c = new Color[100]; + + p2 = new Plot2DPanel(); + + XYZ = new double[100][2]; + for (int j = 0; j < XYZ.length; j++) { + XYZ[j][0] = 2*Math.PI*(double)j/XYZ.length; + XYZ[j][1] = Math.sin(XYZ[j][0]); + c[j] = new Color((int)(Math.random() * 0x1000000)); + } + p2.addLinePlot("sin", c, XYZ); + + p2.setLegendOrientation(PlotPanel.SOUTH); + new FrameView(p2).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + + p = new Plot3DPanel(); + + XYZ = new double[100][3]; + for (int j = 0; j < XYZ.length; j++) { + XYZ[j][0] = 2*Math.PI*(double)j/XYZ.length; + XYZ[j][1] = Math.sin(XYZ[j][0]); + XYZ[j][2] = Math.sin(XYZ[j][0])*Math.cos(XYZ[j][1]); + c[j] = new Color((int)(Math.random() * 0x1000000)); + } + p.addLinePlot("toto", c, XYZ); p.setLegendOrientation(PlotPanel.SOUTH); new FrameView(p).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); diff --git a/src/main/java/org/math/plot/plots/Plot.java b/src/main/java/org/math/plot/plots/Plot.java index 9310d1b..0361948 100644 --- a/src/main/java/org/math/plot/plots/Plot.java +++ b/src/main/java/org/math/plot/plots/Plot.java @@ -25,7 +25,7 @@ public abstract class Plot implements Plotable, Noteable, Editable { public String name; - public Color color; + public Color[] colors; public boolean visible = true; public LinkedList layers; public boolean noted = false; @@ -33,9 +33,16 @@ public abstract class Plot implements Plotable, Noteable, Editable { //public boolean forcenoted = false; public int note_precision = 5; + public Plot(String n, Color[] c) { + name = n; + colors = c; + layers = new LinkedList(); + + } + public Plot(String n, Color c) { name = n; - color = c; + colors = new Color[] { c }; layers = new LinkedList(); } @@ -125,18 +132,27 @@ public String getName() { * public String getType() { return type; } */ public Color getColor() { - return color; + return colors[0]; } public void setColor(Color c) { - color = c; + colors[0] = c; + } + + public Color[] getColors() { + return colors; + } + + public void setColors(Color[] c) { + colors = c; } public abstract double[] isSelected(int[] screenCoordTest, AbstractDrawer draw); public void note(AbstractDrawer draw) { - plot(draw, PlotCanvas.NOTE_COLOR); - plotLayerPlots(draw, PlotCanvas.NOTE_COLOR); + Color[] c = new Color [] { PlotCanvas.NOTE_COLOR }; + plot(draw, c); + plotLayerPlots(draw, c); } public void noteCoord(AbstractDrawer draw, double[] coordNoted) { @@ -149,16 +165,16 @@ public void noteCoord(AbstractDrawer draw, double[] coordNoted) { draw.drawShadowedText(Array.cat("\n", draw.canvas.reverseMapedData(coordNoted)), .5f, coordNoted); } - public abstract void plot(AbstractDrawer draw, Color c); + public abstract void plot(AbstractDrawer draw, Color[] c); public void plot(AbstractDrawer draw) { //if (layers.size() > 0) - plotLayerPlots(draw, color); + plotLayerPlots(draw, colors); //else - plot(draw, color); + plot(draw, colors); } - public void plotLayerPlots(AbstractDrawer draw, Color c) { + public void plotLayerPlots(AbstractDrawer draw, Color[] c) { for (int i = 0; i < layers.size(); i++) { layers.get(i).plot(draw, c); } @@ -170,8 +186,9 @@ public void edit(Object src) { } public void editnote(AbstractDrawer draw) { - plot(draw, PlotCanvas.EDIT_COLOR); - plotLayerPlots(draw, PlotCanvas.EDIT_COLOR); + Color[] c = new Color [] { PlotCanvas.NOTE_COLOR }; + plot(draw, c); + plotLayerPlots(draw, c); } public DataPanel datapanel = null; public PlotCanvas plotCanvas; diff --git a/src/main/java/org/math/plot/plots/QuantileLayerPlot.java b/src/main/java/org/math/plot/plots/QuantileLayerPlot.java index 336baf9..c1864de 100644 --- a/src/main/java/org/math/plot/plots/QuantileLayerPlot.java +++ b/src/main/java/org/math/plot/plots/QuantileLayerPlot.java @@ -76,41 +76,43 @@ public double getQuantileRate() { return quantileRate; } - public void plot(AbstractDrawer draw, Color c) { + public void plot(AbstractDrawer draw, Color[] c) { if (!plot.visible) return; - draw.setColor(c); - gradC = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255 * (1 - quantileRate))); draw.setLineType(AbstractDrawer.CONTINOUS_LINE); draw.setLineWidth(WIDTH); if (main_data_constant == 0) for (int i = 0; i < plot.getData().length; i++) { + Color c0 = (c.length > 1 ? c[i] : c[0]); + gradC = new Color(c0.getRed(), c0.getGreen(), c0.getBlue(), (int) (255 * (1 - quantileRate))); double[] d = Array.getRowCopy(plot.getData(), i); d[axe] += Q[i];///quantileRate; - draw.setGradient(plot.getData()[i], c, d, gradC); + draw.setGradient(plot.getData()[i], c0, d, gradC); draw.drawLine(plot.getData()[i], d); // draw.drawDot(d, RADIUS/*(int)(RADIUS*quantileRate)*/); if (symetric) { d[axe] -= 2 * Q[i];///quantileRate; - draw.setGradient(plot.getData()[i], c, d, gradC); + draw.setGradient(plot.getData()[i], c0, d, gradC); draw.drawLine(plot.getData()[i], d); // draw.drawDot(d, RADIUS/*(int)(RADIUS*quantileRate)*/); } } else for (int i = 0; i < plot.getData().length; i++) { + Color c0 = (c.length > 1 ? c[i] : c[0]); + gradC = new Color(c0.getRed(), c0.getGreen(), c0.getBlue(), (int) (255 * (1 - quantileRate))); double[] d = Array.getRowCopy(plot.getData(), i); d[axe] += main_data_constant;///quantileRate; - draw.setGradient(plot.getData()[i], c, d, gradC); + draw.setGradient(plot.getData()[i], c0, d, gradC); draw.drawLine(plot.getData()[i], d); // draw.drawDot(d, shape/*RADIUS/*(int)(RADIUS*quantileRate)*/); if (symetric) { d[axe] -= 2 * main_data_constant;///quantileRate; - draw.setGradient(plot.getData()[i], c, d, gradC); + draw.setGradient(plot.getData()[i], c0, d, gradC); draw.drawLine(plot.getData()[i], d); // draw.drawDot(d, RADIUS/*(int)(RADIUS*quantileRate)*/); } @@ -142,5 +144,18 @@ public static void main(String[] args) { } p2.addQuantiletoPlot(0, 1, 1.0, true, 0.2); new FrameView(p2).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + + Color[] c = new Color[10]; + p2 = new Plot2DPanel(); + double[][] XYZ = new double[10][2]; + for (int j = 0; j < XYZ.length; j++) { + XYZ[j][0] = /*1 + */Math.random(); + XYZ[j][1] = /*100 * */Math.random(); + c[j] = new Color((int)(Math.random() * 0x1000000)); + } + p2.addScatterPlot("toto", c, XYZ); + p2.addQuantiletoPlot(0, 1, 1.0, true, 0.2); + new FrameView(p2).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } \ No newline at end of file diff --git a/src/main/java/org/math/plot/plots/ScatterPlot.java b/src/main/java/org/math/plot/plots/ScatterPlot.java index 577a37f..f954bae 100644 --- a/src/main/java/org/math/plot/plots/ScatterPlot.java +++ b/src/main/java/org/math/plot/plots/ScatterPlot.java @@ -20,6 +20,10 @@ public class ScatterPlot extends Plot { private String[] tags; public ScatterPlot(String n, Color c, boolean[][] _pattern, double[][] _XY) { + this(n, new Color[] { c }, _pattern, _XY); + } + + public ScatterPlot(String n, Color[] c, boolean[][] _pattern, double[][] _XY) { super(n, c); XY = _XY; use_pattern = true; @@ -27,6 +31,10 @@ public ScatterPlot(String n, Color c, boolean[][] _pattern, double[][] _XY) { } public ScatterPlot(String n, Color c, int _type, int _radius, double[][] _XY) { + this(n, new Color[] { c }, _type, _radius, _XY); + } + + public ScatterPlot(String n, Color[] c, int _type, int _radius, double[][] _XY) { super(n, c); XY = _XY; use_pattern = false; @@ -38,12 +46,23 @@ public ScatterPlot(String n, Color c, double[][] _XY) { this(n, c, AbstractDrawer.ROUND_DOT, AbstractDrawer.DEFAULT_DOT_RADIUS, _XY); } - public void plot(AbstractDrawer draw, Color c) { + public ScatterPlot(String n, Color[] c, double[][] _XY) { + this(n, c, AbstractDrawer.ROUND_DOT, AbstractDrawer.DEFAULT_DOT_RADIUS, _XY); + } + + public void plot(AbstractDrawer draw, Color[] c) { if (!visible) { return; } + + boolean monoColor = false; + if (c.length == 1) { + monoColor = true; + } + else if (c.length != XY.length) { + throw new IllegalArgumentException("Color array length must match length of data array. "); + } - draw.setColor(c); if (use_pattern) { draw.setDotType(AbstractDrawer.PATTERN_DOT); draw.setDotPattern(pattern); @@ -57,6 +76,7 @@ public void plot(AbstractDrawer draw, Color c) { } for (int i = 0; i < XY.length; i++) { + draw.setColor(monoColor ? c[0] : c[i]); draw.drawDot(XY[i]); } } @@ -127,6 +147,36 @@ public static void main(String[] args) { } ((ScatterPlot) p.getPlot(0)).setTags(tags); + p.setLegendOrientation(PlotPanel.SOUTH); + new FrameView(p).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + + Color[] c = new Color[10]; + p2 = new Plot2DPanel(); + double[][] XYZ = new double[10][2]; + for (int j = 0; j < XYZ.length; j++) { + XYZ[j][0] = /*1 + */ Math.random(); + XYZ[j][1] = /*100 * */ Math.random(); + c[j] = new Color((int)(Math.random() * 0x1000000)); + } + p2.addScatterPlot("toto", c, XYZ); + + p2.setLegendOrientation(PlotPanel.SOUTH); + new FrameView(p2).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + p = new Plot3DPanel(); + XYZ = new double[10][3]; + tags = new String[10]; + for (int j = 0; j < XYZ.length; j++) { + XYZ[j][0] = /*1 +*/ 2.5 * Math.random(); + XYZ[j][1] = /*100 **/ Math.random(); + XYZ[j][2] = /*0.0001 **/ Math.random(); + tags[j] = "tags " + j; + c[j] = new Color((int)(Math.random() * 0x1000000)); + } + p.addScatterPlot("toto", c, XYZ); + ((ScatterPlot) p.getPlot(0)).setTags(tags); + p.setLegendOrientation(PlotPanel.SOUTH); new FrameView(p).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } diff --git a/src/main/java/org/math/plot/plots/StaircasePlot.java b/src/main/java/org/math/plot/plots/StaircasePlot.java index f99dcef..c811eb5 100644 --- a/src/main/java/org/math/plot/plots/StaircasePlot.java +++ b/src/main/java/org/math/plot/plots/StaircasePlot.java @@ -19,22 +19,36 @@ public StaircasePlot(String n, Color c, int _type, int _radius, double[][] _XY) super(n, c, _type, _radius, _XY); } + public StaircasePlot(String n, Color[] c, int _type, int _radius, double[][] _XY) { + super(n, c, _type, _radius, _XY); + } + public StaircasePlot(String n, Color c, double[][] _XY) { super(n, c, _XY); } - public void plot(AbstractDrawer draw, Color c) { + public StaircasePlot(String n, Color[] c, double[][] _XY) { + super(n, c, _XY); + } + + public void plot(AbstractDrawer draw, Color[] c) { if (!visible) return; - - //System.out.println(Array.toString(XY)); + + boolean monoColor = false; + if (c.length == 1) { + monoColor = true; + } + else if (c.length != XY.length) { + throw new IllegalArgumentException("Color array length must match length of data array. "); + } - draw.setColor(c); draw.setLineType(AbstractDrawer.CONTINOUS_LINE); for (int i = 0; i < XY.length - 1; i++) { double[] begin = XY[i].clone(); double[] end = XY[i + 1].clone(); end[end.length - 1] = XY[i][end.length - 1]; + draw.setColor(monoColor ? c[0] : c[i]); draw.drawLine(begin, end); } @@ -55,14 +69,29 @@ public void plot(AbstractDrawer draw, Color c) { public static void main(String[] args) { Plot2DPanel p = new Plot2DPanel(); - double[] X = new double[10]; - double[] Y = new double[10]; - for (int j = 0; j < X.length; j++) { - X[j] = j; - Y[j] = Math.random(); - } - p.addStaircasePlot("toto", X,Y); + double[] X = new double[10]; + double[] Y = new double[10]; + for (int j = 0; j < X.length; j++) { + X[j] = j; + Y[j] = Math.random(); + } + p.addStaircasePlot("toto", X,Y); + + new FrameView(p).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + Color[] c = new Color[10]; + p = new Plot2DPanel(); + + X = new double[10]; + Y = new double[10]; + for (int j = 0; j < X.length; j++) { + X[j] = j; + Y[j] = Math.random(); + c[j] = new Color((int)(Math.random() * 0x1000000)); + } + p.addStaircasePlot("toto", c, X,Y); + new FrameView(p).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } diff --git a/src/main/java/org/math/plot/plots/VectorLayerPlot.java b/src/main/java/org/math/plot/plots/VectorLayerPlot.java index 8dc7fe7..67c0e9e 100644 --- a/src/main/java/org/math/plot/plots/VectorLayerPlot.java +++ b/src/main/java/org/math/plot/plots/VectorLayerPlot.java @@ -43,12 +43,10 @@ public double[][] getData() { return V; } - public void plot(AbstractDrawer draw, Color c) { + public void plot(AbstractDrawer draw, Color[] c) { if (!plot.visible) return; - draw.setColor(c); - draw.setLineType(AbstractDrawer.CONTINOUS_LINE); for (int i = 0; i < plot.getData().length; i++) { @@ -56,6 +54,7 @@ public void plot(AbstractDrawer draw, Color c) { for (int j = 0; j < d.length; j++) { d[j] += V[i][j]; } + draw.setColor(c.length > 1 ? c[i] : c[0]); draw.drawLine(plot.getData()[i], d); //TODO: draw arrow at position d @@ -78,5 +77,21 @@ public static void main(String[] args) { p2.addVectortoPlot(0, dXYZ); new FrameView(p2).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + + Color[] c = new Color[100]; + p2 = new Plot2DPanel(); + + for (int j = 0; j < XYZ.length; j++) { + XYZ[j][0] = Math.random()*10; + XYZ[j][1] = Math.random()*10; + dXYZ[j][0] = 1.0/Math.sqrt(1+Math.log(XYZ[j][0])*Math.log(XYZ[j][0])); + dXYZ[j][1] = Math.log(XYZ[j][0])/Math.sqrt(1+Math.log(XYZ[j][0])*Math.log(XYZ[j][0])); + c[j] = new Color((int)(Math.random() * 0x1000000)); + } + p2.addScatterPlot("toto", c, XYZ); + + p2.addVectortoPlot(0, dXYZ); + new FrameView(p2).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } \ No newline at end of file