KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > plot > CartesianCoordinateSystem


1 /*
2  * Copyright 2003-2004, Franz-Josef Elmer, All rights reserved
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details
13  * (http://www.gnu.org/copyleft/lesser.html).
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jcckit.plot;
20
21 import jcckit.data.DataPoint;
22 import jcckit.graphic.ClippingRectangle;
23 import jcckit.graphic.ClippingShape;
24 import jcckit.graphic.GraphicalComposite;
25 import jcckit.graphic.GraphicalElement;
26 import jcckit.graphic.GraphicAttributes;
27 import jcckit.graphic.GraphPoint;
28 import jcckit.graphic.LineAttributes;
29 import jcckit.graphic.Polygon;
30 import jcckit.graphic.Text;
31 import jcckit.transformation.CartesianTransformation;
32 import jcckit.transformation.Transformation;
33 import jcckit.util.ConfigParameters;
34
35 /**
36  * A Cartesian coordinate system. One or both axes can be logarithmic.
37  *
38  * @author Franz-Josef Elmer
39  */

40 public class CartesianCoordinateSystem implements CoordinateSystem {
41   /** Configuration parameter key. */
42   public static final String JavaDoc ORIGIN_KEY = "origin",
43                              X_AXIS_KEY = "xAxis",
44                              Y_AXIS_KEY = "yAxis";
45
46   private final CartesianTransformation _transformation;
47   private final GraphicalComposite _view;
48   private final ClippingRectangle _clippingRectangle;
49
50   /**
51    * Creates an instance from the specified configuration parameters.
52    * <table border=1 cellpadding=5>
53    * <tr><th>Key &amp; Default Value</th><th>Type</th><th>Mandatory</th>
54    * <th>Description</th></tr>
55    * <tr><td><tt>origin = 0.15,&nbsp;0.1</tt></td>
56    * <td><tt>double[]</tt></td><td>no</td>
57    * <td>Position (in device-independent coordinates) of the lower-left
58    * corner of the axis box.</td></tr>
59    * <tr><td><tt>xAxis</tt></td>
60    * <td><tt>ConfigParameters</tt></td><td>no</td>
61    * <td>Parameters defining the x-axis. For definitions and default
62    * values see {@link AxisParameters#createXAxis
63    * AxisParameters.createXAxis()}.</td></tr>
64    * <tr><td><tt>yAxis</tt></td>
65    * <td><tt>ConfigParameters</tt></td><td>no</td>
66    * <td>Parameters defining the y-axis. For definitions and default
67    * values see {@link AxisParameters#createYAxis
68    * AxisParameters.createYAxis()}.</td></tr>
69    * </table>
70    */

71   public CartesianCoordinateSystem(ConfigParameters config) {
72     this(new GraphPoint(config.getDoubleArray(ORIGIN_KEY,
73                                               new double[] {0.15, 0.1})),
74          AxisParameters.createXAxis(config.getNode(X_AXIS_KEY)),
75          AxisParameters.createYAxis(config.getNode(Y_AXIS_KEY)));
76   }
77
78   /**
79    * Creates an instance for the specified origin and parameters
80    * of both axes.
81    * @param origin Position (in device-independent coordinates) of the
82    * lower-left corner of the axis box.
83    * @param xAxisParameters Parameters of the x-axis.
84    * @param yAxisParameters Parameters of the y-axis.
85    */

86   public CartesianCoordinateSystem(GraphPoint origin,
87                                    AxisParameters xAxisParameters,
88                                    AxisParameters yAxisParameters) {
89     double x = origin.getX();
90     double y = origin.getY();
91     _transformation = new CartesianTransformation(xAxisParameters.logScale,
92                                                   yAxisParameters.logScale,
93         new DataPoint(xAxisParameters.minimum, yAxisParameters.minimum),
94         new GraphPoint(x, y),
95         new DataPoint(xAxisParameters.maximum, yAxisParameters.maximum),
96         new GraphPoint(x + xAxisParameters.axisLength,
97                        y + yAxisParameters.axisLength));
98     _clippingRectangle = new ClippingRectangle(x, y,
99                                                x + xAxisParameters.axisLength,
100                                                y + yAxisParameters.axisLength);
101     _view = new GraphicalComposite(null);
102     createView(origin, xAxisParameters, yAxisParameters);
103   }
104
105   /** Creates the graphical representation of this coordinate system. */
106   private void createView(GraphPoint origin,
107                           AxisParameters xAxisParameters,
108                           AxisParameters yAxisParameters) {
109     double x0 = origin.getX();
110     double x1 = x0 + xAxisParameters.axisLength;
111     double y0 = origin.getY();
112     double y1 = y0 + yAxisParameters.axisLength;
113     GraphPoint lowerLeftCorner = new GraphPoint(x0, y0);
114     GraphPoint upperLeftCorner = new GraphPoint(x0, y1);
115     GraphPoint lowerRightCorner = new GraphPoint(x1, y0);
116     GraphPoint upperRightCorner = new GraphPoint(x1, y1);
117     LineAttributes xLineAttributes = xAxisParameters.axisAttributes;
118     LineAttributes yLineAttributes = yAxisParameters.axisAttributes;
119     createTicsAndGrid(true, y0, y1, xAxisParameters);
120     createTicsAndGrid(false, x0, x1, yAxisParameters);
121     addLine(lowerLeftCorner, lowerRightCorner, xLineAttributes);
122     addLine(lowerLeftCorner, upperLeftCorner, yLineAttributes);
123     addLine(upperLeftCorner, upperRightCorner, xLineAttributes);
124     addLine(lowerRightCorner, upperRightCorner, yLineAttributes);
125     createLabel(0.5 * (x0 + x1), y0, xAxisParameters);
126     createLabel(x0, 0.5 * (y0 + y1), yAxisParameters);
127   }
128
129   private void createLabel(double x, double y, AxisParameters parameters) {
130     if (parameters.axisLabel.length() > 0) {
131       _view.addElement(new Text(
132           new GraphPoint(x + parameters.axisLabelPosition.getX(),
133                          y + parameters.axisLabelPosition.getY()),
134           parameters.axisLabel, parameters.axisLabelAttributes));
135     }
136   }
137
138   private void createTicsAndGrid(boolean isXAxis, double low, double high,
139                                  AxisParameters parameters) {
140     double[] tics = parameters.calculateTics();
141     int offIndex = isXAxis ? 1 : 0;
142     double[] point = new double[2]; // helper array
143
for (int i = 0; i < tics.length; i++) {
144       point[1 - offIndex] = tics[i];
145       point[offIndex] = 1;
146       GraphPoint gPoint1 =
147           _transformation.transformToGraph(new DataPoint(point[0], point[1]));
148       point[0] = gPoint1.getX();
149       point[1] = gPoint1.getY();
150       point[offIndex] = high;
151       gPoint1 = new GraphPoint(point[0], point[1]);
152       point[offIndex] += parameters.ticLength;
153       addLine(gPoint1, new GraphPoint(point[0], point[1]),
154                                       parameters.ticAttributes);
155       point[offIndex] = low;
156       GraphPoint gPoint2 = new GraphPoint(point[0], point[1]);
157       if (parameters.grid) {
158         addLine(gPoint1, gPoint2, parameters.gridAttributes);
159       }
160       point[offIndex] -= parameters.ticLength;
161       addLine(gPoint2, new GraphPoint(point[0], point[1]),
162                                       parameters.ticAttributes);
163       if (parameters.ticLabelFormat != null) {
164         point[offIndex] += parameters.ticLength;
165         point[0] += parameters.ticLabelPosition.getX();
166         point[1] += parameters.ticLabelPosition.getY();
167         _view.addElement(new Text(new GraphPoint(point[0], point[1]),
168                                   parameters.ticLabelFormat.form(tics[i]),
169                                   parameters.ticLabelAttributes));
170       }
171     }
172   }
173
174   private void addLine(GraphPoint point1, GraphPoint point2,
175                        GraphicAttributes attributes) {
176     Polygon line = new Polygon(attributes, false);
177     line.addPoint(point1);
178     line.addPoint(point2);
179     _view.addElement(line);
180   }
181
182   /**
183    * Returns the graphical representation of the coordinate system.
184    * In each call the same instance is returned.
185    */

186   public GraphicalElement getView() {
187     return _view;
188   }
189
190   /**
191    * Returns the clipping rectangle of specified by the axis.
192    * In each call the same instance is returned.
193    */

194   public ClippingShape getClippingShape() {
195     return _clippingRectangle;
196   }
197
198   /**
199    * Returns the transformation of data coordinates into the device-independent
200    * coordinates of the axis box.
201    * In each call the same instance is returned.
202    */

203   public Transformation getTransformation() {
204     return _transformation;
205   }
206 }
207
Popular Tags