comparison flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java @ 1684:bdb05dc9b763

Bugfix: #353 Enabled chart's to be drawn with proper axes set even if no data is contained. flys-artifacts/trunk@2902 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 07 Oct 2011 10:51:09 +0000
parents 69929c471646
children 022f62c75878
comparison
equal deleted inserted replaced
1683:acb4d20b130e 1684:bdb05dc9b763
7 import java.io.IOException; 7 import java.io.IOException;
8 8
9 import java.text.NumberFormat; 9 import java.text.NumberFormat;
10 10
11 import java.util.ArrayList; 11 import java.util.ArrayList;
12 import java.util.HashMap;
12 import java.util.List; 13 import java.util.List;
14 import java.util.Map;
13 15
14 import org.w3c.dom.Document; 16 import org.w3c.dom.Document;
15 17
16 import org.apache.log4j.Logger; 18 import org.apache.log4j.Logger;
17 19
25 import org.jfree.chart.plot.PlotOrientation; 27 import org.jfree.chart.plot.PlotOrientation;
26 import org.jfree.chart.plot.XYPlot; 28 import org.jfree.chart.plot.XYPlot;
27 import org.jfree.chart.renderer.xy.XYItemRenderer; 29 import org.jfree.chart.renderer.xy.XYItemRenderer;
28 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; 30 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
29 import org.jfree.data.Range; 31 import org.jfree.data.Range;
30 import org.jfree.data.xy.XYDataset;
31 import org.jfree.data.xy.XYSeries; 32 import org.jfree.data.xy.XYSeries;
32 import org.jfree.data.xy.XYSeriesCollection; 33 import org.jfree.data.xy.XYSeriesCollection;
33 34
34 import org.jfree.ui.RectangleInsets; 35 import org.jfree.ui.RectangleInsets;
35 36
55 /** SeriesCollection used for the second axis. */ 56 /** SeriesCollection used for the second axis. */
56 protected XYSeriesCollection second; 57 protected XYSeriesCollection second;
57 58
58 /** List of annotations to insert in plot. */ 59 /** List of annotations to insert in plot. */
59 protected List<FLYSAnnotation> annotations; 60 protected List<FLYSAnnotation> annotations;
61
62 /** The max X range that includes all X values of all series for each axis.*/
63 protected Map<Integer, Range> xRanges;
64
65 /** The max Y range that includes all Y values of all series for each axis.*/
66 protected Map<Integer, Range> yRanges;
60 67
61 68
62 public static final Color DEFAULT_GRID_COLOR = Color.GRAY; 69 public static final Color DEFAULT_GRID_COLOR = Color.GRAY;
63 public static final float DEFAULT_GRID_LINE_WIDTH = 0.3f; 70 public static final float DEFAULT_GRID_LINE_WIDTH = 0.3f;
64 71
147 plot.setDataset(1, second); 154 plot.setDataset(1, second);
148 } 155 }
149 } 156 }
150 157
151 158
152 public void addFirstAxisSeries(XYSeries series) { 159 public void addFirstAxisSeries(XYSeries series, boolean visible) {
153 if (first == null) { 160 if (first == null) {
154 first = new XYSeriesCollection(); 161 first = new XYSeriesCollection();
155 } 162 }
156 163
157 if (series != null) { 164 if (series != null) {
158 first.addSeries(series); 165 if (visible) {
159 } 166 first.addSeries(series);
160 } 167 }
161 168
162 169 combineYRanges(new Range(series.getMinY(), series.getMaxY()), 0);
163 public void addSecondAxisSeries(XYSeries series) { 170 combineXRanges(new Range(series.getMinX(), series.getMaxX()), 0);
171 }
172 }
173
174
175 public void addSecondAxisSeries(XYSeries series, boolean visible) {
164 if (second == null) { 176 if (second == null) {
165 second = new XYSeriesCollection(); 177 second = new XYSeriesCollection();
166 } 178 }
167 179
168 if (series != null) { 180 if (series != null) {
169 second.addSeries(series); 181 if (visible) {
170 } 182 second.addSeries(series);
171 } 183 }
172 184
173 185 combineYRanges(new Range(series.getMinY(), series.getMaxY()), 1);
174 public void addAnnotations(FLYSAnnotation annotation) { 186 combineXRanges(new Range(series.getMinX(), series.getMaxX()), 0);
187 }
188 }
189
190
191 private void combineXRanges(Range range, int index) {
192 Integer key = new Integer(index);
193
194 if (xRanges == null) {
195 xRanges = new HashMap<Integer, Range>();
196 xRanges.put(key, range);
197 return;
198 }
199
200 Range newX = null;
201 Range oldX = xRanges.get(key);
202
203 if (oldX != null) {
204 newX = Range.combine(oldX, range);
205 }
206 else {
207 newX = range;
208 }
209
210 xRanges.put(key, newX);
211 }
212
213
214 private void combineYRanges(Range range, int index) {
215 Integer key = new Integer(index);
216
217 if (yRanges == null) {
218 yRanges = new HashMap<Integer, Range>();
219 yRanges.put(key, range);
220 return;
221 }
222
223 Range newY = null;
224 Range oldY = yRanges.get(key);
225
226 if (oldY != null) {
227 newY = Range.combine(oldY, range);
228 }
229 else {
230 newY = range;
231 }
232
233 yRanges.put(key, newY);
234 }
235
236
237 public void addAnnotations(FLYSAnnotation annotation, boolean visible) {
238 if (!visible) {
239 return;
240 }
241
175 if (annotations == null) { 242 if (annotations == null) {
176 annotations = new ArrayList<FLYSAnnotation>(); 243 annotations = new ArrayList<FLYSAnnotation>();
177 } 244 }
178 245
179 annotations.add(annotation); 246 annotations.add(annotation);
204 logger.debug("Zoom to specified ranges."); 271 logger.debug("Zoom to specified ranges.");
205 272
206 Range xrange = getDomainAxisRange(); 273 Range xrange = getDomainAxisRange();
207 Range yrange = getValueAxisRange(); 274 Range yrange = getValueAxisRange();
208 275
209 for (int i = 0, num = plot.getDatasetCount(); i < num; i++) { 276 logger.debug("XXX: CLIENT X RANGE = " + xrange);
210 XYDataset dataset = plot.getDataset(i); 277 logger.debug("XXX: CLIENT Y RANGE = " + yrange);
211 278
212 if (dataset == null) { 279 for (int i = 0, num = plot.getRangeAxisCount(); i < num; i++) {
213 continue; 280 Range[] ranges = new Range[] {
214 } 281 xRanges.get(0),
215 282 yRanges.get(new Integer(i)) };
216 Range[] ranges = getRangesForDataset(dataset); 283
217 284
218 if (i == 0) { 285 if (i == 0) {
219 ValueAxis xaxis = plot.getDomainAxis(); 286 ValueAxis xaxis = plot.getDomainAxis();
220 zoomX(plot, xaxis, ranges[0], xrange); 287 zoomX(plot, xaxis, ranges[0], xrange);
221 } 288 }
224 291
225 if (yaxis == null) { 292 if (yaxis == null) {
226 continue; 293 continue;
227 } 294 }
228 295
296 logger.debug("XXX Zoom y axis for index: " + i);
297 logger.debug("XXX Y MAX RANGE = " + ranges[1]);
229 zoomY(plot, yaxis, ranges[1], yrange); 298 zoomY(plot, yaxis, ranges[1], yrange);
230 } 299 }
231 } 300 }
232 301
233 302
272 return false; 341 return false;
273 } 342 }
274 343
275 344
276 /** 345 /**
277 * This method extracts the minimum and maximum values for x and y axes. 346 * This method extracts the minimum and maximum values for x and y axes
278 * 347 * which are stored in <i>xRanges</i> and <i>yRanges</i>.
279 * @param dataset The dataset that should be observed. 348 *
349 * @param index The index of the y-Axis.
280 * 350 *
281 * @return a Range[] as follows: [x-Range, y-Range]. 351 * @return a Range[] as follows: [x-Range, y-Range].
282 */ 352 */
283 public static Range[] getRangesForDataset(XYDataset dataset) { 353 public Range[] getRangesForDataset(int index) {
284 double[] xr = new double[] { Double.MAX_VALUE, -Double.MAX_VALUE }; 354 return new Range[] {
285 double[] yr = new double[] { Double.MAX_VALUE, -Double.MAX_VALUE }; 355 xRanges.get(new Integer(0)),
286 356 yRanges.get(new Integer(index))
287 if (dataset != null) { 357 };
288 int sCount = dataset.getSeriesCount();
289
290 for (int i = 0; i < sCount; i++) {
291 int iCount = dataset.getItemCount(i);
292
293 for (int j = 0; j < iCount; j++) {
294 double x = dataset.getX(i, j).doubleValue();
295 double y = dataset.getY(i, j).doubleValue();
296
297 if (!Double.isNaN(x)) {
298 xr[0] = xr[0] < x ? xr[0] : x;
299 xr[1] = xr[1] > x ? xr[1] : x;
300 }
301
302 if (!Double.isNaN(y)) {
303 yr[0] = yr[0] < y ? yr[0] : y;
304 yr[1] = yr[1] > y ? yr[1] : y;
305 }
306 }
307 }
308 }
309
310 // this is only required, if there are no items in the dataset.
311 xr[0] = xr[0] < xr[1] ? xr[0] : xr[1];
312 xr[1] = xr[1] > xr[0] ? xr[1] : xr[0];
313 yr[0] = yr[0] < yr[1] ? yr[0] : yr[1];
314 yr[1] = yr[1] > yr[0] ? yr[1] : yr[0];
315
316 return new Range[] {new Range(xr[0], xr[1]), new Range(yr[0], yr[1])};
317 } 358 }
318 359
319 360
320 protected void addAnnotations(XYPlot plot) { 361 protected void addAnnotations(XYPlot plot) {
321 plot.clearAnnotations(); 362 plot.clearAnnotations();

http://dive4elements.wald.intevation.org