comparison flys-artifacts/src/main/java/de/intevation/flys/jfree/StableXYDifferenceRenderer.java @ 2666:6da7e064ae90

Allow basic and styled area labels, yet on static position within plot. flys-artifacts/trunk@4344 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Thu, 03 May 2012 20:27:01 +0000
parents b5cc53a84b66
children b75681c09ef8
comparison
equal deleted inserted replaced
2665:bcf4011fa3f1 2666:6da7e064ae90
78 package de.intevation.flys.jfree; 78 package de.intevation.flys.jfree;
79 79
80 import java.awt.BasicStroke; 80 import java.awt.BasicStroke;
81 import java.awt.Color; 81 import java.awt.Color;
82 import java.awt.Graphics2D; 82 import java.awt.Graphics2D;
83 import java.awt.Font;
83 import java.awt.Paint; 84 import java.awt.Paint;
84 import java.awt.Shape; 85 import java.awt.Shape;
85 import java.awt.Stroke; 86 import java.awt.Stroke;
86 import java.awt.geom.GeneralPath; 87 import java.awt.geom.GeneralPath;
87 import java.awt.geom.Line2D; 88 import java.awt.geom.Line2D;
135 public class StableXYDifferenceRenderer extends AbstractXYItemRenderer 136 public class StableXYDifferenceRenderer extends AbstractXYItemRenderer
136 implements XYItemRenderer, PublicCloneable { 137 implements XYItemRenderer, PublicCloneable {
137 138
138 private static Logger log = Logger.getLogger(StableXYDifferenceRenderer.class); 139 private static Logger log = Logger.getLogger(StableXYDifferenceRenderer.class);
139 140
140 public static final int CALCULATE_NO_AREA = 0;
141 public static final int CALCULATE_POSITIVE_AREA = 1; 141 public static final int CALCULATE_POSITIVE_AREA = 1;
142 public static final int CALCULATE_NEGATIVE_AREA = 2; 142 public static final int CALCULATE_NEGATIVE_AREA = 2;
143 public static final int CALCULATE_ALL_AREA = 143 public static final int CALCULATE_ALL_AREA =
144 CALCULATE_POSITIVE_AREA | CALCULATE_NEGATIVE_AREA; 144 CALCULATE_POSITIVE_AREA | CALCULATE_NEGATIVE_AREA;
145 145
167 /** The shape to display in the legend item. */ 167 /** The shape to display in the legend item. */
168 private transient Shape legendShape; 168 private transient Shape legendShape;
169 169
170 protected boolean drawOriginalSeries; 170 protected boolean drawOriginalSeries;
171 171
172 /** The color of the label showing the calculated area. */
173 protected Color labelColor;
174
175 /** Font to draw label of calculated area with. */
176 protected Font labelFont;
177
172 protected int areaCalculationMode; 178 protected int areaCalculationMode;
173 179
174 protected double positiveArea; 180 protected double positiveArea;
175 protected double negativeArea; 181 protected double negativeArea;
182
183 /** Whether or not to draw a label in the area. */
184 protected boolean labelArea = true;
176 185
177 186
178 /** 187 /**
179 * This flag controls whether or not the x-coordinates (in Java2D space) 188 * This flag controls whether or not the x-coordinates (in Java2D space)
180 * are rounded to integers. When set to true, this can avoid the vertical 189 * are rounded to integers. When set to true, this can avoid the vertical
193 this(Color.green, Color.red, false /*, null */); 202 this(Color.green, Color.red, false /*, null */);
194 } 203 }
195 204
196 public StableXYDifferenceRenderer(Paint positivePaint, Paint negativePaint, 205 public StableXYDifferenceRenderer(Paint positivePaint, Paint negativePaint,
197 boolean shapes) { 206 boolean shapes) {
198 this(positivePaint, negativePaint, shapes, CALCULATE_NO_AREA); 207 this(positivePaint, negativePaint, shapes, CALCULATE_ALL_AREA);
199 } 208 }
200 209
201 /** 210 /**
202 * Creates a new renderer. 211 * Creates a new renderer.
203 * 212 *
235 244
236 public void setAreaCalculationMode(int areaCalculationMode) { 245 public void setAreaCalculationMode(int areaCalculationMode) {
237 this.areaCalculationMode = areaCalculationMode; 246 this.areaCalculationMode = areaCalculationMode;
238 } 247 }
239 248
249 public boolean isLabelArea() {
250 return this.labelArea;
251 }
252
253 public void setLabelArea(boolean label) {
254 this.labelArea = label;
255 }
256
257
258 /** Set font to paint label with. */
259 public void setLabelFont(Font font) {
260 this.labelFont = font;
261 }
262
263
264 /** Get font with which label is painted. */
265 public Font getLabelFont() {
266 return this.labelFont;
267 }
268
269
270 /** Set color with which to paint label. */
271 public void setLabelColor(Color color) {
272 this.labelColor = color;
273 }
274
275
276 /** Get color with which label is painted. */
277 public Color getLabelColor() {
278 return this.labelColor;
279 }
240 280
241 281
242 public double getCalculatedArea() { 282 public double getCalculatedArea() {
243 return positiveArea + negativeArea; 283 return positiveArea + negativeArea;
244 } 284 }
805 case 1: 845 case 1:
806 drawItemPass1(g2, dataArea, info, 846 drawItemPass1(g2, dataArea, info,
807 plot, domainAxis, rangeAxis, 847 plot, domainAxis, rangeAxis,
808 dataset, series, item, crosshairState); 848 dataset, series, item, crosshairState);
809 } 849 }
850
851 // Find geometric middle, calculate area and paint a string with it here.
852 // TODO also i18n
853 if (pass == 1 && this.labelArea) {
854 float center_x = 100f;
855 float center_y = 100f+ pass*50f;
856 float area = 0f;
857 if (areaCalculationMode == CALCULATE_POSITIVE_AREA
858 || areaCalculationMode == CALCULATE_ALL_AREA) {
859 area += Math.abs(positiveArea);
860 }
861 if (areaCalculationMode == CALCULATE_NEGATIVE_AREA
862 || areaCalculationMode == CALCULATE_ALL_AREA) {
863 area += Math.abs(negativeArea);
864 }
865 // TODO respect text bg settings.
866 if (area != 0f) {
867 Color oldColor = g2.getColor();
868 Font oldFont = g2.getFont();
869 g2.setFont(labelFont);
870 g2.setColor(labelColor);
871 g2.drawString("Area= "+area+"m2", center_x, center_y);
872 g2.setFont(oldFont);
873 g2.setColor(oldColor);
874 }
875 }
810 } 876 }
811 877
812 /** 878 /**
813 * Draws the visual representation of a single data item, first pass. 879 * Draws the visual representation of a single data item, first pass.
814 * 880 *
1432 1498
1433 return ((l_minuendLast < l_subtrahendFirst) 1499 return ((l_minuendLast < l_subtrahendFirst)
1434 || (l_subtrahendLast < l_minuendFirst)); 1500 || (l_subtrahendLast < l_minuendFirst));
1435 } 1501 }
1436 1502
1503
1437 public static double calculateArea(Object [] xValues, Object [] yValues) { 1504 public static double calculateArea(Object [] xValues, Object [] yValues) {
1438 double area = 0d; 1505 double area = 0d;
1439 1506
1440 for (int i = 0, N = xValues.length; i < N; ++i) { 1507 for (int i = 0, N = xValues.length; i < N; ++i) {
1441 int j = (i + 1) % N; 1508 int j = (i + 1) % N;
1481 RectangleEdge l_rangeAxisLocation = x_plot.getRangeAxisEdge(); 1548 RectangleEdge l_rangeAxisLocation = x_plot.getRangeAxisEdge();
1482 1549
1483 Object[] l_xValues = x_xValues.toArray(); 1550 Object[] l_xValues = x_xValues.toArray();
1484 Object[] l_yValues = x_yValues.toArray(); 1551 Object[] l_yValues = x_yValues.toArray();
1485 1552
1486 int acm = areaCalculationMode; 1553 double area = calculateArea(l_xValues, l_yValues);
1487 1554 if (x_positive) positiveArea += area;
1488 if (acm != CALCULATE_NO_AREA) { 1555 else negativeArea += 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 }
1499 1556
1500 GeneralPath l_path = new GeneralPath(); 1557 GeneralPath l_path = new GeneralPath();
1501 1558
1502 if (PlotOrientation.VERTICAL == l_orientation) { 1559 if (PlotOrientation.VERTICAL == l_orientation) {
1503 double l_x = x_domainAxis.valueToJava2D(( 1560 double l_x = x_domainAxis.valueToJava2D((

http://dive4elements.wald.intevation.org