00001 package org.math.plot.plots;
00002
00003 import java.awt.Color;
00004
00005 import javax.swing.JFrame;
00006
00007 import org.math.plot.FrameView;
00008 import org.math.plot.Plot2DPanel;
00009 import org.math.plot.render.AbstractDrawer;
00010 import org.math.plot.utils.Array;
00011
00016 public class DensityLayerPlot extends LayerPlot {
00017
00018 public static int WIDTH = 2;
00019
00020 int axis;
00021
00022 double[] constant_Q;
00023
00024 double[][] Q;
00025
00026 public DensityLayerPlot(Plot p, int a, double[] quantiles) {
00027 this(p, a, new double[0][0]);
00028 constant_Q = quantiles;
00029 }
00030
00036 public DensityLayerPlot(Plot p, int a, double[][] quantiles) {
00037 super("Density of " + p.name, p);
00038 if (quantiles != null && quantiles.length > 0)
00039 Array.checkRowDimension(quantiles, p.getData().length);
00040 Q = quantiles;
00041 axis = a;
00042 }
00043
00044 public int getAxe() {
00045 return axis;
00046 }
00047
00048 public void plot(AbstractDrawer draw, Color c) {
00049 if (!plot.visible)
00050 return;
00051
00052 draw.setColor(c);
00053
00054 draw.setLineType(AbstractDrawer.CONTINOUS_LINE);
00055 draw.setLineWidth(WIDTH);
00056 if (constant_Q == null)
00057 for (int i = 0; i < plot.getData().length; i++) {
00058
00059 double norm = Double.MAX_VALUE;
00060 for (int j = 0; j < Q[i].length - 1; j++)
00061 norm = Math.min(1 / (Q[i][j + 1] - Q[i][j]), norm);
00062
00063 double[] d0 = Array.getRowCopy(plot.getData(), i);
00064 double[] d1 = Array.getRowCopy(plot.getData(), i);
00065 double[] d2 = Array.getRowCopy(plot.getData(), i);
00066
00067 for (int j = 0; j < Q[i].length - 2; j++) {
00068 d1[axis] = d0[axis] + ((Q[i][j] + Q[i][j + 1]) / 2);
00069 d2[axis] = d0[axis] + ((Q[i][j + 1] + Q[i][j + 2]) / 2);
00070 Color c1 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255.0 * (norm / (Q[i][j + 1] - Q[i][j]))));
00071 Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255.0 * (norm / (Q[i][j + 2] - Q[i][j + 1]))));
00072 draw.setGradient(d1, c1, d2, c2);
00073 draw.drawLine(d1, d2);
00074 }
00075 }
00076 else {
00077
00078 double norm = Double.MAX_VALUE;
00079 for (int j = 0; j < constant_Q.length - 1; j++)
00080 norm = Math.min(1 / (constant_Q[j + 1] - constant_Q[j]), norm);
00081
00082 for (int i = 0; i < plot.getData().length; i++) {
00083 double[] d0 = Array.getRowCopy(plot.getData(), i);
00084 double[] d1 = Array.getRowCopy(plot.getData(), i);
00085 double[] d2 = Array.getRowCopy(plot.getData(), i);
00086
00087 for (int j = 0; j < constant_Q.length - 2; j++) {
00088 d1[axis] = d0[axis] + (constant_Q[j] + constant_Q[j + 1]) / 2;
00089 d2[axis] = d0[axis] + (constant_Q[j + 1] + constant_Q[j + 2]) / 2;
00090 Color c1 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255.0 * (norm / (constant_Q[j + 1] - constant_Q[j]))));
00091 Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (255.0 * (norm / (constant_Q[j + 2] - constant_Q[j + 1]))));
00092 draw.setGradient(d1, c1, d2, c2);
00093 draw.drawLine(d1, d2);
00094 }
00095 }
00096 }
00097 draw.resetGradient();
00098 draw.setLineWidth(AbstractDrawer.DEFAULT_LINE_WIDTH);
00099
00100 }
00101
00102 @Override
00103 public void setData(double[][] d) {
00104
00105 }
00106
00107 @Override
00108 public double[][] getData() {
00109 return null;
00110 }
00111
00112 public static void main(String[] args) {
00113 Plot2DPanel p2 = new Plot2DPanel();
00114 for (int i = 0; i < 2; i++) {
00115 double[][] XYZ = new double[10][2];
00116 for (int j = 0; j < XYZ.length; j++) {
00117 XYZ[j][0] = Math.random();
00118 XYZ[j][1] = 10 * Math.random();
00119 }
00120
00121 p2.addScatterPlot("toto" + i, XYZ);
00122 }
00123 p2.getPlot(0).addQuantiles(1, new double[] {-4, -2, -0.5, 0, 0.5, 2, 4 });
00124 p2.getPlot(1).addQuantiles(1, new double[] { -3, -2, -1, 0, 1, 2, 3 });
00125
00126
00127 new FrameView(p2).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
00128 }
00129 }