comparison flys-artifacts/src/main/java/de/intevation/flys/jfree/StableXYDifferenceRenderer.java @ 2108:b5cc53a84b66

StableXYDifferenceRenderer: Added code to calculate the area of the generated polygons. flys-artifacts/trunk@3667 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 12 Jan 2012 16:49:00 +0000
parents 51b9899f819d
children 6da7e064ae90
comparison
equal deleted inserted replaced
2107:51b9899f819d 2108:b5cc53a84b66
135 public class StableXYDifferenceRenderer extends AbstractXYItemRenderer 135 public class StableXYDifferenceRenderer extends AbstractXYItemRenderer
136 implements XYItemRenderer, PublicCloneable { 136 implements XYItemRenderer, PublicCloneable {
137 137
138 private static Logger log = Logger.getLogger(StableXYDifferenceRenderer.class); 138 private static Logger log = Logger.getLogger(StableXYDifferenceRenderer.class);
139 139
140 public static final int CALCULATE_NO_AREA = 0;
141 public static final int CALCULATE_POSITIVE_AREA = 1;
142 public static final int CALCULATE_NEGATIVE_AREA = 2;
143 public static final int CALCULATE_ALL_AREA =
144 CALCULATE_POSITIVE_AREA | CALCULATE_NEGATIVE_AREA;
145
140 /** For serialization. */ 146 /** For serialization. */
141 private static final long serialVersionUID = -8447915602375584857L; 147 private static final long serialVersionUID = -8447915602375584857L;
142 148
143 /** The paint used to highlight positive differences (y(0) > y(1)). */ 149 /** The paint used to highlight positive differences (y(0) > y(1)). */
144 private transient Paint positivePaint; 150 private transient Paint positivePaint;
161 /** The shape to display in the legend item. */ 167 /** The shape to display in the legend item. */
162 private transient Shape legendShape; 168 private transient Shape legendShape;
163 169
164 protected boolean drawOriginalSeries; 170 protected boolean drawOriginalSeries;
165 171
166 //private XYDatasetToZeroMapper mapper; 172 protected int areaCalculationMode;
173
174 protected double positiveArea;
175 protected double negativeArea;
176
167 177
168 /** 178 /**
169 * This flag controls whether or not the x-coordinates (in Java2D space) 179 * This flag controls whether or not the x-coordinates (in Java2D space)
170 * are rounded to integers. When set to true, this can avoid the vertical 180 * are rounded to integers. When set to true, this can avoid the vertical
171 * striping that anti-aliasing can generate. However, the rounding may not 181 * striping that anti-aliasing can generate. However, the rounding may not
181 */ 191 */
182 public StableXYDifferenceRenderer() { 192 public StableXYDifferenceRenderer() {
183 this(Color.green, Color.red, false /*, null */); 193 this(Color.green, Color.red, false /*, null */);
184 } 194 }
185 195
196 public StableXYDifferenceRenderer(Paint positivePaint, Paint negativePaint,
197 boolean shapes) {
198 this(positivePaint, negativePaint, shapes, CALCULATE_NO_AREA);
199 }
200
186 /** 201 /**
187 * Creates a new renderer. 202 * Creates a new renderer.
188 * 203 *
189 * @param positivePaint the highlight color for positive differences 204 * @param positivePaint the highlight color for positive differences
190 * (<code>null</code> not permitted). 205 * (<code>null</code> not permitted).
191 * @param negativePaint the highlight color for negative differences 206 * @param negativePaint the highlight color for negative differences
192 * (<code>null</code> not permitted). 207 * (<code>null</code> not permitted).
193 * @param shapes draw shapes? 208 * @param shapes draw shapes?
194 */ 209 */
195 public StableXYDifferenceRenderer(Paint positivePaint, Paint negativePaint, 210 public StableXYDifferenceRenderer(Paint positivePaint, Paint negativePaint,
196 boolean shapes) { 211 boolean shapes, int areaCalculationMode) {
197 if (positivePaint == null) { 212 if (positivePaint == null) {
198 throw new IllegalArgumentException( 213 throw new IllegalArgumentException(
199 "Null 'positivePaint' argument."); 214 "Null 'positivePaint' argument.");
200 } 215 }
201 if (negativePaint == null) { 216 if (negativePaint == null) {
209 this.roundXCoordinates = false; 224 this.roundXCoordinates = false;
210 this.drawOutline = true; 225 this.drawOutline = true;
211 this.outlineStroke = new BasicStroke(1); 226 this.outlineStroke = new BasicStroke(1);
212 this.outlinePaint = Color.black; 227 this.outlinePaint = Color.black;
213 this.drawOriginalSeries = false; 228 this.drawOriginalSeries = false;
214 } 229 this.areaCalculationMode = areaCalculationMode;
215 230 }
231
232 public int getAreaCalculationMode() {
233 return areaCalculationMode;
234 }
235
236 public void setAreaCalculationMode(int areaCalculationMode) {
237 this.areaCalculationMode = areaCalculationMode;
238 }
239
240
241
242 public double getCalculatedArea() {
243 return positiveArea + negativeArea;
244 }
216 245
217 /** 246 /**
218 * Sets color that is used if drawOutline is true. 247 * Sets color that is used if drawOutline is true.
219 */ 248 */
220 public void setOutlinePaint(Paint outlinePaint) { 249 public void setOutlinePaint(Paint outlinePaint) {
1403 1432
1404 return ((l_minuendLast < l_subtrahendFirst) 1433 return ((l_minuendLast < l_subtrahendFirst)
1405 || (l_subtrahendLast < l_minuendFirst)); 1434 || (l_subtrahendLast < l_minuendFirst));
1406 } 1435 }
1407 1436
1437 public static double calculateArea(Object [] xValues, Object [] yValues) {
1438 double area = 0d;
1439
1440 for (int i = 0, N = xValues.length; i < N; ++i) {
1441 int j = (i + 1) % N;
1442 double xi = ((Double)xValues[i]).doubleValue();
1443 double yi = ((Double)yValues[i]).doubleValue();
1444 double xj = ((Double)xValues[j]).doubleValue();
1445 double yj = ((Double)yValues[j]).doubleValue();
1446
1447 area += xi*yj;
1448 area -= xj*yi;
1449 }
1450
1451 return 0.5d*area;
1452 }
1453
1408 /** 1454 /**
1409 * Draws the visual representation of a polygon 1455 * Draws the visual representation of a polygon
1410 * 1456 *
1411 * @param x_graphics the graphics device. 1457 * @param x_graphics the graphics device.
1412 * @param x_dataArea the area within which the data is being drawn. 1458 * @param x_dataArea the area within which the data is being drawn.
1434 RectangleEdge l_domainAxisLocation = x_plot.getDomainAxisEdge(); 1480 RectangleEdge l_domainAxisLocation = x_plot.getDomainAxisEdge();
1435 RectangleEdge l_rangeAxisLocation = x_plot.getRangeAxisEdge(); 1481 RectangleEdge l_rangeAxisLocation = x_plot.getRangeAxisEdge();
1436 1482
1437 Object[] l_xValues = x_xValues.toArray(); 1483 Object[] l_xValues = x_xValues.toArray();
1438 Object[] l_yValues = x_yValues.toArray(); 1484 Object[] l_yValues = x_yValues.toArray();
1485
1486 int acm = areaCalculationMode;
1487
1488 if (acm != CALCULATE_NO_AREA) {
1489 if ((x_positive && ((acm|CALCULATE_POSITIVE_AREA)
1490 == CALCULATE_POSITIVE_AREA))
1491 || (!x_positive && ((acm|CALCULATE_NEGATIVE_AREA)
1492 == CALCULATE_NEGATIVE_AREA))
1493 ) {
1494 double area = calculateArea(l_xValues, l_yValues);
1495 if (x_positive) positiveArea += area;
1496 else negativeArea += area;
1497 }
1498 }
1439 1499
1440 GeneralPath l_path = new GeneralPath(); 1500 GeneralPath l_path = new GeneralPath();
1441 1501
1442 if (PlotOrientation.VERTICAL == l_orientation) { 1502 if (PlotOrientation.VERTICAL == l_orientation) {
1443 double l_x = x_domainAxis.valueToJava2D(( 1503 double l_x = x_domainAxis.valueToJava2D((

http://dive4elements.wald.intevation.org