src/org/math/plot/plots/ScatterPlot.java

00001 package org.math.plot.plots;
00002 
00003 import java.awt.*;
00004 
00005 import javax.swing.*;
00006 
00007 import org.math.plot.*;
00008 import org.math.plot.render.*;
00009 
00010 public class ScatterPlot extends Plot {
00011 
00012         private int type;
00013 
00014         private int radius;
00015 
00016         private boolean[][] pattern;
00017 
00018         private boolean use_pattern;
00019 
00020         double[][] XY;
00021 
00022         public ScatterPlot(String n, Color c, boolean[][] _pattern, double[][] _XY) {
00023                 super(n, c);
00024                 XY = _XY;
00025                 use_pattern = true;
00026                 pattern = _pattern;
00027         }
00028 
00029         public ScatterPlot(String n, Color c, int _type, int _radius, double[][] _XY) {
00030                 super(n, c);
00031                 XY = _XY;
00032                 use_pattern = false;
00033                 type = _type;
00034                 radius = _radius;
00035         }
00036 
00037         public ScatterPlot(String n, Color c, double[][] _XY) {
00038                 this(n, c, AbstractDrawer.ROUND_DOT, AbstractDrawer.DEFAULT_DOT_RADIUS, _XY);
00039         }
00040 
00041         public void plot(AbstractDrawer draw, Color c) {
00042                 if (!visible)
00043                         return;
00044 
00045                 draw.setColor(c);
00046                 if (use_pattern) {
00047                         draw.setDotType(AbstractDrawer.PATTERN_DOT);
00048                         draw.setDotPattern(pattern);
00049                 } else {
00050                         draw.setDotRadius(radius);
00051                         if (type == AbstractDrawer.CROSS_DOT)
00052                                 draw.setDotType(AbstractDrawer.CROSS_DOT);
00053                         else
00054                                 draw.setDotType(AbstractDrawer.ROUND_DOT);
00055                 }
00056 
00057                 for (int i = 0; i < XY.length; i++)
00058                         draw.drawDot(XY[i]);
00059         }
00060 
00061         public void setDotPattern(int t) {
00062                 type = t;
00063                 use_pattern = false;
00064         }
00065 
00066         public void setDotPattern(boolean[][] t) {
00067                 use_pattern = true;
00068                 pattern = t;
00069         }
00070 
00071         @Override
00072         public void setData(double[][] d) {
00073                 XY = d;
00074         }
00075 
00076         @Override
00077         public double[][] getData() {
00078                 return XY;
00079         }
00080 
00081         public double[] isSelected(int[] screenCoordTest, AbstractDrawer draw) {
00082                 for (int i = 0; i < XY.length; i++) {
00083                         int[] screenCoord = draw.project(XY[i]);
00084 
00085                         if ((screenCoord[0] + note_precision > screenCoordTest[0]) && (screenCoord[0] - note_precision < screenCoordTest[0])
00086                                         && (screenCoord[1] + note_precision > screenCoordTest[1]) && (screenCoord[1] - note_precision < screenCoordTest[1]))
00087                                 return XY[i];
00088                 }
00089                 return null;
00090         }
00091 
00092         public static void main(String[] args) {
00093                 Plot2DPanel p2 = new Plot2DPanel();
00094                 for (int i = 0; i < 3; i++) {
00095                         double[][] XYZ = new double[10][2];
00096                         for (int j = 0; j < XYZ.length; j++) {
00097                                 XYZ[j][0] = /*1 + */Math.random();
00098                                 XYZ[j][1] = /*100 * */Math.random();
00099                         }
00100                         p2.addScatterPlot("toto" + i, XYZ);
00101                 }
00102 
00103                 p2.setLegendOrientation(PlotPanel.SOUTH);
00104                 new FrameView(p2).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
00105 
00106                 Plot3DPanel p = new Plot3DPanel();
00107                 for (int i = 0; i < 3; i++) {
00108                         double[][] XYZ = new double[10][3];
00109                         for (int j = 0; j < XYZ.length; j++) {
00110                                 XYZ[j][0] = /*1 +*/Math.random();
00111                                 XYZ[j][1] = /*100 **/Math.random();
00112                                 XYZ[j][2] = /*0.0001 **/Math.random();
00113                         }
00114                         p.addScatterPlot("toto" + i, XYZ);
00115                 }
00116 
00117                 p.setLegendOrientation(PlotPanel.SOUTH);
00118                 new FrameView(p).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
00119         }
00120 
00121 }

Generated on Wed Sep 5 21:44:02 2007 for jmathplot by  doxygen 1.5.1