Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/jfree/StableXYDifferenceRenderer.java @ 2667:b75681c09ef8
Respect area label bg text style, draw label roughly at centroid of polygons (sofar, better but not yet good).
flys-artifacts/trunk@4346 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Fri, 04 May 2012 13:47:37 +0000 |
parents | 6da7e064ae90 |
children | 5642a83420f2 |
comparison
equal
deleted
inserted
replaced
2666:6da7e064ae90 | 2667:b75681c09ef8 |
---|---|
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.Font; |
84 import java.awt.Paint; | 84 import java.awt.Paint; |
85 import java.awt.geom.Point2D; | |
85 import java.awt.Shape; | 86 import java.awt.Shape; |
86 import java.awt.Stroke; | 87 import java.awt.Stroke; |
87 import java.awt.geom.GeneralPath; | 88 import java.awt.geom.GeneralPath; |
88 import java.awt.geom.Line2D; | 89 import java.awt.geom.Line2D; |
89 import java.awt.geom.Rectangle2D; | 90 import java.awt.geom.Rectangle2D; |
170 protected boolean drawOriginalSeries; | 171 protected boolean drawOriginalSeries; |
171 | 172 |
172 /** The color of the label showing the calculated area. */ | 173 /** The color of the label showing the calculated area. */ |
173 protected Color labelColor; | 174 protected Color labelColor; |
174 | 175 |
176 /** The background color of the label showing the calculated area. */ | |
177 protected Color labelBGColor; | |
178 | |
175 /** Font to draw label of calculated area with. */ | 179 /** Font to draw label of calculated area with. */ |
176 protected Font labelFont; | 180 protected Font labelFont; |
177 | 181 |
178 protected int areaCalculationMode; | 182 protected int areaCalculationMode; |
179 | 183 |
180 protected double positiveArea; | 184 protected double positiveArea; |
181 protected double negativeArea; | 185 protected double negativeArea; |
182 | 186 |
183 /** Whether or not to draw a label in the area. */ | 187 /** Whether or not to draw a label in the area. */ |
184 protected boolean labelArea = true; | 188 protected boolean labelArea = true; |
189 | |
190 | |
191 /** Arithmetic centroid of drawn polygons. */ | |
192 protected Point2D.Double centroid; | |
193 | |
194 | |
195 /** Number of points that contributed to the centroid. */ | |
196 protected int centroidNPoints = 0; | |
185 | 197 |
186 | 198 |
187 /** | 199 /** |
188 * This flag controls whether or not the x-coordinates (in Java2D space) | 200 * This flag controls whether or not the x-coordinates (in Java2D space) |
189 * are rounded to integers. When set to true, this can avoid the vertical | 201 * are rounded to integers. When set to true, this can avoid the vertical |
234 this.drawOutline = true; | 246 this.drawOutline = true; |
235 this.outlineStroke = new BasicStroke(1); | 247 this.outlineStroke = new BasicStroke(1); |
236 this.outlinePaint = Color.black; | 248 this.outlinePaint = Color.black; |
237 this.drawOriginalSeries = false; | 249 this.drawOriginalSeries = false; |
238 this.areaCalculationMode = areaCalculationMode; | 250 this.areaCalculationMode = areaCalculationMode; |
251 this.labelBGColor = null; | |
252 this.centroid = new Point2D.Double(0,0); | |
239 } | 253 } |
240 | 254 |
241 public int getAreaCalculationMode() { | 255 public int getAreaCalculationMode() { |
242 return areaCalculationMode; | 256 return areaCalculationMode; |
243 } | 257 } |
274 | 288 |
275 | 289 |
276 /** Get color with which label is painted. */ | 290 /** Get color with which label is painted. */ |
277 public Color getLabelColor() { | 291 public Color getLabelColor() { |
278 return this.labelColor; | 292 return this.labelColor; |
293 } | |
294 | |
295 | |
296 /** Set color with which to paint label bg. */ | |
297 public void setLabelBGColor(Color color) { | |
298 this.labelBGColor = color; | |
299 } | |
300 | |
301 | |
302 /** Get color with which label is painted. */ | |
303 public Color getLabelBGColor() { | |
304 return this.labelBGColor; | |
279 } | 305 } |
280 | 306 |
281 | 307 |
282 public double getCalculatedArea() { | 308 public double getCalculatedArea() { |
283 return positiveArea + negativeArea; | 309 return positiveArea + negativeArea; |
849 } | 875 } |
850 | 876 |
851 // Find geometric middle, calculate area and paint a string with it here. | 877 // Find geometric middle, calculate area and paint a string with it here. |
852 // TODO also i18n | 878 // TODO also i18n |
853 if (pass == 1 && this.labelArea) { | 879 if (pass == 1 && this.labelArea) { |
854 float center_x = 100f; | 880 double center_x = centroid.getX(); |
855 float center_y = 100f+ pass*50f; | 881 double center_y = centroid.getY(); |
882 center_x = domainAxis.valueToJava2D(center_x, dataArea, | |
883 plot.getDomainAxisEdge()); | |
884 center_y = rangeAxis.valueToJava2D(center_y, dataArea, | |
885 plot.getRangeAxisEdge()); | |
886 | |
887 // Respect text-extend if text should appear really centered. | |
888 | |
856 float area = 0f; | 889 float area = 0f; |
857 if (areaCalculationMode == CALCULATE_POSITIVE_AREA | 890 if (areaCalculationMode == CALCULATE_POSITIVE_AREA |
858 || areaCalculationMode == CALCULATE_ALL_AREA) { | 891 || areaCalculationMode == CALCULATE_ALL_AREA) { |
859 area += Math.abs(positiveArea); | 892 area += Math.abs(positiveArea); |
860 } | 893 } |
861 if (areaCalculationMode == CALCULATE_NEGATIVE_AREA | 894 if (areaCalculationMode == CALCULATE_NEGATIVE_AREA |
862 || areaCalculationMode == CALCULATE_ALL_AREA) { | 895 || areaCalculationMode == CALCULATE_ALL_AREA) { |
863 area += Math.abs(negativeArea); | 896 area += Math.abs(negativeArea); |
864 } | 897 } |
865 // TODO respect text bg settings. | |
866 if (area != 0f) { | 898 if (area != 0f) { |
867 Color oldColor = g2.getColor(); | 899 Color oldColor = g2.getColor(); |
868 Font oldFont = g2.getFont(); | 900 Font oldFont = g2.getFont(); |
869 g2.setFont(labelFont); | 901 g2.setFont(labelFont); |
902 String labelText = "Area= " + area + "m2"; | |
903 if (labelBGColor != null) { | |
904 EnhancedLineAndShapeRenderer.drawTextBox(g2, labelText, | |
905 (float)center_x, (float)center_y, labelBGColor); | |
906 } | |
870 g2.setColor(labelColor); | 907 g2.setColor(labelColor); |
871 g2.drawString("Area= "+area+"m2", center_x, center_y); | 908 g2.drawString(labelText, (float)center_x, (float)center_y); |
872 g2.setFont(oldFont); | 909 g2.setFont(oldFont); |
873 g2.setColor(oldColor); | 910 g2.setColor(oldColor); |
874 } | 911 } |
875 } | 912 } |
876 } | 913 } |
1499 return ((l_minuendLast < l_subtrahendFirst) | 1536 return ((l_minuendLast < l_subtrahendFirst) |
1500 || (l_subtrahendLast < l_minuendFirst)); | 1537 || (l_subtrahendLast < l_minuendFirst)); |
1501 } | 1538 } |
1502 | 1539 |
1503 | 1540 |
1541 public void updateCentroid(Object [] xValues, Object [] yValues) { | |
1542 double x = 0d, y = 0d; | |
1543 | |
1544 for (int i = 0, N = xValues.length; i < N; ++i) { | |
1545 x += ((Double)xValues[i]).doubleValue(); | |
1546 y += ((Double)yValues[i]).doubleValue(); | |
1547 } | |
1548 | |
1549 x /= xValues.length; | |
1550 y /= yValues.length; | |
1551 | |
1552 centroidNPoints++; | |
1553 double factorNew = 1d / centroidNPoints; | |
1554 double factorOld = 1d - factorNew; | |
1555 | |
1556 centroid = new Point2D.Double((factorNew * x + factorOld * centroid.x), | |
1557 (factorNew * y + factorOld * centroid.y)); | |
1558 } | |
1559 | |
1560 | |
1504 public static double calculateArea(Object [] xValues, Object [] yValues) { | 1561 public static double calculateArea(Object [] xValues, Object [] yValues) { |
1505 double area = 0d; | 1562 double area = 0d; |
1506 | 1563 |
1507 for (int i = 0, N = xValues.length; i < N; ++i) { | 1564 for (int i = 0, N = xValues.length; i < N; ++i) { |
1508 int j = (i + 1) % N; | 1565 int j = (i + 1) % N; |
1511 double xj = ((Double)xValues[j]).doubleValue(); | 1568 double xj = ((Double)xValues[j]).doubleValue(); |
1512 double yj = ((Double)yValues[j]).doubleValue(); | 1569 double yj = ((Double)yValues[j]).doubleValue(); |
1513 | 1570 |
1514 area += xi*yj; | 1571 area += xi*yj; |
1515 area -= xj*yi; | 1572 area -= xj*yi; |
1573 // TODO centroid calculation here? | |
1516 } | 1574 } |
1517 | 1575 |
1518 return 0.5d*area; | 1576 return 0.5d*area; |
1519 } | 1577 } |
1520 | 1578 |
1551 Object[] l_yValues = x_yValues.toArray(); | 1609 Object[] l_yValues = x_yValues.toArray(); |
1552 | 1610 |
1553 double area = calculateArea(l_xValues, l_yValues); | 1611 double area = calculateArea(l_xValues, l_yValues); |
1554 if (x_positive) positiveArea += area; | 1612 if (x_positive) positiveArea += area; |
1555 else negativeArea += area; | 1613 else negativeArea += area; |
1614 updateCentroid(l_xValues, l_yValues); | |
1556 | 1615 |
1557 GeneralPath l_path = new GeneralPath(); | 1616 GeneralPath l_path = new GeneralPath(); |
1558 | 1617 |
1559 if (PlotOrientation.VERTICAL == l_orientation) { | 1618 if (PlotOrientation.VERTICAL == l_orientation) { |
1560 double l_x = x_domainAxis.valueToJava2D(( | 1619 double l_x = x_domainAxis.valueToJava2D(( |