/Users/lyon/j4p/src/bookExamples/ch27BusinessGraphics/charts/Multiplot.java

1    package bookExamples.ch27BusinessGraphics.charts; 
2     
3    import ip.gui.frames.ClosableFrame; 
4     
5    import java.awt.*; 
6    import java.util.StringTokenizer; 
7     
8    public class Multiplot { 
9     
10       public static void main(String args[]) { 
11    
12           double limit = 2 * Math.PI; 
13           double incr = 0.01; 
14    
15           int arr1 = (int) (1 + limit / incr); 
16           int arr2 = 4; 
17           double x; 
18           int n; 
19    
20           String xaxis[] = {"0 1.0 2.0 3.0", 
21                             "0 pi/2 pi 3pi/2 2pi", 
22                             "0 1 2 3 4 5"}; 
23           String yaxis[] = {"1.00 0.50 0.00 -0.5 -1.0", 
24                             "1.0  0.0  -1.0", 
25                             "A B C"}; 
26           String title[] = {"x", "Cos(2x)", "Cos2(pi+x)", 
27                             "Cos2(2pi+x)", "Cos2(3pi+x)"}; 
28    
29           double data1[][] = new double[arr1][arr2]; 
30    
31           for (int m = 0; m < arr2; m++) { 
32               for (x = 0, n = 0; x < limit; x += incr, n++) 
33                   if (m == 0) 
34                       data1[n][m] = x; //setup x-axis 
35                   else 
36                       data1[n][m] = Math.cos((m - 1) * Math.PI + x); 
37           } 
38           Db data = new Db(arr1, arr2, data1, xaxis, yaxis, 
39                   title); 
40    
41           NewPlotFrame np = new NewPlotFrame(data); 
42           np.setSize(400, 380); 
43           np.setVisible(true); 
44       } 
45    
46   } 
47    
48   class NewPlotFrame extends ClosableFrame { 
49    
50       Db data; 
51    
52       NewPlotFrame(Db d) { 
53           super("My New Graph"); 
54           data = d; 
55       } 
56    
57       public void paint(Graphics g) { 
58    
59           Font f = new Font("TimesRoman", Font.PLAIN, 12); 
60           g.setFont(f); 
61    
62           Rectangle r = getBounds(); 
63           FontMetrics gfm = g.getFontMetrics(); 
64           int markMargin = 5; 
65           int labHmg = gfm.getHeight(); 
66           int xMargin = 100; 
67    
68           int w = r.width - 2 * xMargin; 
69           int h = r.height - 5 * labHmg; 
70    
71           int area = h / (data.ar2 - 1) - labHmg; 
72    
73           setBackground(Color.white); 
74    
75           Point p[][] = new Point[data.ar2 - 1][2]; 
76           int x[][] = new int[data.ar2 - 1][2]; 
77           int y[][] = new int[data.ar2 - 1][2]; 
78           double max = 2 * Math.PI; 
79    
80           int num1 = 0,sum; 
81           StringTokenizer st1,st2; 
82    
83           for (int i = 0; i < data.ar2 - 1; i++) { 
84               g.drawLine(xMargin, (1 + i) * (area + labHmg), 
85                       w + xMargin, (1 + i) * (area + labHmg));//Xaxis 
86               g.drawLine(xMargin, (1 + i) * labHmg + i * area, xMargin, 
87                       (1 + i) * (area + labHmg)); //Yaxis 
88               g.drawString(data.t[0], w + xMargin + 4 * markMargin, 
89                       (1 + i) * (area + labHmg) + markMargin);//x 
90               g.drawString(data.t[i + 1], labHmg, 
91                       (1 + i) * labHmg + i * area + 2 * markMargin);     //f(x) 
92           } 
93    
94           //y lab 
95           for (int i = 0; i < data.ar2 - 1; i++) { 
96               st1 = new StringTokenizer(data.ylab[i]); 
97               while (st1.hasMoreTokens()) { 
98                   st1.nextToken(); 
99                   ++num1; 
100              } 
101              sum = num1; 
102              num1 = 0; 
103              st2 = new StringTokenizer(data.ylab[i]); 
104              int labWmg = (gfm.stringWidth(data.ylab[i]) / sum); 
105              while (st2.hasMoreTokens()) { 
106                  g.drawString(st2.nextToken(), xMargin - labWmg, 
107                          i * (area + labHmg) + 2 * labHmg + area / sum * num1); 
108   
109                  for (int j = 0; j < 2 * sum; j++) {     //y mark 
110                      if (j / 2 != num1) 
111                          g.drawLine(xMargin, 
112                                  i * (area + labHmg) + labHmg + area / (sum * 2) * j, 
113                                  xMargin + labWmg / 5, 
114                                  i * (area + labHmg) + labHmg + area / (sum * 2) * j); 
115                      else 
116                          g.drawLine(xMargin, 
117                                  i * (area + labHmg) + labHmg + area / sum * num1, 
118                                  xMargin + labWmg / 3, 
119                                  i * (area + labHmg) + labHmg + area / sum * num1); 
120                  } 
121                  ++num1; 
122              } 
123              sum = 0; 
124              num1 = 0; 
125          } 
126   
127          //x lab 
128          for (int i = 0; i < data.ar2 - 1; i++) { 
129              st1 = new StringTokenizer(data.xlab[i]); 
130              while (st1.hasMoreTokens()) { 
131                  st1.nextToken(); 
132                  ++num1; 
133              } 
134              sum = num1; 
135              num1 = 0; 
136              st2 = new StringTokenizer(data.xlab[i]); 
137              while (st2.hasMoreTokens()) { 
138                  g.drawString(st2.nextToken(), w / sum * num1 + 
139                          xMargin - markMargin, 
140                          (1 + i) * (area + labHmg) + 2 * markMargin); 
141   
142                  for (int j = 0; j < sum * sum; j++) {   //x mark 
143                      if (j / sum != num1) 
144                          g.drawLine(w / (sum * sum) * j + xMargin, 
145                                  (1 + i) * (area + labHmg) - markMargin, 
146                                  w / (sum * sum) * j + xMargin, 
147                                  (1 + i) * (area + labHmg)); 
148                      else 
149                          g.drawLine(w / sum * num1 + xMargin, 
150                                  (1 + i) * (area + labHmg) - 2 * markMargin, 
151                                  w / sum * num1 + xMargin, 
152                                  (1 + i) * (area + labHmg)); 
153                  } 
154                  ++num1; 
155              } 
156              sum = 0; 
157              num1 = 0; 
158          } 
159   
160   
161          for (int i = 0; i < data.ar2 - 1; i++) 
162              p[i][0] = new Point(xMargin, (i + 1) * (area / 2 + 2 * labHmg)); 
163   
164          for (int j = 0; j < data.ar2 - 1; j++) { 
165              for (int i = 0; i < data.ar1; i++) { 
166                  x[j][1] = (int) (data.dataxy[i][0] * w / max + xMargin); 
167                  y[j][1] = (int) ((j * (area + labHmg)) + area / 2 - area / 2 * 
168                          (data.dataxy[i][j + 1]) + labHmg); 
169   
170                  p[j][1] = new Point(x[j][1], y[j][1]); 
171                  g.drawLine(p[j][0].x, 
172                          p[j][0].y, p[j][1].x, p[j][1].y); 
173                  p[j][0] = p[j][1]; 
174              } 
175          } 
176      } 
177  } 
178   
179  class Db { 
180      double limit = 2 * Math.PI; 
181      double incr = 0.01; 
182      int ar1 = (int) ((1 + limit) / incr); 
183      int ar2 = 4; 
184   
185      double[][] dataxy = new double[ar1][ar2]; 
186      String xlab[]; 
187      String ylab[]; 
188      String t[]; 
189   
190      public Db(int array1, int array2, 
191                double data[][], String[] x, 
192                String[] y, String[] tt) { 
193          ar1 = array1; 
194          ar2 = array2; 
195          for (int i = 0; i < ar1; i++) { 
196              for (int j = 0; j < ar2; j++) 
197                  dataxy[i][j] = data[i][j]; 
198          } 
199   
200          xlab = x; 
201          ylab = y; 
202          t = tt; 
203      } 
204   
205      public Db(Db data) { 
206          ar1 = data.ar1; 
207          ar2 = data.ar2; 
208          for (int i = 0; i < ar1; i++) { 
209              for (int j = 0; j < ar2; j++) 
210                  dataxy[i][j] = data.dataxy[i][j]; 
211          } 
212   
213          xlab = data.getX(); 
214          ylab = data.getY(); 
215          t = data.getT(); 
216      } 
217   
218      public String[] getT() { 
219          return t; 
220      } 
221   
222      public String[] getY() { 
223          return ylab; 
224      } 
225   
226      public String[] getX() { 
227          return xlab; 
228      } 
229  } 
230