1
2
3 package org.catacomb.graph.gui;
4
5 import org.catacomb.report.E;
6
7 import java.awt.FontMetrics;
8 import java.awt.Graphics2D;
9 import java.awt.geom.AffineTransform;
10
11
12 public class YAxisCanvas extends BaseCanvas
13 implements RangeListener {
14 static final long serialVersionUID = 1001;
15
16 double ylow;
17 double yhigh;
18
19 int ntick = 5;
20
21 String labY;
22
23 final static int[] intervals = {1, 2, 5};
24
25 AffineTransform upTransform;
26 AffineTransform normalTransform;
27
28
29 public YAxisCanvas(int w, int h) {
30 super(w, h);
31
32 upTransform = new AffineTransform();
33 upTransform.setToRotation(-1 * Math.PI/2.);
34 normalTransform = new AffineTransform();
35 }
36
37
38 public void setLabel(String s) {
39 labY = s;
40 }
41
42
43 public void rangeChanged(int mode, double[] xyxylims) {
44 if (mode == RangeListener.X || mode == RangeListener.BOTH) {
45 ylow = xyxylims[1];
46 yhigh = xyxylims[3];
47 repaint();
48
49 }
50 }
51
52
53 public void paint2D(Graphics2D g) {
54 drawAxis(g);
55 }
56
57
58
59
60 public final void drawAxis(Graphics2D g) {
61 FontMetrics fm = g.getFontMetrics();
62
63
64 g.setColor(getNormalForeground());
65
66 int width = getWidth();
67 int height = getHeight();
68
69 double yran = Math.abs(yhigh - ylow);
70 double dy = 1.5 * yran / ntick;
71
72 double log = Math.log(dy) / Math.log(10.);
73 double powten = (int) Math.floor(log);
74 int iiind = (int)(2.999 * (log - powten));
75 if (iiind < 0 || iiind >= 3) {
76 E.error("error gdc, 650: " + log + " " + powten + " " + iiind);
77 iiind = 2;
78 }
79 int ii = intervals[iiind];
80 dy = Math.pow(10.0, powten) * ii;
81
82 int i0 = (int)(ylow / dy);
83 int i1 = (int)(yhigh / dy);
84
85 for (int i = i0; i <= i1; i++) {
86 double yy = i * dy;
87 String lab = "0";
88 if (i == 0) {
89
90
91 } else if (dy >= 0.999 && dy < 1.e4) {
92 lab = String.valueOf((int)(yy));
93 } else {
94 lab = String.valueOf((float)(yy));
95 }
96
97 int iy = height - (int)(height * (yy - ylow) / (yhigh - ylow));
98
99 int off = fm.stringWidth(lab);
100
101 g.drawString(lab, width - 12 - off, iy + 4);
102 g.drawLine(width - 5, iy, width, iy);
103
104 if (labY != null) {
105 int ilx = 18;
106 int ily = height / 2 + fm.stringWidth(labY) / 2;
107
108
109 AffineTransform at = g.getTransform();
110
111 g.translate((double)ilx, (double)ily);
112 g.rotate(-1 * Math.PI / 2);
113
114 g.drawString(labY, 0, 0);
115
116
117
118 g.setTransform(at);
119 }
120 }
121 }
122
123
124
125
126
127
128 }