Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/exports/LongitudinalSectionGenerator.java @ 1036:e6aff80b59ff
Added proof-of-concept collision detection for text annotations.
flys-artifacts/trunk@2497 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Wed, 17 Aug 2011 09:16:20 +0000 |
parents | 9f69a5f0af98 |
children | 75cf1b11c97e |
comparison
equal
deleted
inserted
replaced
1035:9f69a5f0af98 | 1036:e6aff80b59ff |
---|---|
1 package de.intevation.flys.exports; | 1 package de.intevation.flys.exports; |
2 | 2 |
3 import java.util.ArrayList; | 3 import java.util.ArrayList; |
4 import java.util.Iterator; | |
4 import java.util.List; | 5 import java.util.List; |
5 | 6 |
6 import org.apache.log4j.Logger; | 7 import org.apache.log4j.Logger; |
8 | |
9 import java.awt.Shape; | |
10 import java.awt.geom.Rectangle2D; | |
7 | 11 |
8 import org.jfree.chart.annotations.XYLineAnnotation; | 12 import org.jfree.chart.annotations.XYLineAnnotation; |
9 import org.jfree.chart.annotations.XYTextAnnotation; | 13 import org.jfree.chart.annotations.XYTextAnnotation; |
10 import org.jfree.chart.JFreeChart; | 14 import org.jfree.chart.JFreeChart; |
11 import org.jfree.chart.axis.NumberAxis; | 15 import org.jfree.chart.axis.NumberAxis; |
14 import org.jfree.chart.title.TextTitle; | 18 import org.jfree.chart.title.TextTitle; |
15 import org.jfree.data.Range; | 19 import org.jfree.data.Range; |
16 import org.jfree.data.xy.XYSeries; | 20 import org.jfree.data.xy.XYSeries; |
17 import org.jfree.ui.TextAnchor; | 21 import org.jfree.ui.TextAnchor; |
18 import org.jfree.chart.plot.PlotRenderingInfo; | 22 import org.jfree.chart.plot.PlotRenderingInfo; |
23 import org.jfree.text.TextUtilities; | |
24 import org.jfree.chart.entity.XYAnnotationEntity; | |
25 import org.jfree.chart.plot.PlotOrientation; | |
26 import org.jfree.ui.RectangleEdge; | |
27 import org.jfree.chart.plot.Plot; | |
28 import org.jfree.chart.ChartRenderingInfo; | |
19 | 29 |
20 import org.w3c.dom.Document; | 30 import org.w3c.dom.Document; |
21 | 31 |
22 import de.intevation.artifacts.Artifact; | 32 import de.intevation.artifacts.Artifact; |
23 | 33 |
47 super(text, x, y); | 57 super(text, x, y); |
48 } | 58 } |
49 | 59 |
50 | 60 |
51 /** | 61 /** |
52 * Yet trivial draw without implementing any CustomAnnotation-specific | 62 * Draw the Annotiation if it does not collide with other already drawn |
53 * feature. | 63 * Annotations. |
54 */ | 64 * |
65 * @param g2 the graphics device. | |
66 * @param plot the plot. | |
67 * @param dataArea the data area. | |
68 * @param domainAxis the domain axis. | |
69 * @param rangeAxis the range axis. | |
70 * @param rendererIndex the render index. | |
71 * @param info state information, escpecially collects info about | |
72 * already drawn shapes (and thus annotations), used | |
73 * for collision detection. | |
74 */ | |
75 @Override | |
55 public void draw( | 76 public void draw( |
56 java.awt.Graphics2D g2, | 77 java.awt.Graphics2D g2, |
57 XYPlot plot, | 78 XYPlot plot, |
58 java.awt.geom.Rectangle2D dataArea, | 79 java.awt.geom.Rectangle2D dataArea, |
59 ValueAxis domainAxis, | 80 ValueAxis domainAxis, |
60 ValueAxis rangeAxis, | 81 ValueAxis rangeAxis, |
61 int rendererIndex, | 82 int rendererIndex, |
62 PlotRenderingInfo info) { | 83 PlotRenderingInfo info) { |
84 | |
85 if (info == null) | |
86 return; | |
87 | |
88 // Calculate bounding box as in super.draw(). | |
89 // TODO overwrite draw such that even if the annotation gets painted | |
90 // the bounding box has to be calculated only once. | |
91 ChartRenderingInfo chartInfo = info.getOwner(); | |
92 | |
93 PlotOrientation orientation = plot.getOrientation(); | |
94 RectangleEdge domainEdge = Plot.resolveDomainAxisLocation( | |
95 plot.getDomainAxisLocation(), orientation); | |
96 RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation( | |
97 plot.getRangeAxisLocation(), orientation); | |
98 float anchorX = (float) domainAxis.valueToJava2D( | |
99 getX(), dataArea, domainEdge); | |
100 float anchorY = (float) rangeAxis.valueToJava2D( | |
101 getY(), dataArea, rangeEdge); | |
102 if (orientation == PlotOrientation.HORIZONTAL) { | |
103 float tempAnchor = anchorX; | |
104 anchorX = anchorY; | |
105 anchorY = tempAnchor; | |
106 } | |
107 | |
108 Shape hotspot = TextUtilities.calculateRotatedStringBounds( | |
109 getText(), g2, anchorX, anchorY, getTextAnchor(), | |
110 getRotationAngle(), getRotationAnchor()); | |
111 Rectangle2D hotspotBox = hotspot.getBounds2D(); | |
112 | |
113 // Check for collisions with other XYAnnotations. | |
114 for (Iterator i = chartInfo.getEntityCollection().iterator(); | |
115 i.hasNext(); ) { | |
116 Object next = i.next(); | |
117 // Collision with other stuff than XYAnnotations are okay. | |
118 if (next instanceof XYAnnotationEntity) { | |
119 XYAnnotationEntity drawnShape = (XYAnnotationEntity) next; | |
120 if (drawnShape.getArea().intersects(hotspotBox)) { | |
121 // Found collision, early stop. | |
122 return; | |
123 } | |
124 } | |
125 } | |
126 | |
127 // Set URL of current annotation. This will let super.draw() add | |
128 // the relevant info to the PlotRenderingInfo. | |
129 setURL(""); | |
63 super.draw(g2, plot, dataArea, domainAxis, rangeAxis, rendererIndex, info); | 130 super.draw(g2, plot, dataArea, domainAxis, rangeAxis, rendererIndex, info); |
64 } | 131 } |
65 } | 132 } |
66 | 133 |
67 | 134 |
180 posY = (float) yAxis.getRange().getLowerBound(); | 247 posY = (float) yAxis.getRange().getLowerBound(); |
181 // Lets add some (1%) space between Text and Axis . | 248 // Lets add some (1%) space between Text and Axis . |
182 posY += 0.1f * (yAxis.getRange().getUpperBound() | 249 posY += 0.1f * (yAxis.getRange().getUpperBound() |
183 - yAxis.getRange().getLowerBound()); | 250 - yAxis.getRange().getLowerBound()); |
184 } | 251 } |
185 double freeFrom = axis.getRange().getLowerBound(); | 252 |
186 //logger.warn("Axis lower Bound: " + freeFrom); | 253 // Add all annotations. |
187 | |
188 // Assuming ordered annotations. | |
189 for (Annotation a: annotations) { | 254 for (Annotation a: annotations) { |
190 float posX = (float) a.getRange().getA().doubleValue(); | 255 float posX = (float) a.getRange().getA().doubleValue(); |
191 if (posX < freeFrom) { continue; } | |
192 String text = a.getPosition().getValue(); | 256 String text = a.getPosition().getValue(); |
193 | 257 |
194 XYTextAnnotation ta = new CustomAnnotation(text, posX, posY); | 258 XYTextAnnotation ta = new CustomAnnotation(text, posX, posY); |
195 double rotation = 270.0f * (Math.PI / 180.0f); | 259 double rotation = 270.0f * (Math.PI / 180.0f); |
196 ta.setRotationAngle(rotation); | 260 ta.setRotationAngle(rotation); |
197 ta.setRotationAnchor(TextAnchor.CENTER_LEFT); | 261 ta.setRotationAnchor(TextAnchor.CENTER_LEFT); |
198 ta.setTextAnchor(TextAnchor.CENTER_LEFT); | 262 ta.setTextAnchor(TextAnchor.CENTER_LEFT); |
199 plot.getRenderer().addAnnotation(ta); | 263 plot.getRenderer().addAnnotation(ta); |
264 // TODO Merge XYLineAnnotation and CustomAnnotation. | |
200 XYLineAnnotation la = new XYLineAnnotation(posX, 0, posX, posY); | 265 XYLineAnnotation la = new XYLineAnnotation(posX, 0, posX, posY); |
201 plot.getRenderer().addAnnotation(la); | 266 plot.getRenderer().addAnnotation(la); |
202 freeFrom += 2.0; | |
203 } | 267 } |
204 } | 268 } |
205 | 269 |
206 | 270 |
207 /** | 271 /** |