1 | /* |
---|
2 | * |
---|
3 | * This software is provided by NOAA for full, free and open release. It is |
---|
4 | * understood by the recipient/user that NOAA assumes no liability for any |
---|
5 | * errors contained in the code. Although this software is released without |
---|
6 | * conditions or restrictions in its use, it is expected that appropriate |
---|
7 | * credit be given to its author and to the National Oceanic and Atmospheric |
---|
8 | * Administration should the software be included by the recipient as an |
---|
9 | * element in other product development. |
---|
10 | */ |
---|
11 | // RCS $Id: LogAxis.java,v 1.5 2003/08/22 23:02:32 dwd Exp $ |
---|
12 | |
---|
13 | package gov.noaa.pmel.sgt; |
---|
14 | |
---|
15 | import java.awt.Graphics; |
---|
16 | import java.awt.Rectangle; |
---|
17 | import gov.noaa.pmel.util.Point2D; |
---|
18 | import gov.noaa.pmel.util.Debug; |
---|
19 | |
---|
20 | /** |
---|
21 | * Axis class for creation of "log" axes. An {@link gov.noaa.pmel.sgt.demo.JLogLogDemo |
---|
22 | * exmample} is available demonstrating <code>LogAxis</code> use. |
---|
23 | * |
---|
24 | *--------------------------------------------------------------------------<br> |
---|
25 | * NAME : LogAxis.java<br> |
---|
26 | * FUNCTION : Draws axes using "log" style axis.<br> |
---|
27 | * ORIGIN : GFI INFORMATIQUE<br> |
---|
28 | * PROJECT : SONC DPS<br> |
---|
29 | * -------------------------------------------------------------------------<br> |
---|
30 | * HISTORY<br> |
---|
31 | * VERSION : 03/07/2002 : V0.0 : LBE<br> |
---|
32 | * old version had no fonctionality. It was just written |
---|
33 | * for future evolutions. This new version complete the class<br> |
---|
34 | * END-HISTORY<br> |
---|
35 | * ------------------------------------------------------------------------<br> |
---|
36 | * @author Donald Denbo |
---|
37 | * @version $Revision: 1.5 $, $Date: 2003/08/22 23:02:32 $ |
---|
38 | * @since 3.0 |
---|
39 | * |
---|
40 | */ |
---|
41 | |
---|
42 | public class LogAxis extends SpaceAxis implements Cloneable { |
---|
43 | public LogAxis(String id) { |
---|
44 | super(id); |
---|
45 | space_ = true; |
---|
46 | numSmallTics_ = 9; //fixed number. only 8 shown |
---|
47 | } |
---|
48 | |
---|
49 | |
---|
50 | public Axis copy() { |
---|
51 | LogAxis newAxis; |
---|
52 | try { |
---|
53 | newAxis = (LogAxis)clone(); |
---|
54 | } catch (CloneNotSupportedException e) { |
---|
55 | newAxis = new LogAxis(getId()); |
---|
56 | } |
---|
57 | return (Axis)newAxis; |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | void draw(Graphics g) { |
---|
62 | //throw new MethodNotImplementedError(); |
---|
63 | int xloc, yloc, xend, yend; |
---|
64 | int istop, i; |
---|
65 | double j; |
---|
66 | double xt, yt, dir, x, y, xp, yp; |
---|
67 | double xtitle, ytitle; |
---|
68 | double delta = uRange_.delta; |
---|
69 | Format format; |
---|
70 | String labelText; |
---|
71 | SGLabel title = getTitle(); |
---|
72 | if(!visible_) return; |
---|
73 | if(Double.isNaN(delta)) delta = (uRange_.end - uRange_.start)/10.0; |
---|
74 | if(title != null) title.setLayer(graph_.getLayer()); |
---|
75 | // |
---|
76 | g.setColor(graph_.getLayer().getPane().getComponent().getForeground()); |
---|
77 | // |
---|
78 | |
---|
79 | if(labelFormat_.length() <= 0) { |
---|
80 | format = new Format(Format.computeFormat(uRange_.start, uRange_.end, sigDigits_)); |
---|
81 | } else { |
---|
82 | format = new Format(labelFormat_); |
---|
83 | } |
---|
84 | if(orientation_ == Axis.HORIZONTAL) { |
---|
85 | if(Debug.DEBUG) System.out.println("LogAxis: start drawing XAxis"); |
---|
86 | if(uLocation_ == null) { |
---|
87 | yloc = graph_.getYUtoD(tLocation_.t); |
---|
88 | yp = graph_.getYUtoP(tLocation_.t); |
---|
89 | } else { |
---|
90 | yloc = graph_.getYUtoD(uLocation_.y); |
---|
91 | yp = graph_.getYUtoP(uLocation_.y); |
---|
92 | } |
---|
93 | xloc = graph_.getXUtoD(uRange_.start); |
---|
94 | xend = graph_.getXUtoD(uRange_.end); |
---|
95 | g.drawLine(xloc, yloc, xend, yloc); |
---|
96 | |
---|
97 | //X tics drawing |
---|
98 | dir = delta > 0? 1.0: -1.0; |
---|
99 | xt = (int)((uRange_.start/delta + (dir*uRange_.start > 0? 1.0: -1.0)*0.00001))*delta; |
---|
100 | |
---|
101 | if(dir*xt < dir*uRange_.start) xt += delta; |
---|
102 | istop = (int)((uRange_.end - xt)/delta + 0.00001); |
---|
103 | |
---|
104 | if(uRange_.start<=0) return; |
---|
105 | |
---|
106 | int imin = (int)(Math.ceil(Math.log(uRange_.start)/Math.log(10))); // first large tic |
---|
107 | int imax = (int)(Math.floor(Math.log(uRange_.end)/Math.log(10))); //last large tic |
---|
108 | int nblabel = imax-imin +1; |
---|
109 | |
---|
110 | |
---|
111 | /* System.out.println("uRange.start/end: "+uRange_.start+"/"+uRange_.end); |
---|
112 | System.out.println("uRangeP: "+graph_.getYUtoP(uRange_.start)+"/"+graph_.getYUtoP(uRange_.end)); |
---|
113 | */ |
---|
114 | double min = (double)Math.pow(10,imin); |
---|
115 | double max = (double)Math.pow(10,imax); |
---|
116 | |
---|
117 | xt=min; |
---|
118 | x = xt; |
---|
119 | xp = graph_.getXUtoP(x); |
---|
120 | |
---|
121 | for (j=min/10.0d;j<min;j=j+min/10.0d) { |
---|
122 | xp = graph_.getXUtoP(j); |
---|
123 | if(j>uRange_.start) drawXTic(g, xp, yp, smallTicHeight_); |
---|
124 | } |
---|
125 | |
---|
126 | for (j=min;j<=max;j=j*10.0d) { |
---|
127 | if(j>min) drawSmallXTics(g, j/10, uRange_.end, j, yp); |
---|
128 | //if(j>min) drawSmallXTics(g, yp, j/10, uRange_.end, j); |
---|
129 | xp = graph_.getXUtoP(j); |
---|
130 | drawXTic(g, xp, yp, largeTicHeight_); |
---|
131 | } |
---|
132 | drawSmallXTics(g, j, uRange_.end, j, yp); |
---|
133 | //drawSmallXTics(g, yp, j, uRange_.end, j); |
---|
134 | |
---|
135 | // |
---|
136 | if(labelInterval_ <= 0 || labelPosition_ == NO_LABEL) return; |
---|
137 | |
---|
138 | SGLabel label; |
---|
139 | int vertalign; |
---|
140 | int horzalign; |
---|
141 | |
---|
142 | if(dir*uRange_.start <= 0 && dir*uRange_.end >= 0) { |
---|
143 | x = ((int)(uRange_.start/(delta*labelInterval_) - 0.00001))*delta*labelInterval_; |
---|
144 | } else { |
---|
145 | x = xt; |
---|
146 | } |
---|
147 | istop = (int)((uRange_.end - x)/(delta*labelInterval_) + 0.00001); |
---|
148 | long jump = 10; // label display on each tic |
---|
149 | if(istop<nblabel) jump = 100; // one on two |
---|
150 | |
---|
151 | if(labelPosition_ == POSITIVE_SIDE) { |
---|
152 | vertalign = SGLabel.BOTTOM; |
---|
153 | horzalign = SGLabel.CENTER; |
---|
154 | if(ticPosition_ == BOTH_SIDES || ticPosition_ == POSITIVE_SIDE) { |
---|
155 | yt = yp + TIC_RATIO*largeTicHeight_; |
---|
156 | } else { |
---|
157 | yt = yp + TIC_GAP; |
---|
158 | } |
---|
159 | ytitle = yt + LABEL_RATIO*labelHeight_; |
---|
160 | } else { |
---|
161 | vertalign = SGLabel.TOP; |
---|
162 | horzalign = SGLabel.CENTER; |
---|
163 | if(ticPosition_ == BOTH_SIDES || ticPosition_ == NEGATIVE_SIDE) { |
---|
164 | yt = yp - TIC_RATIO*largeTicHeight_; |
---|
165 | } else { |
---|
166 | yt = yp - TIC_GAP; |
---|
167 | } |
---|
168 | ytitle = yt - LABEL_RATIO*labelHeight_; |
---|
169 | } |
---|
170 | |
---|
171 | for(j=min; j <= max; j*=jump) { |
---|
172 | xt = graph_.getXUtoP(j)-LABEL_RATIO*labelHeight_*0.25; |
---|
173 | //xt = graph_.getXUtoP(j); |
---|
174 | //System.out.println("affich["+j+"]: 10e"+Math.round( Math.log(j)/Math.log(10) )); |
---|
175 | labelText = "10e"+Math.round(Math.log(j)/Math.log(10)); |
---|
176 | label = new SGLabel("coordinate", labelText, new Point2D.Double(xt, yt)); |
---|
177 | label.setAlign(vertalign, horzalign); |
---|
178 | label.setOrientation(SGLabel.HORIZONTAL); |
---|
179 | label.setFont(labelFont_); |
---|
180 | label.setColor(labelColor_); |
---|
181 | label.setHeightP(labelHeight_); |
---|
182 | label.setLayer(graph_.getLayer()); |
---|
183 | try { |
---|
184 | label.draw(g); |
---|
185 | } catch (LayerNotFoundException e) {} |
---|
186 | //x = x + delta*labelInterval_; |
---|
187 | } |
---|
188 | if(title_ != null) { |
---|
189 | //xtitle = (uRange_.end + uRange_.start)*0.5; |
---|
190 | xtitle = graph_.getXUtoP(uRange_.end) + graph_.getXUtoP(uRange_.start); |
---|
191 | xt = xtitle*0.5; |
---|
192 | yt = ytitle; |
---|
193 | xt = graph_.getXUtoP(xtitle); |
---|
194 | title.setLocationP(new Point2D.Double(xt, yt)); |
---|
195 | title.setAlign(vertalign, SGLabel.CENTER); |
---|
196 | title.setOrientation(SGLabel.HORIZONTAL); |
---|
197 | try { |
---|
198 | title.draw(g); |
---|
199 | } catch (LayerNotFoundException e) {} |
---|
200 | } |
---|
201 | } else { // orientation is vertical |
---|
202 | if(Debug.DEBUG) System.out.println("LogAxis: start drawing YAxis"); |
---|
203 | if(uLocation_ == null) { |
---|
204 | xloc = graph_.getXUtoD(tLocation_.t); |
---|
205 | xp = graph_.getXUtoP(tLocation_.t); |
---|
206 | } else { |
---|
207 | xloc = graph_.getXUtoD(uLocation_.x); |
---|
208 | xp = graph_.getXUtoP(uLocation_.x); |
---|
209 | } |
---|
210 | yloc = graph_.getYUtoD(uRange_.start); |
---|
211 | yend = graph_.getYUtoD(uRange_.end); |
---|
212 | g.drawLine(xloc, yloc, xloc, yend); |
---|
213 | |
---|
214 | //draw Y tics |
---|
215 | dir = delta > 0? 1.0: -1.0; |
---|
216 | yt = (int)((uRange_.start/delta) + (dir*uRange_.start > 0? 1.0: -1.0)*0.00001)*delta; |
---|
217 | if(dir*yt < dir*uRange_.start) yt += delta; |
---|
218 | istop = (int)((uRange_.end - yt)/delta + 0.00001); |
---|
219 | |
---|
220 | if(uRange_.start<=0) return; |
---|
221 | |
---|
222 | int imin = (int)(Math.ceil(Math.log(uRange_.start)/Math.log(10))); // premier large tic |
---|
223 | int imax = (int)(Math.floor(Math.log(uRange_.end)/Math.log(10))); //dernier large tic |
---|
224 | int nblabel = imax-imin +1; |
---|
225 | |
---|
226 | //System.out.println("uRange.start/end: "+uRange_.start+"/"+uRange_.end); |
---|
227 | //System.out.println("uRangeP: "+graph_.getYUtoP(uRange_.start)+"/"+graph_.getYUtoP(uRange_.end)); |
---|
228 | |
---|
229 | |
---|
230 | double min = (double)Math.pow(10,imin); |
---|
231 | double max = (double)Math.pow(10,imax); |
---|
232 | |
---|
233 | |
---|
234 | yt=min; |
---|
235 | y = yt; |
---|
236 | yp = graph_.getYUtoP(y); |
---|
237 | |
---|
238 | for (j=min/10.0d;j<min;j=j+min/10.0d) { |
---|
239 | yp = graph_.getYUtoP(j); |
---|
240 | if(j>uRange_.start) drawYTic(g, xp, yp, smallTicHeight_); |
---|
241 | } |
---|
242 | |
---|
243 | for (j=min;j<=max;j=j*10.0d) { |
---|
244 | if(j>min) drawSmallYTics(g, xp, j/10, uRange_.end, j); |
---|
245 | yp = graph_.getYUtoP(j); |
---|
246 | drawYTic(g, xp, yp, largeTicHeight_); |
---|
247 | } |
---|
248 | drawSmallYTics(g, xp, j, uRange_.end, j); |
---|
249 | |
---|
250 | // |
---|
251 | if(labelInterval_ <= 0 || labelPosition_ == NO_LABEL) return; |
---|
252 | // |
---|
253 | SGLabel label; |
---|
254 | int vertalign; |
---|
255 | int horzalign; |
---|
256 | |
---|
257 | if(dir*uRange_.start <= 0 && dir*uRange_.end >= 0) { |
---|
258 | y = ((int)(uRange_.start/(delta*labelInterval_) - 0.00001))*delta*labelInterval_; |
---|
259 | } else { |
---|
260 | y = yt; |
---|
261 | } |
---|
262 | |
---|
263 | istop = (int)((uRange_.end - y)/(delta*labelInterval_) + 0.00001); |
---|
264 | long jump = 10; // label display on each tic |
---|
265 | if(istop<nblabel) jump = 100; // one on two |
---|
266 | |
---|
267 | Layer l = graph_.getLayer(); |
---|
268 | double widthP=0; |
---|
269 | double maxWidthP=0; |
---|
270 | if (l!=null) { |
---|
271 | for(j=min; j <= max; j*=jump) { |
---|
272 | labelText = "10e"+Math.round(Math.log(j)/Math.log(10)); |
---|
273 | //get Y Label size in Device unit |
---|
274 | //widthP = l.getXDtoP(l.getFontMetrics(labelFont_).stringWidth(labelText)); |
---|
275 | label = new SGLabel("coordinate", labelText.trim(), new Point2D.Double(0, yt)); |
---|
276 | label.setOrientation(SGLabel.HORIZONTAL); |
---|
277 | label.setFont(labelFont_); |
---|
278 | label.setHeightP(labelHeight_); |
---|
279 | label.setLayer(l); |
---|
280 | widthP = l.getXDtoP((int)label.getStringWidth(g)); |
---|
281 | if (widthP>maxWidthP) maxWidthP = widthP; |
---|
282 | } |
---|
283 | } |
---|
284 | |
---|
285 | if(labelPosition_ == NEGATIVE_SIDE) { |
---|
286 | vertalign = SGLabel.BOTTOM; |
---|
287 | horzalign = SGLabel.RIGHT; |
---|
288 | if(ticPosition_ == BOTH_SIDES || ticPosition_ == NEGATIVE_SIDE) { |
---|
289 | xt = xp - TIC_RATIO*largeTicHeight_; |
---|
290 | } else { |
---|
291 | xt = xp - TIC_GAP; |
---|
292 | } |
---|
293 | //xtitle = xt - LABEL_RATIO*labelHeight_-LABEL_RATIO*maxWidthP*0.5; |
---|
294 | xtitle = xt - maxWidthP; |
---|
295 | } else { |
---|
296 | vertalign = SGLabel.TOP; |
---|
297 | horzalign = SGLabel.LEFT; |
---|
298 | if(ticPosition_ == BOTH_SIDES || ticPosition_ == POSITIVE_SIDE) { |
---|
299 | xt = xp + TIC_RATIO*largeTicHeight_; |
---|
300 | } else { |
---|
301 | xt = xp + TIC_GAP; |
---|
302 | } |
---|
303 | //xtitle = xt + LABEL_RATIO*labelHeight_ + maxWidthP; |
---|
304 | xtitle = xt + maxWidthP; |
---|
305 | } |
---|
306 | |
---|
307 | //g.drawLine(l.getXPtoD(xt),l.getYPtoD(0.0),l.getXPtoD(xt),l.getYPtoD(4.0)); |
---|
308 | //g.drawLine(l.getXPtoD(xtitle),l.getYPtoD(0.0),l.getXPtoD(xtitle),l.getYPtoD(4.0)); |
---|
309 | |
---|
310 | for(j=min; j <= max; j*=jump) { |
---|
311 | yt = graph_.getYUtoP(j);//-LABEL_RATIO*labelHeight_*0.25; |
---|
312 | //System.out.println("affich["+j+"]: 10e"+Math.round( Math.log(j)/Math.log(10) )); |
---|
313 | labelText = "10e"+Math.round(Math.log(j)/Math.log(10)); |
---|
314 | label = new SGLabel("coordinate", labelText, new Point2D.Double(xt, yt)); |
---|
315 | label.setAlign(SGLabel.CENTER, horzalign); |
---|
316 | label.setOrientation(SGLabel.HORIZONTAL); |
---|
317 | label.setFont(labelFont_); |
---|
318 | label.setColor(labelColor_); |
---|
319 | label.setHeightP(labelHeight_); |
---|
320 | label.setLayer(graph_.getLayer()); |
---|
321 | try { |
---|
322 | label.draw(g); |
---|
323 | } catch (LayerNotFoundException e) {} |
---|
324 | //y = j*10;//delta*labelInterval_; |
---|
325 | } |
---|
326 | if(title_ != null) { |
---|
327 | ytitle = graph_.getYUtoP(uRange_.end) + graph_.getYUtoP(uRange_.start); |
---|
328 | yt = ytitle*0.5; |
---|
329 | xt = xtitle; |
---|
330 | title.setLocationP(new Point2D.Double(xt, yt)); |
---|
331 | title.setAlign(vertalign, SGLabel.CENTER); |
---|
332 | title.setOrientation(SGLabel.VERTICAL); |
---|
333 | try { |
---|
334 | title.draw(g); |
---|
335 | } catch (LayerNotFoundException e) {} |
---|
336 | } |
---|
337 | } |
---|
338 | } |
---|
339 | |
---|
340 | /** |
---|
341 | * Get the bounding box for the axis in device units. |
---|
342 | * |
---|
343 | * @return bounding box |
---|
344 | * @see Rectangle |
---|
345 | **/ |
---|
346 | public Rectangle getBounds() { |
---|
347 | double xp, yp, ymin, ymax, xmin, xmax; |
---|
348 | int xd, yd, width, height, x, y; |
---|
349 | if(orientation_ == Axis.HORIZONTAL) { |
---|
350 | xd = graph_.getXUtoD(uRange_.start); |
---|
351 | if(uLocation_ == null) { |
---|
352 | yp = graph_.getYUtoP(tLocation_.t); |
---|
353 | } else { |
---|
354 | yp = graph_.getYUtoP(uLocation_.y); |
---|
355 | } |
---|
356 | width = graph_.getXUtoD(uRange_.end) - xd; |
---|
357 | x = xd; |
---|
358 | ymin = yp; |
---|
359 | ymax = yp; |
---|
360 | if(ticPosition_ == BOTH_SIDES || ticPosition_ == POSITIVE_SIDE) { |
---|
361 | ymax = ymax + largeTicHeight_; |
---|
362 | } |
---|
363 | if(ticPosition_ == BOTH_SIDES || ticPosition_ == NEGATIVE_SIDE) { |
---|
364 | ymin = ymin - largeTicHeight_; |
---|
365 | } |
---|
366 | if(labelPosition_ == POSITIVE_SIDE) { |
---|
367 | if(ticPosition_ == BOTH_SIDES || ticPosition_ == POSITIVE_SIDE) { |
---|
368 | ymax = ymax + (1.0 -TIC_RATIO)*largeTicHeight_ + labelHeight_; |
---|
369 | } else { |
---|
370 | ymax = ymax + TIC_GAP + labelHeight_; |
---|
371 | } |
---|
372 | } else if(labelPosition_ == NEGATIVE_SIDE) { |
---|
373 | if(ticPosition_ == BOTH_SIDES || ticPosition_ == NEGATIVE_SIDE) { |
---|
374 | ymin = ymin - (1.0 - TIC_RATIO)*largeTicHeight_ - labelHeight_; |
---|
375 | } else { |
---|
376 | ymin = ymin - TIC_GAP - labelHeight_; |
---|
377 | } |
---|
378 | } |
---|
379 | y = graph_.getLayer().getYPtoD(ymax); |
---|
380 | height = graph_.getLayer().getYPtoD(ymin) - y; |
---|
381 | } else { |
---|
382 | yd = graph_.getYUtoD(uRange_.start); |
---|
383 | if(uLocation_ == null) { |
---|
384 | xp = graph_.getXUtoP(tLocation_.t); |
---|
385 | } else { |
---|
386 | xp = graph_.getXUtoP(uLocation_.x); |
---|
387 | } |
---|
388 | y = graph_.getYUtoD(uRange_.end); |
---|
389 | height = yd - y; |
---|
390 | xmin = xp; |
---|
391 | xmax = xp; |
---|
392 | if(ticPosition_ == BOTH_SIDES || ticPosition_ == POSITIVE_SIDE) { |
---|
393 | xmax = xmax + largeTicHeight_; |
---|
394 | } |
---|
395 | if(ticPosition_ == BOTH_SIDES || ticPosition_ == NEGATIVE_SIDE) { |
---|
396 | xmin = xmin - largeTicHeight_; |
---|
397 | } |
---|
398 | if(labelPosition_ == POSITIVE_SIDE) { |
---|
399 | if(ticPosition_ == BOTH_SIDES || ticPosition_ == POSITIVE_SIDE) { |
---|
400 | xmax = xmax + (1.0 -TIC_RATIO)*largeTicHeight_ + labelHeight_; |
---|
401 | } else { |
---|
402 | xmax = xmax + TIC_GAP + labelHeight_; |
---|
403 | } |
---|
404 | } else if(labelPosition_ == NEGATIVE_SIDE) { |
---|
405 | if(ticPosition_ == BOTH_SIDES || ticPosition_ == NEGATIVE_SIDE) { |
---|
406 | xmin = xmin - (1.0 - TIC_RATIO)*largeTicHeight_ - labelHeight_; |
---|
407 | } else { |
---|
408 | xmin = xmin - TIC_GAP - labelHeight_; |
---|
409 | } |
---|
410 | } |
---|
411 | x = graph_.getLayer().getXPtoD(xmin); |
---|
412 | width = graph_.getLayer().getXPtoD(xmax) - x; |
---|
413 | } |
---|
414 | return new Rectangle(x, y, width, height); |
---|
415 | } |
---|
416 | |
---|
417 | public void setBounds(int x, int y, int width, int height) { |
---|
418 | } |
---|
419 | public void setBounds(Rectangle rect) { |
---|
420 | setBounds(rect.x, rect.y, rect.width, rect.height); |
---|
421 | } |
---|
422 | } |
---|