comparison flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.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 4a8251eae217
children bdb05dc9b763
comparison
equal deleted inserted replaced
1678:03fbf1b30e72 1679:69929c471646
6 6
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;
12 import java.util.List;
13
14 import org.w3c.dom.Document;
15
11 import org.apache.log4j.Logger; 16 import org.apache.log4j.Logger;
12 17
13 import org.jfree.chart.ChartFactory; 18 import org.jfree.chart.ChartFactory;
14 import org.jfree.chart.JFreeChart; 19 import org.jfree.chart.JFreeChart;
20 import org.jfree.chart.LegendItem;
21 import org.jfree.chart.LegendItemCollection;
22 import org.jfree.chart.annotations.XYTextAnnotation;
15 import org.jfree.chart.axis.NumberAxis; 23 import org.jfree.chart.axis.NumberAxis;
16 import org.jfree.chart.axis.ValueAxis; 24 import org.jfree.chart.axis.ValueAxis;
17 import org.jfree.chart.plot.PlotOrientation; 25 import org.jfree.chart.plot.PlotOrientation;
18 import org.jfree.chart.plot.XYPlot; 26 import org.jfree.chart.plot.XYPlot;
27 import org.jfree.chart.renderer.xy.XYItemRenderer;
19 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; 28 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
20 import org.jfree.data.Range; 29 import org.jfree.data.Range;
21 import org.jfree.data.xy.XYDataset; 30 import org.jfree.data.xy.XYDataset;
22 import org.jfree.data.xy.XYSeries; 31 import org.jfree.data.xy.XYSeries;
23 import org.jfree.data.xy.XYSeriesCollection; 32 import org.jfree.data.xy.XYSeriesCollection;
24 33
25 import org.jfree.ui.RectangleInsets; 34 import org.jfree.ui.RectangleInsets;
26 35
27 import de.intevation.flys.exports.ChartExportHelper; 36 import de.intevation.flys.exports.ChartExportHelper;
37 import de.intevation.flys.jfree.FLYSAnnotation;
38 import de.intevation.flys.utils.ThemeUtil;
28 39
29 40
30 /** 41 /**
31 * An abstract base class for creating XY charts. 42 * An abstract base class for creating XY charts.
32 * 43 *
41 /** SeriesCollection used for the first axis. */ 52 /** SeriesCollection used for the first axis. */
42 protected XYSeriesCollection first; 53 protected XYSeriesCollection first;
43 54
44 /** SeriesCollection used for the second axis. */ 55 /** SeriesCollection used for the second axis. */
45 protected XYSeriesCollection second; 56 protected XYSeriesCollection second;
57
58 /** List of annotations to insert in plot. */
59 protected List<FLYSAnnotation> annotations;
60
46 61
47 public static final Color DEFAULT_GRID_COLOR = Color.GRAY; 62 public static final Color DEFAULT_GRID_COLOR = Color.GRAY;
48 public static final float DEFAULT_GRID_LINE_WIDTH = 0.3f; 63 public static final float DEFAULT_GRID_LINE_WIDTH = 0.3f;
49 64
50 65
104 chart.getPlot().setBackgroundPaint(Color.WHITE); 119 chart.getPlot().setBackgroundPaint(Color.WHITE);
105 120
106 XYPlot plot = (XYPlot) chart.getPlot(); 121 XYPlot plot = (XYPlot) chart.getPlot();
107 122
108 addDatasets(plot); 123 addDatasets(plot);
124 addAnnotations(plot);
109 addSubtitles(chart); 125 addSubtitles(chart);
110 adjustPlot(plot); 126 adjustPlot(plot);
111 adjustAxes(plot); 127 adjustAxes(plot);
112 localizeAxes(plot); 128 localizeAxes(plot);
113 129
150 } 166 }
151 167
152 if (series != null) { 168 if (series != null) {
153 second.addSeries(series); 169 second.addSeries(series);
154 } 170 }
171 }
172
173
174 public void addAnnotations(FLYSAnnotation annotation) {
175 if (annotations == null) {
176 annotations = new ArrayList<FLYSAnnotation>();
177 }
178
179 annotations.add(annotation);
155 } 180 }
156 181
157 182
158 private void removeEmptyRangeAxes(XYPlot plot) { 183 private void removeEmptyRangeAxes(XYPlot plot) {
159 if (first == null) { 184 if (first == null) {
290 315
291 return new Range[] {new Range(xr[0], xr[1]), new Range(yr[0], yr[1])}; 316 return new Range[] {new Range(xr[0], xr[1]), new Range(yr[0], yr[1])};
292 } 317 }
293 318
294 319
320 protected void addAnnotations(XYPlot plot) {
321 plot.clearAnnotations();
322
323 if (annotations == null) {
324 logger.debug("No Annotations given.");
325 return;
326 }
327
328 LegendItemCollection lic = new LegendItemCollection();
329
330 int idx = 0;
331 if (plot.getRangeAxis(idx) == null && plot.getRangeAxisCount() >= 2) {
332 idx = 1;
333 }
334
335 XYItemRenderer renderer = plot.getRenderer(idx);
336
337 for (FLYSAnnotation fa: annotations) {
338 Document theme = fa.getTheme();
339
340 Color color = theme != null
341 ? ThemeUtil.parseLineColorField(theme)
342 : null;
343
344 if (color == null) {
345 color = Color.black;
346 }
347
348 lic.add(new LegendItem(fa.getLabel(), color));
349
350 for (XYTextAnnotation ta: fa.getAnnotations()) {
351 ta.setPaint(color);
352 renderer.addAnnotation(ta);
353 }
354
355 plot.setFixedLegendItems(lic);
356 }
357 }
358
359
295 /** 360 /**
296 * Adjusts the axes of a plot. 361 * Adjusts the axes of a plot.
297 * 362 *
298 * @param plot The XYPlot of the chart. 363 * @param plot The XYPlot of the chart.
299 */ 364 */
404 } 469 }
405 } 470 }
406 471
407 472
408 protected void applyThemes(XYPlot plot, XYSeriesCollection dataset, int i) { 473 protected void applyThemes(XYPlot plot, XYSeriesCollection dataset, int i) {
474 LegendItemCollection lic = new LegendItemCollection();
475 LegendItemCollection anno = plot.getFixedLegendItems();
476
409 XYLineAndShapeRenderer r = getRenderer(plot, i); 477 XYLineAndShapeRenderer r = getRenderer(plot, i);
410 478
411 for (int s = 0, num = dataset.getSeriesCount(); s < num; s++) { 479 for (int s = 0, num = dataset.getSeriesCount(); s < num; s++) {
412 XYSeries series = dataset.getSeries(s); 480 XYSeries series = dataset.getSeries(s);
413 481
414 if (series instanceof StyledXYSeries) { 482 if (series instanceof StyledXYSeries) {
415 ((StyledXYSeries) series).applyTheme(r, s); 483 ((StyledXYSeries) series).applyTheme(r, s);
416 } 484 }
417 } 485
486 lic.add(r.getLegendItem(i, s));
487 }
488
489 if (anno != null) {
490 lic.addAll(anno);
491 }
492
493 plot.setFixedLegendItems(lic);
418 494
419 plot.setRenderer(i, r); 495 plot.setRenderer(i, r);
420 } 496 }
421 497
422 498

http://dive4elements.wald.intevation.org