1 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 40 public class CartesianCoordinateSystem implements CoordinateSystem { 41 42 public static final String 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 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 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 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]; 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 186 public GraphicalElement getView() { 187 return _view; 188 } 189 190 194 public ClippingShape getClippingShape() { 195 return _clippingRectangle; 196 } 197 198 203 public Transformation getTransformation() { 204 return _transformation; 205 } 206 } 207 | Popular Tags |