comparison flys-artifacts/src/main/java/de/intevation/flys/exports/LongitudinalSectionGenerator.java @ 1679:69929c471646

Improved the creation/rendering of annotations (km favorites, mainvalues). flys-artifacts/trunk@2896 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 05 Oct 2011 14:23:53 +0000
parents dd9dfe1e48fa
children bdb05dc9b763
comparison
equal deleted inserted replaced
1678:03fbf1b30e72 1679:69929c471646
1 package de.intevation.flys.exports; 1 package de.intevation.flys.exports;
2 2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.apache.log4j.Logger; 3 import org.apache.log4j.Logger;
7 4
8 import org.jfree.chart.JFreeChart; 5 import org.jfree.chart.JFreeChart;
9 import org.jfree.chart.LegendItem;
10 import org.jfree.chart.LegendItemCollection;
11 import org.jfree.chart.axis.NumberAxis; 6 import org.jfree.chart.axis.NumberAxis;
12 import org.jfree.chart.axis.ValueAxis; 7 import org.jfree.chart.axis.ValueAxis;
13 import org.jfree.chart.plot.XYPlot; 8 import org.jfree.chart.plot.XYPlot;
14 import org.jfree.chart.title.TextTitle; 9 import org.jfree.chart.title.TextTitle;
15 import org.jfree.data.Range; 10 import org.jfree.data.Range;
16 import org.jfree.data.xy.XYSeries; 11 import org.jfree.data.xy.XYSeries;
17 import org.jfree.ui.TextAnchor;
18 12
19 import org.w3c.dom.Document; 13 import org.w3c.dom.Document;
20 14
21 import de.intevation.artifacts.Artifact; 15 import de.intevation.artifacts.Artifact;
22 16
26 20
27 import de.intevation.flys.artifacts.model.FacetTypes; 21 import de.intevation.flys.artifacts.model.FacetTypes;
28 import de.intevation.flys.artifacts.model.WQKms; 22 import de.intevation.flys.artifacts.model.WQKms;
29 23
30 import de.intevation.flys.jfree.FLYSAnnotation; 24 import de.intevation.flys.jfree.FLYSAnnotation;
31 import de.intevation.flys.jfree.StickyAxisAnnotation;
32 import de.intevation.flys.model.Annotation;
33 import de.intevation.flys.utils.FLYSUtils; 25 import de.intevation.flys.utils.FLYSUtils;
34 26
35 27
36 /** 28 /**
37 * An OutGenerator that generates discharge curves. 29 * An OutGenerator that generates discharge curves.
70 public static final String I18N_2YAXIS_LABEL_DEFAULT = "Q [m\u00b3/s]"; 62 public static final String I18N_2YAXIS_LABEL_DEFAULT = "Q [m\u00b3/s]";
71 63
72 64
73 protected boolean inverted; 65 protected boolean inverted;
74 66
75 /** List of annotations to insert in plot. */
76 protected List<FLYSAnnotation> annotations;
77
78 67
79 public LongitudinalSectionGenerator() { 68 public LongitudinalSectionGenerator() {
80 super(); 69 super();
81 annotations = new ArrayList<FLYSAnnotation>();
82 } 70 }
83 71
84 72
85 protected String getChartTitle() { 73 protected String getChartTitle() {
86 return msg(I18N_CHART_TITLE, I18N_CHART_TITLE_DEFAULT); 74 return msg(I18N_CHART_TITLE, I18N_CHART_TITLE_DEFAULT);
106 94
107 String subtitle = msg(I18N_CHART_SUBTITLE, "", args); 95 String subtitle = msg(I18N_CHART_SUBTITLE, "", args);
108 chart.addSubtitle(new TextTitle(subtitle)); 96 chart.addSubtitle(new TextTitle(subtitle));
109 } 97 }
110 98
111 @Override
112 public JFreeChart generateChart() {
113 JFreeChart c = super.generateChart();
114 XYPlot p = (XYPlot) c.getPlot();
115
116 redoAnnotations(p, p.getDomainAxis());
117
118 return c;
119 }
120
121 99
122 protected String getXAxisLabel() { 100 protected String getXAxisLabel() {
123 FLYSArtifact flys = (FLYSArtifact) master; 101 FLYSArtifact flys = (FLYSArtifact) master;
124 102
125 return msg( 103 return msg(
148 msg(I18N_2YAXIS_LABEL, I18N_2YAXIS_LABEL_DEFAULT)); 126 msg(I18N_2YAXIS_LABEL, I18N_2YAXIS_LABEL_DEFAULT));
149 127
150 plot.setRangeAxis(1, qAxis); 128 plot.setRangeAxis(1, qAxis);
151 129
152 invertXAxis(plot.getDomainAxis()); 130 invertXAxis(plot.getDomainAxis());
153 }
154
155
156 /**
157 * Remove all annotations from plot and re-insert them at an approximately
158 * okay position. The followed approach is naive but side-effect free.
159 *
160 * @param plot the plot.
161 * @param axis the value axis.
162 */
163 protected void redoAnnotations(XYPlot plot, ValueAxis axis) {
164 plot.clearAnnotations();
165 // TODO Position calculation could/should be done in
166 // the StickyAxisAnnotation-Implementation itself.
167
168 int idx = 0;
169 ValueAxis yAxis = plot.getRangeAxis(idx);
170
171 if (yAxis == null) {
172 if (plot.getRangeAxisCount() >= 2) {
173 yAxis = plot.getRangeAxis(++idx);
174 }
175 }
176
177 if (yAxis == null) {
178 // XXX There is no y-axis that might be used to add annotations. If
179 // we absolutely want to display annotations, we need to create a
180 // virtual dataset for an axis.
181 return;
182 }
183
184 float posY = 140.f;
185 posY = (float) yAxis.getRange().getLowerBound();
186 // Add some (2%) space between Text and axis.
187 posY += 0.02f * (yAxis.getRange().getUpperBound()
188 - yAxis.getRange().getLowerBound());
189
190 LegendItemCollection lic = plot.getLegendItems();
191
192 // Add all annotations.
193 for (FLYSAnnotation fa: annotations) {
194 lic.add(new LegendItem(fa.getLabel()));
195
196 for (Annotation a: fa.getAnnotations()) {
197 float posX = (float) a.getRange().getA().doubleValue();
198 String text = a.getPosition().getValue();
199
200 StickyAxisAnnotation ta = new StickyAxisAnnotation(
201 text,
202 posX,
203 posY);
204
205 double rotation = 270.0f * (Math.PI / 180.0f);
206 ta.setRotationAngle(rotation);
207 ta.setRotationAnchor(TextAnchor.CENTER_LEFT);
208 ta.setTextAnchor(TextAnchor.CENTER_LEFT);
209 plot.getRenderer(idx).addAnnotation(ta);
210 }
211 }
212
213 plot.setFixedLegendItems(lic);
214 } 131 }
215 132
216 133
217 /** 134 /**
218 * Create a range that includes 0 (for the Q axis). 135 * Create a range that includes 0 (for the Q axis).
297 protected void doAnnotationsOut(Object o, Document theme) { 214 protected void doAnnotationsOut(Object o, Document theme) {
298 logger.debug("LongitudinalSectionGenerator.doAnnotationsOut"); 215 logger.debug("LongitudinalSectionGenerator.doAnnotationsOut");
299 216
300 // Add all annotations in list o to our annotation pool. 217 // Add all annotations in list o to our annotation pool.
301 FLYSAnnotation fa = (FLYSAnnotation) o; 218 FLYSAnnotation fa = (FLYSAnnotation) o;
302 annotations.add(fa); 219 addAnnotations(fa);
303 } 220 }
304 221
305 222
306 /** 223 /**
307 * Process the output for W facets in a longitudinal section curve. 224 * Process the output for W facets in a longitudinal section curve.

http://dive4elements.wald.intevation.org