00001 package org.math.plot;
00002
00003 import java.awt.BorderLayout;
00004 import java.awt.Color;
00005 import java.awt.Graphics;
00006 import java.awt.Image;
00007 import java.awt.image.BufferedImage;
00008 import java.awt.image.RenderedImage;
00009 import java.io.File;
00010 import java.io.IOException;
00011 import java.util.LinkedList;
00012
00013 import javax.imageio.ImageIO;
00014 import javax.swing.ImageIcon;
00015 import javax.swing.JFrame;
00016 import javax.swing.JPanel;
00017
00018 import org.math.io.files.ASCIIFile;
00019 import org.math.plot.canvas.PlotCanvas;
00020 import org.math.plot.components.LegendPanel;
00021 import org.math.plot.components.PlotToolBar;
00022 import org.math.plot.plotObjects.Axis;
00023 import org.math.plot.plotObjects.Plotable;
00024 import org.math.plot.plots.Plot;
00025 import org.math.plot.utils.Array;
00026
00033 public abstract class PlotPanel extends JPanel {
00034
00035 private static final long serialVersionUID = 1L;
00036
00037 public PlotToolBar plotToolBar;
00038
00039 public PlotCanvas plotCanvas;
00040
00041 public LegendPanel plotLegend;
00042
00043 public final static String EAST = BorderLayout.EAST;
00044
00045 public final static String SOUTH = BorderLayout.SOUTH;
00046
00047 public final static String NORTH = BorderLayout.NORTH;
00048
00049 public final static String WEST = BorderLayout.WEST;
00050
00051 public final static String INVISIBLE = "INVISIBLE";
00052
00053 public final static String SCATTER = "SCATTER";
00054
00055 public final static String LINE = "LINE";
00056
00057 public final static String BAR = "BAR";
00058
00059 public final static String HISTOGRAM = "HISTOGRAM";
00060
00061 public final static String BOX = "BOX";
00062
00063 public final static String STAIRCASE = "STAIRCASE";
00064
00065 public final static String GRID = "GRID";
00066
00067 public final static Color[] COLORLIST = { Color.BLUE, Color.RED, Color.GREEN, Color.YELLOW, Color.ORANGE, Color.PINK, Color.CYAN, Color.MAGENTA };
00068
00069 public PlotPanel(PlotCanvas _canvas, String legendOrientation) {
00070 plotCanvas = _canvas;
00071 setBackground(Color.WHITE);
00072 setLayout(new BorderLayout());
00073
00074 addPlotToolBar(NORTH);
00075
00076 addLegend(legendOrientation);
00077
00078 add(plotCanvas, BorderLayout.CENTER);
00079 }
00080
00081 public PlotPanel(PlotCanvas _canvas) {
00082 this(_canvas, INVISIBLE);
00083 }
00084
00095 public void addLegend(String location) {
00096 if (location.equalsIgnoreCase(EAST)) {
00097 plotLegend = new LegendPanel(this, LegendPanel.VERTICAL);
00098 add(plotLegend, EAST);
00099 } else if (location.equalsIgnoreCase(SOUTH)) {
00100 plotLegend = new LegendPanel(this, LegendPanel.HORIZONTAL);
00101 add(plotLegend, SOUTH);
00102 } else if (location.equalsIgnoreCase(WEST)) {
00103 plotLegend = new LegendPanel(this, LegendPanel.VERTICAL);
00104 add(plotLegend, WEST);
00105 } else if (location.equalsIgnoreCase(NORTH)) {
00106 plotLegend = new LegendPanel(this, LegendPanel.HORIZONTAL);
00107 add(plotLegend, NORTH);
00108 } else if (location.equalsIgnoreCase(INVISIBLE)) {
00109 plotLegend = new LegendPanel(this, LegendPanel.INVISIBLE);
00110
00111 } else
00112 System.err.println("Orientation " + location + " is unknonw.");
00113 }
00114
00118 public void removeLegend() {
00119 remove(plotLegend);
00120 }
00121
00131 public void setLegendOrientation(String location) {
00132 removeLegend();
00133 addLegend(location);
00134 }
00135
00145 public void addPlotToolBar(String location) {
00146 if (location.equalsIgnoreCase(EAST)) {
00147 removePlotToolBar();
00148 plotToolBar = new PlotToolBar(this);
00149 plotToolBar.setFloatable(false);
00150 add(plotToolBar, EAST);
00151 } else if (location.equalsIgnoreCase(SOUTH)) {
00152 removePlotToolBar();
00153 plotToolBar = new PlotToolBar(this);
00154 plotToolBar.setFloatable(false);
00155 add(plotToolBar, SOUTH);
00156 } else if (location.equalsIgnoreCase(WEST)) {
00157 removePlotToolBar();
00158 plotToolBar = new PlotToolBar(this);
00159 plotToolBar.setFloatable(false);
00160 add(plotToolBar, WEST);
00161 } else if (location.equalsIgnoreCase(NORTH)) {
00162 removePlotToolBar();
00163 plotToolBar = new PlotToolBar(this);
00164 plotToolBar.setFloatable(false);
00165 add(plotToolBar, NORTH);
00166 } else
00167 System.err.println("Location " + location + " is unknonw.");
00168 }
00169
00173 public void removePlotToolBar() {
00174 if (plotToolBar == null)
00175 return;
00176 remove(plotToolBar);
00177 }
00178
00187 public void setPlotToolBarOrientation(String location) {
00188 addPlotToolBar(location);
00189 }
00190
00191
00192
00193
00194
00195 public void setActionMode(int am) {
00196 plotCanvas.setActionMode(am);
00197 }
00198
00199 public void setNoteCoords(boolean b) {
00200 plotCanvas.setNoteCoords(b);
00201 }
00202
00203 public void setEditable(boolean b) {
00204 plotCanvas.setEditable(b);
00205 }
00206
00207 public boolean getEditable() {
00208 return plotCanvas.getEditable();
00209 }
00210
00211 public void setNotable(boolean b) {
00212 plotCanvas.setNotable(b);
00213 }
00214
00215 public boolean getNotable() {
00216 return plotCanvas.getNotable();
00217 }
00218
00219
00220
00221
00222
00223 public LinkedList<Plot> getPlots() {
00224 return plotCanvas.getPlots();
00225 }
00226
00227 public Plot getPlot(int i) {
00228 return plotCanvas.getPlot(i);
00229 }
00230
00231 public int getPlotIndex(Plot p) {
00232 return plotCanvas.getPlotIndex(p);
00233 }
00234
00235 public LinkedList<Plotable> getPlotables() {
00236 return plotCanvas.getPlotables();
00237 }
00238
00239 public Plotable getPlotable(int i) {
00240 return plotCanvas.getPlotable(i);
00241 }
00242
00248 public Axis getAxis(int i) {
00249 return plotCanvas.getGrid().getAxis(i);
00250 }
00251
00257 public String[] getAxisScales() {
00258 return plotCanvas.getAxisScales();
00259 }
00260
00261
00266 public void setAxisLabels(String... labels) {
00267 plotCanvas.setAxisLabels(labels);
00268 }
00269
00275 public void setAxisLabel(int axe, String label) {
00276 plotCanvas.setAxisLabel(axe, label);
00277 }
00278
00283 public void setAxisScales(String... scales) {
00284 plotCanvas.setAxisScales(scales);
00285 }
00286
00292 public void setAxisScale(int axe, String scale) {
00293 plotCanvas.setAxiScale(axe, scale);
00294 }
00295
00301 public void setFixedBounds(double[] min, double[] max) {
00302 plotCanvas.setFixedBounds(min, max);
00303 }
00304
00311 public void setFixedBounds(int axe, double min, double max) {
00312 plotCanvas.setFixedBounds(axe, min, max);
00313 }
00314
00319 public void includeInBounds(double... into) {
00320 plotCanvas.includeInBounds(into);
00321 }
00322
00327 public void includeInBounds(Plot plot) {
00328 plotCanvas.includeInBounds(plot);
00329 }
00330
00334 public void setAutoBounds() {
00335 plotCanvas.setAutoBounds();
00336 }
00337
00342 public void setAutoBounds(int axe) {
00343 plotCanvas.setAutoBounds(axe);
00344 }
00345
00346 public double[][] mapData(Object[][] stringdata) {
00347 return plotCanvas.mapData(stringdata);
00348 }
00349
00350 public void resetMapData() {
00351 plotCanvas.resetMapData();
00352 }
00353
00354
00355
00356
00357
00358 public void addLabel(String text, Color c, double... where) {
00359 plotCanvas.addLabel(text, c, where);
00360 }
00361
00362 public void addBaseLabel(String text, Color c, double... where) {
00363 plotCanvas.addBaseLabel(text, c, where);
00364 }
00365
00366 public void addPlotable(Plotable p) {
00367 plotCanvas.addPlotable(p);
00368 }
00369
00370 public void removePlotable(Plotable p) {
00371 plotCanvas.removePlotable(p);
00372 }
00373
00374 public void removePlotable(int i) {
00375 plotCanvas.removePlotable(i);
00376 }
00377
00378 public int addPlot(Plot newPlot) {
00379 return plotCanvas.addPlot(newPlot);
00380 }
00381
00382 protected Color getNewColor() {
00383 return COLORLIST[plotCanvas.plots.size() % COLORLIST.length];
00384 }
00385
00386 public int addPlot(String type, String name, double[]... v) {
00387 return addPlot(type, name, getNewColor(), v);
00388 }
00389
00390 public abstract int addPlot(String type, String name, Color c, double[]... v);
00391
00392 public void setPlot(int I, Plot p) {
00393 plotCanvas.setPlot(I, p);
00394 }
00395
00396 public void changePlotData(int I, double[]... XY) {
00397 plotCanvas.changePlotData(I, XY);
00398 }
00399
00400 public void changePlotName(int I, String name) {
00401 plotCanvas.changePlotName(I, name);
00402 }
00403
00404 public void changePlotColor(int I, Color c) {
00405 plotCanvas.changePlotColor(I, c);
00406 }
00407
00408 public void removePlot(int I) {
00409 plotCanvas.removePlot(I);
00410 }
00411
00412 public void removePlot(Plot p) {
00413 plotCanvas.removePlot(p);
00414 }
00415
00416 public void removeAllPlots() {
00417 plotCanvas.removeAllPlots();
00418 }
00419
00420 public void addVectortoPlot(int numPlot, double[][] v) {
00421 plotCanvas.addVectortoPlot(numPlot, v);
00422 }
00423
00424 public void addQuantiletoPlot(int numPlot, int numAxe, double rate, boolean symetric, double[] q) {
00425 plotCanvas.addQuantiletoPlot(numPlot, numAxe, rate, symetric, q);
00426 }
00427
00428 public void addQuantiletoPlot(int numPlot, int numAxe, double rate, boolean symetric, double q) {
00429 plotCanvas.addQuantiletoPlot(numPlot, numAxe, rate, symetric, q);
00430 }
00431
00432 public void addQuantilestoPlot(int numPlot, int numAxe, double[][] q) {
00433 plotCanvas.addQuantilestoPlot(numPlot, numAxe, q);
00434 }
00435
00436 public void addQuantilestoPlot(int numPlot, int numAxe, double[] q) {
00437 plotCanvas.addQuantilestoPlot(numPlot, numAxe, q);
00438 }
00439
00440 public void addGaussQuantilestoPlot(int numPlot, int numAxe, double[] s) {
00441 plotCanvas.addGaussQuantilestoPlot(numPlot, numAxe, s);
00442 }
00443
00444 public void addGaussQuantilestoPlot(int numPlot, int numAxe, double s) {
00445 plotCanvas.addGaussQuantilestoPlot(numPlot, numAxe, s);
00446 }
00447
00448 public void toGraphicFile(File file) throws IOException {
00449
00450 plotToolBar.setVisible(false);
00451
00452 Image image = createImage(getWidth(), getHeight());
00453 paint(image.getGraphics());
00454 image = new ImageIcon(image).getImage();
00455
00456 BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
00457 Graphics g = bufferedImage.createGraphics();
00458 g.drawImage(image, 0, 0, Color.WHITE, null);
00459 g.dispose();
00460
00461
00462 plotToolBar.setVisible(true);
00463
00464 try {
00465 ImageIO.write((RenderedImage) bufferedImage, "PNG", file);
00466 } catch (IllegalArgumentException ex) {
00467 }
00468 }
00469
00470 public static void main(String[] args) {
00471 String man = "Usage: jplot.<sh|bat> <-2D|-3D> [-l <INVISIBLE|NORTH|SOUTH|EAST|WEST>] [options] <ASCII file (n rows, m columns)> [[options] other ASCII file]\n"
00472 + "[-l <INVISIBLE|NORTH|SOUTH|EAST|WEST>] giving the legend position\n"
00473 + "[options] are:\n"
00474 + " -t <SCATTER|LINE|BAR|HISTOGRAM2D(<integer h>)|HISTOGRAM3D(<integer h>,<integer k>)|GRID3D|CLOUD2D(<integer h>,<integer k>)|CLOUD3D(<integer h>,<integer k>,<integer l>)> type of the plot\n"
00475 + " SCATTER|LINE|BAR: each line of the ASCII file contains coordinates of one point.\n"
00476 + " HISTOGRAM2D(<integer h>): ASCII file contains the 1D sample (i.e. m=1) to split in h slices.\n"
00477 + " HISTOGRAM3D(<integer h>,<integer k>): ASCII file contains the 2D sample (i.e. m=2) to split in h*k slices (h slices on X axis and k slices on Y axis).\n"
00478 + " GRID3D: ASCII file is a matrix, first row gives n X grid values, first column gives m Y grid values, other values are Z values.\n"
00479 + " CLOUD2D(<integer h>,<integer k>): ASCII file contains the 2D sample (i.e. m=2) to split in h*k slices (h slices on X axis and k slices on Y axis), density of cloud corresponds to frequency of X-Y slice in given 2D sample.\n"
00480 + " CLOUD3D(<integer h>,<integer k>,<integer l>): ASCII file contains the 3D sample (i.e. m=3) to split in h*k*l slices (h slices on X axis, k slices on Y axis, l slices on Y axis), density of cloud corresponds to frequency of X-Y-Z slice in given 3D sample.\n"
00481 + " -n name name of the plot\n"
00482 + " -v <ASCII file (n,3|2)> vector data to add to the plot\n"
00483 + " -q<X|Y|Z>(<float Q>) <ASCII file (n,1)> Q-quantile to add to the plot on <X|Y|Z> axis. Each line of the given ASCII file contains the value of quantile for probvability Q.\n"
00484 + " -qP<X|Y|Z> <ASCII file (n,p)> p-quantiles density to add to the plot on <X|Y|Z> axis. Each line of the given ASCII file contains p values.\n"
00485 + " -qN<X|Y|Z> <ASCII file (n,1)> Gaussian density to add to the plot on <X|Y|Z> axis. Each line of the given ASCII file contains a standard deviation.";
00486
00487 if (args.length == 0) {
00488 double[][] data = new double[20][];
00489 for (int i = 0; i < data.length; i++)
00490 data[i] = new double[] { Math.random(), Math.random(), Math.random() };
00491 ASCIIFile.writeDoubleArray(new File("tmp.dat"), data);
00492
00493 args = new String[] { "-3D", "-l", "SOUTH", "-t", "SCATTER", "tmp.dat" };
00494 System.out.println(man);
00495 System.out.println("\nExample: jplot.<sh|bat> " + Array.cat(args));
00496 }
00497
00498 PlotPanel p = null;
00499 if (args[0].equals("-2D"))
00500 p = new Plot2DPanel();
00501 else if (args[0].equals("-3D"))
00502 p = new Plot3DPanel();
00503 else
00504 System.out.println(man);
00505
00506 try {
00507
00508 String leg = "INVISIBLE";
00509 String type = SCATTER;
00510 String name = "";
00511
00512 double[][] v = null;
00513
00514 double[] qX = null;
00515 double[] qY = null;
00516 double[] qZ = null;
00517 double qXp = 0;
00518 double qYp = 0;
00519 double qZp = 0;
00520
00521 double[][] qPX = null;
00522 double[][] qPY = null;
00523 double[][] qPZ = null;
00524
00525 double[] qNX = null;
00526 double[] qNY = null;
00527 double[] qNZ = null;
00528
00529 for (int i = 1; i < args.length; i++) {
00530
00531 if (args[i].equals("-l")) {
00532 leg = args[i + 1];
00533 i++;
00534 } else if (args[i].equals("-t")) {
00535 type = args[i + 1];
00536 i++;
00537 } else if (args[i].equals("-n")) {
00538 name = args[i + 1];
00539 i++;
00540 } else if (args[i].equals("-v")) {
00541 v = ASCIIFile.readDoubleArray(new File(args[i + 1]));
00542 i++;
00543 } else if (args[i].startsWith("-qX(")) {
00544 qX = ASCIIFile.readDouble1DArray(new File(args[i + 1]));
00545 qXp = Double.parseDouble(args[i].substring(4, args[i].length() - 1));
00546 i++;
00547 } else if (args[i].startsWith("-qY(")) {
00548 qY = ASCIIFile.readDouble1DArray(new File(args[i + 1]));
00549 qYp = Double.parseDouble(args[i].substring(4, args[i].length() - 1));
00550 i++;
00551 } else if (args[i].startsWith("-qZ(")) {
00552 qZ = ASCIIFile.readDouble1DArray(new File(args[i + 1]));
00553 qZp = Double.parseDouble(args[i].substring(4, args[i].length() - 1));
00554 i++;
00555 } else if (args[i].equals("-qPX")) {
00556 qPX = ASCIIFile.readDoubleArray(new File(args[i + 1]));
00557 i++;
00558 } else if (args[i].equals("-qPY")) {
00559 qPY = ASCIIFile.readDoubleArray(new File(args[i + 1]));
00560 i++;
00561 } else if (args[i].equals("-qPZ")) {
00562 qPZ = ASCIIFile.readDoubleArray(new File(args[i + 1]));
00563 i++;
00564 } else if (args[i].equals("-qNX")) {
00565 qNX = ASCIIFile.readDouble1DArray(new File(args[i + 1]));
00566 i++;
00567 } else if (args[i].equals("-qNY")) {
00568 qNY = ASCIIFile.readDouble1DArray(new File(args[i + 1]));
00569 i++;
00570 } else if (args[i].equals("-qNZ")) {
00571 qNZ = ASCIIFile.readDouble1DArray(new File(args[i + 1]));
00572 i++;
00573 } else {
00574 File input_file = new File(args[i]);
00575 int n = 0;
00576 if (input_file.exists()) {
00577 if (name.length() == 0)
00578 name = input_file.getName();
00579
00580 if (p instanceof Plot2DPanel) {
00581 Plot2DPanel p2d = (Plot2DPanel) p;
00582 if (type.equals("SCATTER"))
00583 n = p2d.addScatterPlot(name, ASCIIFile.readDoubleArray(input_file));
00584 else if (type.equals("LINE"))
00585 n = p2d.addLinePlot(name, ASCIIFile.readDoubleArray(input_file));
00586 else if (type.equals("BAR"))
00587 n = p2d.addBarPlot(name, ASCIIFile.readDoubleArray(input_file));
00588 else if (type.startsWith("HISTOGRAM2D(")) {
00589 n = p2d
00590 .addHistogramPlot(name, ASCIIFile.readDouble1DArray(input_file), Integer
00591 .parseInt(type.substring(12, type.length() - 1)));
00592 } else if (type.startsWith("CLOUD2D(")) {
00593 n = p2d.addCloudPlot(name, ASCIIFile.readDoubleArray(input_file), Integer.parseInt(type.substring(8, type.indexOf(","))),
00594 Integer.parseInt(type.substring(type.indexOf(",") + 1, type.length() - 1)));
00595 } else
00596 p2d.addPlot(type, name, ASCIIFile.readDoubleArray(input_file));
00597 } else {
00598 Plot3DPanel p3d = (Plot3DPanel) p;
00599 if (type.equals("SCATTER"))
00600 n = p3d.addScatterPlot(name, ASCIIFile.readDoubleArray(input_file));
00601 else if (type.equals("LINE"))
00602 n = p3d.addLinePlot(name, ASCIIFile.readDoubleArray(input_file));
00603 else if (type.equals("BAR"))
00604 n = p3d.addBarPlot(name, ASCIIFile.readDoubleArray(input_file));
00605 else if (type.startsWith("HISTOGRAM3D(")) {
00606 n = p3d.addHistogramPlot(name, ASCIIFile.readDoubleArray(input_file), Integer.parseInt(type.substring(12, type.indexOf(","))),
00607 Integer.parseInt(type.substring(type.indexOf(",") + 1, type.length() - 1)));
00608 } else if (type.equals("GRID3D")) {
00609 n = p3d.addGridPlot(name, ASCIIFile.readDoubleArray(input_file));
00610 } else if (type.startsWith("CLOUD3D(")) {
00611 n = p3d.addCloudPlot(name, ASCIIFile.readDoubleArray(input_file), Integer.parseInt(type.substring(8, type.indexOf(","))),
00612 Integer.parseInt(type.substring(type.indexOf(",") + 1, type.indexOf(",", type.indexOf(",") + 1))), Integer
00613 .parseInt(type.substring(type.indexOf(",", type.indexOf(",") + 1) + 1, type.length() - 1)));
00614 } else
00615 p3d.addPlot(type, name, ASCIIFile.readDoubleArray(input_file));
00616 }
00617
00618 if (v != null)
00619 p.addVectortoPlot(n, v);
00620
00621 if (qX != null)
00622 p.addQuantiletoPlot(n, 0, qXp, false, qX);
00623 if (qY != null)
00624 p.addQuantiletoPlot(n, 1, qYp, false, qY);
00625 if (qZ != null)
00626 p.addQuantiletoPlot(n, 2, qZp, false, qZ);
00627
00628 if (qPX != null)
00629 p.addQuantilestoPlot(n, 0, qPX);
00630 if (qPY != null)
00631 p.addQuantilestoPlot(n, 1, qPY);
00632 if (qPZ != null)
00633 p.addQuantilestoPlot(n, 2, qPZ);
00634
00635 if (qNX != null)
00636 p.addGaussQuantilestoPlot(n, 0, qNX);
00637 if (qNY != null)
00638 p.addGaussQuantilestoPlot(n, 1, qNY);
00639 if (qNZ != null)
00640 p.addGaussQuantilestoPlot(n, 2, qNZ);
00641
00642 type = "SCATTER";
00643 leg = "SOUTH";
00644 name = "";
00645 qX = null;
00646 qY = null;
00647 qZ = null;
00648 qXp = 0;
00649 qYp = 0;
00650 qZp = 0;
00651
00652 v = null;
00653
00654 qPX = null;
00655 qPY = null;
00656 qPZ = null;
00657
00658 qNX = null;
00659 qNY = null;
00660 qNZ = null;
00661
00662 } else {
00663 System.out.println("File " + args[i] + " unknown.");
00664 System.out.println(man);
00665 }
00666 }
00667 }
00668 p.setLegendOrientation(leg);
00669 FrameView f = new FrameView(p);
00670 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
00671 } catch (Exception e) {
00672 e.printStackTrace();
00673 System.err.println("\n" + man);
00674 }
00675 }
00676 }