00001 package org.math.plot.plots;
00002
00003 import java.awt.*;
00004
00005 import org.math.plot.*;
00006 import org.math.plot.render.*;
00007
00008 public class HistogramPlot2D extends Plot {
00009
00010 double[][] topLeft;
00011
00012 double[][] topRight;
00013
00014 double[][] bottomLeft;
00015
00016 double[][] bottomRight;
00017
00018 double[] widths;
00019
00020 double width_constant = -1;
00021
00022 double offsetCenter_perWidth;
00023
00024 double factorWidth;
00025
00026 boolean autowidth;
00027
00028 boolean fill_shape = true;
00029
00030 double[][] XY;
00031
00032 public HistogramPlot2D(String n, Color c, double[][] _XY, double w) {
00033 this(n, c, _XY, w, 0.5, 1);
00034 }
00035
00036 public HistogramPlot2D(String n, Color c, double[][] _XY, double[] w) {
00037 this(n, c, _XY, w, 0.5, 1);
00038 }
00039
00040
00041
00042 public HistogramPlot2D(String n, Color c, double[][] _XY, double w, double _offsetCenter_perWidth, double _factorWidth) {
00043 super(n, c);
00044 XY = _XY;
00045 width_constant = w;
00046
00047 autowidth = false;
00048 offsetCenter_perWidth = _offsetCenter_perWidth;
00049 factorWidth = _factorWidth;
00050
00051 build();
00052 }
00053
00054 public HistogramPlot2D(String n, Color c, double[][] _XY, double[] w, double _offsetCenter_perWidth, double _factorWidth) {
00055 super(n, c);
00056 XY = _XY;
00057 widths = w;
00058
00059 autowidth = false;
00060 offsetCenter_perWidth = _offsetCenter_perWidth;
00061 factorWidth = _factorWidth;
00062
00063 build();
00064 }
00065
00066 private void build() {
00067 if (width_constant > 0) {
00068 topLeft = new double[XY.length][];
00069 topRight = new double[XY.length][];
00070 bottomLeft = new double[XY.length][];
00071 bottomRight = new double[XY.length][];
00072 for (int i = 0; i < XY.length; i++) {
00073 topLeft[i] = new double[] { XY[i][0] - factorWidth * width_constant / 2 + (offsetCenter_perWidth - 0.5) * width_constant, XY[i][1] };
00074 topRight[i] = new double[] { XY[i][0] + factorWidth * width_constant / 2 + (offsetCenter_perWidth - 0.5) * width_constant, XY[i][1] };
00075 bottomLeft[i] = new double[] { XY[i][0] - factorWidth * width_constant / 2 + (offsetCenter_perWidth - 0.5) * width_constant, 0 };
00076 bottomRight[i] = new double[] { XY[i][0] + factorWidth * width_constant / 2 + (offsetCenter_perWidth - 0.5) * width_constant, 0 };
00077 }
00078 } else {
00079 topLeft = new double[XY.length][];
00080 topRight = new double[XY.length][];
00081 bottomLeft = new double[XY.length][];
00082 bottomRight = new double[XY.length][];
00083 for (int i = 0; i < XY.length; i++) {
00084 topLeft[i] = new double[] { XY[i][0] - factorWidth * widths[i] / 2 + (offsetCenter_perWidth - 0.5) * widths[i], XY[i][1] };
00085 topRight[i] = new double[] { XY[i][0] + factorWidth * widths[i] / 2 + (offsetCenter_perWidth - 0.5) * widths[i], XY[i][1] };
00086 bottomLeft[i] = new double[] { XY[i][0] - factorWidth * widths[i] / 2 + (offsetCenter_perWidth - 0.5) * widths[i], 0 };
00087 bottomRight[i] = new double[] { XY[i][0] + factorWidth * widths[i] / 2 + (offsetCenter_perWidth - 0.5) * widths[i], 0 };
00088 }
00089 }
00090 }
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 public void plot(AbstractDrawer draw, Color c) {
00126 if (!visible)
00127 return;
00128
00129 draw.canvas.includeInBounds(bottomLeft[0]);
00130 draw.canvas.includeInBounds(topRight[XY.length - 1]);
00131
00132 draw.setColor(c);
00133 draw.setLineType(AbstractDrawer.CONTINOUS_LINE);
00134 for (int i = 0; i < XY.length; i++) {
00135 draw.drawLine(bottomLeft[i], topLeft[i]);
00136 draw.drawLine(topLeft[i], topRight[i]);
00137 draw.drawLine(topRight[i], bottomRight[i]);
00138 draw.drawLine(bottomRight[i], bottomLeft[i]);
00139
00140 if (fill_shape)
00141 draw.fillPolygon(0.2f,bottomLeft[i],topLeft[i],topRight[i],bottomRight[i]);
00142 }
00143 }
00144
00145 @Override
00146 public void setData(double[][] d) {
00147 XY = d;
00148 }
00149
00150 @Override
00151 public double[][] getData() {
00152 return XY;
00153 }
00154
00155 public void setDataWidth(double[] w) {
00156 widths = w;
00157 width_constant = -1;
00158 build();
00159 }
00160
00161 public void setDataWidth(double w) {
00162 width_constant = w;
00163 build();
00164 }
00165
00166 public double[] getDataWidth() {
00167 if (width_constant > 0) {
00168 widths = new double[XY.length];
00169 for (int i = 0; i < widths.length; i++)
00170 widths[i] = width_constant;
00171 }
00172 return widths;
00173 }
00174
00175 public void setData(double[][] d, double[] w) {
00176 XY = d;
00177 setDataWidth(w);
00178 }
00179
00180 public void setData(double[][] d, double w) {
00181 XY = d;
00182 setDataWidth(w);
00183 }
00184
00185 public double[] isSelected(int[] screenCoordTest, AbstractDrawer draw) {
00186 for (int i = 0; i < XY.length; i++) {
00187 int[] screenCoord = draw.project(XY[i]);
00188
00189 if ((screenCoord[0] + note_precision > screenCoordTest[0]) && (screenCoord[0] - note_precision < screenCoordTest[0])
00190 && (screenCoord[1] + note_precision > screenCoordTest[1]) && (screenCoord[1] - note_precision < screenCoordTest[1]))
00191 return XY[i];
00192 }
00193 return null;
00194 }
00195
00196 public static void main(String[] args) {
00197 double[] X = new double[500];
00198 for (int i = 0; i < X.length; i++) {
00199 X[i] = Math.random()+Math.random();
00200 }
00201 Plot2DPanel p = new Plot2DPanel("SOUTH");
00202 p.addHistogramPlot("test", X, 10);
00203 new FrameView(p);
00204 }
00205
00206 }