1 package org.catacomb.dataview;
2
3 import java.awt.Color;
4 import java.awt.FontMetrics;
5 import java.awt.Graphics2D;
6 import java.awt.geom.AffineTransform;
7
8 import org.catacomb.graph.gui.WorldTransform;
9
10
11 public class AxisPainter {
12
13 Graphics2D graphics;
14 WorldTransform wt;
15
16
17 public AxisPainter(Graphics2D g, WorldTransform t) {
18 graphics = g;
19 wt = t;
20 }
21
22
23 public void drawAxes() {
24 int lm = wt.getLeftMargin();
25 int bm = wt.getBottomMargin();
26 int width = wt.getWidth();
27 int height = wt.getHeight();
28 graphics.setColor(Color.lightGray);
29
30 graphics.fillRect(0, 0, lm, height);
31 graphics.fillRect(0, height - bm, width, bm);
32 graphics.setColor(Color.black);
33
34 drawXAxis();
35 drawYAxis();
36
37 }
38
39
40 public void labelAxes(String xlabel, String ylabel) {
41 int lm = wt.getLeftMargin();
42 int bm = wt.getBottomMargin();
43 int width = wt.getWidth();
44 int height = wt.getHeight();
45
46
47 FontMetrics fm = graphics.getFontMetrics();
48
49
50 if (xlabel != null) {
51
52 int ilx = (width - lm) / 2 - fm.stringWidth(xlabel) / 2;
53 int ily = height - 3;
54 graphics.drawString(xlabel, lm + ilx, ily);
55 }
56
57
58 if (ylabel != null) {
59 int ilx = 18;
60 int ily = (height - bm) / 2 + fm.stringWidth(ylabel) / 2;
61
62
63 AffineTransform at = graphics.getTransform();
64
65 graphics.translate((double) ilx, (double) ily);
66 graphics.rotate(-1 * Math.PI / 2);
67
68 graphics.drawString(ylabel, 0, 0);
69
70
71 graphics.setTransform(at);
72 }
73 }
74
75
76 public final void drawXAxis() {
77
78 int[] intervals = { 1, 2, 5 };
79 int ntick = 5;
80
81 int lm = wt.getLeftMargin();
82
83 int bm = wt.getBottomMargin();
84
85
86 int width = wt.getWidth();
87 int height = wt.getHeight();
88
89
90 graphics.drawLine(lm, height - bm, width, height - bm);
91
92 double[] xr = wt.getXRange();
93
94 double xran = Math.abs(xr[1] - xr[0]);
95 double dx = 1.5 * xran / ntick;
96
97 double log = Math.log(dx) / Math.log(10.);
98 double powten = (int) Math.floor(log);
99 int iiind = (int)(2.999 * (log - powten));
100
101 int ii = intervals[iiind];
102 dx = Math.pow(10.0, powten) * ii;
103
104 int i0 = (int)(xr[0] / dx);
105 int i1 = (int)(xr[1] / dx);
106
107 for (int i = i0; i <= i1; i++) {
108 double xx = i * dx;
109 String lab = "0";
110 if (i == 0) {
111
112
113 } else if (dx >= 0.999 && dx < 1.e4) {
114 lab = String.valueOf((int)(xx));
115 } else {
116 lab = String.valueOf((float)(xx));
117 }
118 int off = lab.length();
119 off = 1 - 4 * off;
120 if (i * dx < 0.0) {
121 off -= 4;
122 }
123
124 int ix = (int)(lm + (width - lm) * (xx - xr[0]) / (xr[1] - xr[0]));
125
126 graphics.drawString(lab, ix + off, height - bm + 20);
127 graphics.drawLine(ix, height - bm, ix, height - bm + 5);
128
129 }
130 }
131
132
133 public final void drawYAxis() {
134 FontMetrics fm = graphics.getFontMetrics();
135
136 int[] intervals = { 1, 2, 5 };
137 int ntick = 5;
138
139
140 int height = wt.getHeight();
141 int lm = wt.getLeftMargin();
142
143 int bm = wt.getBottomMargin();
144
145
146 graphics.drawLine(lm, height - bm, lm, 0);
147
148 double[] yr = wt.getYRange();
149 double ylow = yr[0];
150 double yhigh = yr[1];
151
152 double yran = Math.abs(yhigh - ylow);
153 double dy = 1.5 * yran / ntick;
154
155 double log = Math.log(dy) / Math.log(10.);
156 double powten = (int) Math.floor(log);
157 int iiind = (int)(2.999 * (log - powten));
158
159 int ii = intervals[iiind];
160 dy = Math.pow(10.0, powten) * ii;
161
162 int i0 = (int)(ylow / dy);
163 int i1 = (int)(yhigh / dy);
164
165 for (int i = i0; i <= i1; i++) {
166 double yy = i * dy;
167 String lab = "0";
168 if (i == 0) {
169
170
171 } else if (dy >= 0.999 && dy < 1.e4) {
172 lab = String.valueOf((int)(yy));
173 } else {
174 lab = String.valueOf((float)(yy));
175 }
176
177 int iy = height - bm - (int)((height - bm) * (yy - ylow) / (yhigh - ylow));
178
179 int off = fm.stringWidth(lab);
180
181 graphics.drawString(lab, lm - 12 - off, iy + 4);
182 graphics.drawLine(lm - 5, iy, lm, iy);
183
184
185 }
186 }
187
188
189 }