1 package org.catacomb.dataview;
2
3 import java.awt.Color;
4 import java.io.File;
5 import java.util.ArrayList;
6 import java.util.Collections;
7 import java.util.HashMap;
8
9 import org.catacomb.datalish.Box;
10 import org.catacomb.graph.gui.PaintInstructor;
11 import org.catacomb.graph.gui.Painter;
12 import org.catacomb.report.E;
13
14
15 public class Plot implements PaintInstructor {
16
17 Color bgColor;
18
19 ArrayList<PlotElement> elements = new ArrayList<PlotElement>();
20
21 String xlabel;
22 String ylabel;
23
24 double xmin = Double.NaN;
25 double xmax = Double.NaN;
26
27 double ymin = Double.NaN;
28 double ymax = Double.NaN;
29
30 Box wkBox;
31
32 public Plot() {
33 wkBox = new Box();
34 }
35
36
37 public static void main(String[] argv) {
38 Plot p = new Plot();
39 double[] xdat = {0.1, 0.6, 3.2, 5.8};
40 double[] ydat = {4.5, 3, 2, 5};
41 p.setData(xdat, ydat);
42 p.makeImage(300, 300, "tst.png");
43 }
44
45
46
47
48 public void addLine(double[] xpts, double[] ypts, Color c) {
49 addLine(xpts, ypts, c, "");
50 }
51
52 public void addLine(double[][] xy, Color c) {
53 int n = xy.length;
54 double[] xpts = new double[n];
55 double[] ypts = new double[n];
56 for (int i = 0; i < n; i++) {
57 xpts[i] = xy[i][0];
58 ypts[i] = xy[i][0];
59 }
60 addLine(xpts, ypts, c);
61 }
62
63 public void addPolygon(double[][] xy, Color c) {
64 int n = xy.length;
65 double[] xpts = new double[n+1];
66 double[] ypts = new double[n+1];
67 for (int i = 0; i < n; i++) {
68 xpts[i] = xy[i][0];
69 ypts[i] = xy[i][1];
70 }
71 xpts[n] = xy[0][0];
72 ypts[n] = xy[0][1];
73 addLine(xpts, ypts, c);
74 }
75
76
77
78 public void addPolygon(double[] xpts, double[] ypts, Color c) {
79 elements.add(new PolygonPlotElement(xpts, ypts, c));
80 }
81
82
83 public void addLine(double[] xpts, double[] ypts, Color c, String s) {
84 elements.add(new LinePlotElement(xpts, ypts, c, s));
85 }
86
87 public void addPoints(double[] xpts, double[] ypts, Color c) {
88 elements.add(new PointsPlotElement(xpts, ypts, c));
89 }
90
91
92 public void addSmallPoints(double[] xpts, double[] ypts, Color c) {
93 elements.add(new PointsPlotElement(xpts, ypts, c, 1, 3));
94 }
95
96
97 public void addDots(double[] xpts, double[] ypts, Color c) {
98 elements.add(new PointsPlotElement(xpts, ypts, c, 1, 1));
99 }
100
101 public void setData(double[] xp, double[] yp) {
102 elements.add(new LinePlotElement(xp, yp));
103 }
104
105 public void setXRange(double xl, double xh) {
106 xmin = xl;
107 xmax = xh;
108 }
109
110 public void setYRange(double yl, double hy) {
111 ymin = yl;
112 ymax = hy;
113 }
114
115 public void setXLabel(String s) {
116 xlabel = s;
117 }
118
119 public void setYLabel(String s) {
120 ylabel = s;
121 }
122
123
124 public void makeImage(int w, int h, String fnm) {
125 makeImage(w, h, new File(fnm));
126 }
127
128 public void makeImage(int w, int h, File fout) {
129 double[] xyxy = {xmin, ymin, xmax, ymax};
130 makeImage(w, h, xyxy, fout);
131 }
132
133
134
135 public void makeImage(int w, int h, double[] xyxy, File fout) {
136 Box b = new Box();
137
138 if (Double.isNaN(xyxy[0]) ||Double.isNaN(xyxy[1]) || Double.isNaN(xyxy[2]) || Double.isNaN(xyxy[3])) {
139 Box blim = getLimitBox();
140 double dx = blim.getXmax() - blim.getXmin();
141 double dy = blim.getYmax() - blim.getYmin();
142
143 double f = 0.07;
144 b.extendXTo(Double.isNaN(xyxy[0]) ? blim.getXmin() - f * dx : xyxy[0]);
145 b.extendYTo(Double.isNaN(xyxy[1]) ? blim.getYmin() - f * dy : xyxy[1]);
146 b.extendXTo(Double.isNaN(xyxy[2]) ? blim.getXmax() + f * dx : xyxy[2]);
147 b.extendYTo(Double.isNaN(xyxy[3]) ? blim.getYmax() + f * dy: xyxy[3]);
148
149
150 } else {
151 b.extendTo(xyxy[0], xyxy[1]);
152 b.extendTo(xyxy[2], xyxy[3]);
153 }
154
155 b.tidyLimits();
156 makeImage(w, h, b, fout);
157 }
158
159
160
161 public void makeImage(int w, int h, Box b, File fout) {
162
163 GraphMaker gm = new GraphMaker(w, h, bgColor);
164
165
166 if (xlabel != null) {
167 gm.setXAxisLabel(xlabel);
168 }
169 if (ylabel != null) {
170 gm.setYAxisLabel(ylabel);
171 }
172
173 gm.setXRange(b.getXmin(), b.getXmax());
174 gm.setYRange(b.getYmin(), b.getYmax());
175
176 gm.drawData(this, fout);
177 }
178
179
180 public boolean antialias() {
181 return true;
182 }
183
184
185 public Box getLimitBox() {
186
187 Box b = new Box();
188 for (PlotElement pe : elements) {
189 pe.push(b);
190 }
191 return b;
192 }
193
194 public void autorange(boolean fixedAR) {
195 Box b = getLimitBox();
196
197 b.enlarge(0.1);
198 if (fixedAR) {
199 double dx = b.getXmax() - b.getXmin();
200 double dy = b.getYmax() - b.getYmin();
201 if (dy > dx) {
202 double cx = 0.5 * (b.getXmin() + b.getXmax());
203 setXRange(cx - dy/2, cx + dy/2);
204 setYRange(b.getYmin(), b.getYmax());
205 } else {
206 double cy = 0.5 * (b.getYmin() + b.getYmax());
207 setXRange(b.getXmin(), b.getXmax());
208 setYRange(cy - dx/2, cy + dx/2);
209 }
210
211 } else {
212 setXRange(b.getXmin(), b.getXmax());
213 setYRange(b.getYmin(), b.getYmax());
214 }
215 }
216
217
218 public void instruct(Painter p) {
219 for (PlotElement pe : elements) {
220 pe.instruct(p);
221 }
222
223 int ileg = 0;
224
225 for (PlotElement pe : elements) {
226 String s = pe.getLabel();
227 if (s != null && s.length() > 0) {
228 p.setColor(pe.getColor());
229 p.paintLegend(ileg, s);
230 ileg += 1;
231 }
232 }
233
234
235 }
236
237
238 public void addSortedLine(double[] xpts, double[] ypts, Color col) {
239
240
241
242 int np = xpts.length;
243 ArrayList<Double> xa = new ArrayList<Double>();
244 HashMap<Double, Double> dhm = new HashMap<Double, Double>();
245 for (int i = 0; i < xpts.length; i++) {
246 Double x = new Double(xpts[i]);
247 Double y = new Double(ypts[i]);
248 xa.add(x);
249 dhm.put(x, y);
250 }
251 Collections.sort(xa);
252 double[] xpa = new double[xpts.length];
253 double[] ypa = new double[xpts.length];
254 for (int i = 0; i < np; i++) {
255 Double d = xa.get(i);
256 xpa[i] = d.doubleValue();
257 ypa[i] = dhm.get(d).doubleValue();
258 }
259 addLine(xpa, ypa, col);
260
261
262 }
263
264
265 public void addString(double x, double y, String sc) {
266 elements.add(new StringPlotElement(x, y, sc));
267
268 }
269 public void addString(double x, double y, Color c, String sc) {
270 elements.add(new StringPlotElement(x, y, c, sc));
271 }
272
273 public void addArrowString(double x, double y, String sc) {
274 elements.add(new StringPlotElement(x, y, Color.white, sc, 10, -10));
275
276 }
277 public void addArrowString(double x, double y, Color c, String sc) {
278 elements.add(new StringPlotElement(x, y, c, sc, 10, -10));
279 }
280
281
282 public void addRasterRow(double[] times, double[] vs, double vmin, double vmax, double y, double thr) {
283 elements.add(new RasterRowElement(times, vs, vmin, vmax, y, thr));
284
285 }
286
287
288 public void setBackgroundColor(Color bg) {
289 bgColor = bg;
290
291 }
292
293 }
294