Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java @ 2234:46ec09c7f578
Refactoring: moved more base code from XYChartGenerator into its parent class ChartGenerator.
flys-artifacts/trunk@3878 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 02 Feb 2012 12:50:33 +0000 |
parents | 958a10e2e246 |
children | c2b15d9c0f43 |
comparison
equal
deleted
inserted
replaced
2233:958a10e2e246 | 2234:46ec09c7f578 |
---|---|
1 package de.intevation.flys.exports; | 1 package de.intevation.flys.exports; |
2 | 2 |
3 import java.awt.Color; | |
3 import java.io.IOException; | 4 import java.io.IOException; |
4 import java.io.OutputStream; | 5 import java.io.OutputStream; |
5 import java.util.Locale; | 6 import java.util.Locale; |
6 | 7 |
7 import javax.xml.xpath.XPathConstants; | 8 import javax.xml.xpath.XPathConstants; |
41 */ | 42 */ |
42 public abstract class ChartGenerator implements OutGenerator { | 43 public abstract class ChartGenerator implements OutGenerator { |
43 | 44 |
44 private static Logger logger = Logger.getLogger(ChartGenerator.class); | 45 private static Logger logger = Logger.getLogger(ChartGenerator.class); |
45 | 46 |
46 /** The default chart width, if no other width is set. */ | 47 public static final int DEFAULT_CHART_WIDTH = 600; |
47 public static final int DEFAULT_CHART_WIDTH = 600; | 48 public static final int DEFAULT_CHART_HEIGHT = 400; |
48 | 49 public static final String DEFAULT_CHART_FORMAT = "png"; |
49 /** The default chart height, if no other height is set.*/ | 50 public static final Color DEFAULT_GRID_COLOR = Color.GRAY; |
50 public static final int DEFAULT_CHART_HEIGHT = 400; | 51 public static final float DEFAULT_GRID_LINE_WIDTH = 0.3f; |
51 | 52 public static final int DEFAULT_FONT_SIZE = 12; |
52 /** The default chart format, if no other height is set.*/ | 53 public static final String DEFAULT_FONT_NAME = "Tahoma"; |
53 public static final String DEFAULT_CHART_FORMAT = "png"; | 54 |
54 | 55 |
55 /** The XPath that points to the chart size of the incoming request | |
56 * document.*/ | |
57 public static final String XPATH_CHART_SIZE = | 56 public static final String XPATH_CHART_SIZE = |
58 "/art:action/art:attributes/art:size"; | 57 "/art:action/art:attributes/art:size"; |
59 | 58 |
60 public static final String XPATH_CHART_FORMAT = | 59 public static final String XPATH_CHART_FORMAT = |
61 "/art:action/art:attributes/art:format/@art:value"; | 60 "/art:action/art:attributes/art:format/@art:value"; |
91 int length(); | 90 int length(); |
92 String getId(int idx); | 91 String getId(int idx); |
93 } | 92 } |
94 | 93 |
95 | 94 |
95 /** | |
96 * This method needs to be implemented by concrete subclasses to create new | |
97 * instances of JFreeChart. | |
98 * | |
99 * @return a new instance of a JFreeChart. | |
100 */ | |
101 public abstract JFreeChart generateChart(); | |
102 | |
103 | |
104 public abstract void doOut( | |
105 ArtifactAndFacet bundle, | |
106 Document attr, | |
107 boolean visible); | |
108 | |
109 | |
96 protected abstract YAxisWalker getYAxisWalker(); | 110 protected abstract YAxisWalker getYAxisWalker(); |
97 | 111 |
112 | |
113 /** | |
114 * Returns the default title of a chart. | |
115 * | |
116 * @return the default title of a chart. | |
117 */ | |
98 protected abstract String getDefaultChartTitle(); | 118 protected abstract String getDefaultChartTitle(); |
99 | 119 |
120 | |
121 /** | |
122 * Returns the default X-Axis label of a chart. | |
123 * | |
124 * @return the default X-Axis label of a chart. | |
125 */ | |
100 protected abstract String getDefaultXAxisLabel(); | 126 protected abstract String getDefaultXAxisLabel(); |
101 | 127 |
128 | |
129 /** | |
130 * This method is called to retrieve the default label for an Y axis at | |
131 * position <i>pos</i>. | |
132 * | |
133 * @param pos The position of an Y axis. | |
134 * | |
135 * @return the default Y axis label at position <i>pos</i>. | |
136 */ | |
102 protected abstract String getDefaultYAxisLabel(int pos); | 137 protected abstract String getDefaultYAxisLabel(int pos); |
103 | 138 |
104 protected abstract void addSubtitles(JFreeChart chart); | 139 |
105 | 140 /** |
106 | 141 * This method should be used by concrete subclasses to add subtitle to |
142 * <i>chart</i>. <b>The method in this implementation is empty</b>. | |
143 * | |
144 * @param chart The JFreeChart chart object. | |
145 */ | |
146 protected void addSubtitles(JFreeChart chart) { | |
147 // do nothing | |
148 } | |
149 | |
150 | |
151 /** | |
152 * Generate chart. | |
153 */ | |
154 @Override | |
155 public void generate() | |
156 throws IOException | |
157 { | |
158 logger.debug("ChartGenerator.generate"); | |
159 | |
160 JFreeChart chart = generateChart(); | |
161 | |
162 String format = getFormat(); | |
163 int[] size = getSize(); | |
164 | |
165 if (size == null) { | |
166 size = getExportDimension(); | |
167 } | |
168 | |
169 context.putContextValue("chart.width", size[0]); | |
170 context.putContextValue("chart.height", size[1]); | |
171 | |
172 if (format.equals(ChartExportHelper.FORMAT_PNG)) { | |
173 context.putContextValue("chart.image.format", "png"); | |
174 | |
175 ChartExportHelper.exportImage( | |
176 out, | |
177 chart, | |
178 context); | |
179 } | |
180 else if (format.equals(ChartExportHelper.FORMAT_PDF)) { | |
181 preparePDFContext(context); | |
182 | |
183 ChartExportHelper.exportPDF( | |
184 out, | |
185 chart, | |
186 context); | |
187 } | |
188 else if (format.equals(ChartExportHelper.FORMAT_SVG)) { | |
189 prepareSVGContext(context); | |
190 | |
191 ChartExportHelper.exportSVG( | |
192 out, | |
193 chart, | |
194 context); | |
195 } | |
196 else if (format.equals(ChartExportHelper.FORMAT_CSV)) { | |
197 context.putContextValue("chart.image.format", "csv"); | |
198 | |
199 ChartExportHelper.exportCSV( | |
200 out, | |
201 chart, | |
202 context); | |
203 } | |
204 } | |
205 | |
206 | |
207 @Override | |
107 public void init(Document request, OutputStream out, CallContext context) { | 208 public void init(Document request, OutputStream out, CallContext context) { |
108 logger.debug("ChartGenerator.init"); | 209 logger.debug("ChartGenerator.init"); |
109 | 210 |
110 this.request = request; | 211 this.request = request; |
111 this.out = out; | 212 this.out = out; |
112 this.context = context; | 213 this.context = context; |
113 } | 214 } |
114 | 215 |
115 | 216 |
217 @Override | |
116 public void setMasterArtifact(Artifact master) { | 218 public void setMasterArtifact(Artifact master) { |
117 this.master = master; | 219 this.master = master; |
118 } | 220 } |
119 | 221 |
120 | 222 |
205 * @throws NullPointerException if <i>settings</i> is null. | 307 * @throws NullPointerException if <i>settings</i> is null. |
206 */ | 308 */ |
207 public Integer getLegendFontSize(ChartSettings settings) { | 309 public Integer getLegendFontSize(ChartSettings settings) { |
208 LegendSection ls = settings.getLegendSection(); | 310 LegendSection ls = settings.getLegendSection(); |
209 return ls != null ? ls.getFontSize() : null; | 311 return ls != null ? ls.getFontSize() : null; |
312 } | |
313 | |
314 | |
315 /** | |
316 * Returns the title of a chart. The return value depends on the existence | |
317 * of ChartSettings: if there are ChartSettings set, this method returns the | |
318 * chart title provided by those settings. Otherwise, this method returns | |
319 * getDefaultChartTitle(). | |
320 * | |
321 * @return the title of a chart. | |
322 */ | |
323 protected String getChartTitle() { | |
324 ChartSettings chartSettings = getChartSettings(); | |
325 | |
326 if (chartSettings != null) { | |
327 return getChartTitle(chartSettings); | |
328 } | |
329 | |
330 return getDefaultChartTitle(); | |
331 } | |
332 | |
333 | |
334 /** | |
335 * Returns the subtitle of a chart. The return value depends on the | |
336 * existence of ChartSettings: if there are ChartSettings set, this method | |
337 * returns the chart title provided by those settings. Otherwise, this | |
338 * method returns getDefaultChartSubtitle(). | |
339 * | |
340 * @return the subtitle of a chart. | |
341 */ | |
342 protected String getChartSubtitle() { | |
343 ChartSettings chartSettings = getChartSettings(); | |
344 | |
345 if (chartSettings != null) { | |
346 return getChartSubtitle(chartSettings); | |
347 } | |
348 | |
349 return getDefaultChartSubtitle(); | |
350 } | |
351 | |
352 | |
353 /** | |
354 * This method always returns null. Override it in subclasses that require | |
355 * subtitles. | |
356 * | |
357 * @return null. | |
358 */ | |
359 protected String getDefaultChartSubtitle() { | |
360 // Override this method in subclasses | |
361 return null; | |
362 } | |
363 | |
364 | |
365 /** | |
366 * This method is used to determine, if the chart's legend is visible or | |
367 * not. If a <i>settings</i> instance is set, this instance determines the | |
368 * visibility otherwise, this method returns true as default if no | |
369 * <i>settings</i> is set. | |
370 * | |
371 * @return true, if the legend should be visible, otherwise false. | |
372 */ | |
373 protected boolean isLegendVisible() { | |
374 ChartSettings chartSettings = getChartSettings(); | |
375 if (chartSettings != null) { | |
376 return isLegendVisible(chartSettings); | |
377 } | |
378 | |
379 return true; | |
380 } | |
381 | |
382 | |
383 /** | |
384 * This method is used to determine the font size of the chart's legend. If | |
385 * a <i>settings</i> instance is set, this instance determines the font | |
386 * size, otherwise this method returns 12 as default if no <i>settings</i> | |
387 * is set or if it doesn't provide a legend font size. | |
388 * | |
389 * @return a legend font size. | |
390 */ | |
391 protected int getLegendFontSize() { | |
392 Integer fontSize = null; | |
393 | |
394 ChartSettings chartSettings = getChartSettings(); | |
395 if (chartSettings != null) { | |
396 fontSize = getLegendFontSize(chartSettings); | |
397 } | |
398 | |
399 return fontSize != null ? fontSize : DEFAULT_FONT_SIZE; | |
400 } | |
401 | |
402 | |
403 /** | |
404 * This method is used to determine if the resulting chart should display | |
405 * grid lines or not. <b>Note: this method always returns true!</b> | |
406 * | |
407 * @return true, if the chart should display grid lines, otherwise false. | |
408 */ | |
409 protected boolean isGridVisible() { | |
410 return true; | |
411 } | |
412 | |
413 | |
414 /** | |
415 * Returns the X-Axis label of a chart. | |
416 * | |
417 * @return the X-Axis label of a chart. | |
418 */ | |
419 protected String getXAxisLabel() { | |
420 ChartSettings chartSettings = getChartSettings(); | |
421 if (chartSettings == null) { | |
422 return getDefaultXAxisLabel(); | |
423 } | |
424 | |
425 AxisSection as = chartSettings.getAxisSection("X"); | |
426 if (as != null) { | |
427 String label = as.getLabel(); | |
428 | |
429 if (label != null) { | |
430 return label; | |
431 } | |
432 } | |
433 | |
434 return getDefaultXAxisLabel(); | |
435 } | |
436 | |
437 | |
438 /** | |
439 * This method returns the font size for the X axis. If the font size is | |
440 * specified in ChartSettings (if <i>chartSettings</i> is set), this size is | |
441 * returned. Otherwise the default font size 12 is returned. | |
442 * | |
443 * @return the font size for the x axis. | |
444 */ | |
445 protected int getXAxisLabelFontSize() { | |
446 ChartSettings chartSettings = getChartSettings(); | |
447 if (chartSettings == null) { | |
448 return DEFAULT_FONT_SIZE; | |
449 } | |
450 | |
451 AxisSection as = chartSettings.getAxisSection("X"); | |
452 Integer fontSize = as.getFontSize(); | |
453 | |
454 return fontSize != null ? fontSize : DEFAULT_FONT_SIZE; | |
455 } | |
456 | |
457 | |
458 /** | |
459 * This method returns the font size for an Y axis. If the font size is | |
460 * specified in ChartSettings (if <i>chartSettings</i> is set), this size is | |
461 * returned. Otherwise the default font size 12 is returned. | |
462 * | |
463 * @return the font size for the x axis. | |
464 */ | |
465 protected int getYAxisFontSize(int pos) { | |
466 ChartSettings chartSettings = getChartSettings(); | |
467 if (chartSettings == null) { | |
468 return DEFAULT_FONT_SIZE; | |
469 } | |
470 | |
471 YAxisWalker walker = getYAxisWalker(); | |
472 | |
473 AxisSection as = chartSettings.getAxisSection(walker.getId(pos)); | |
474 Integer fontSize = as.getFontSize(); | |
475 | |
476 return fontSize != null ? fontSize : DEFAULT_FONT_SIZE; | |
477 } | |
478 | |
479 | |
480 /** | |
481 * This method returns the export dimension specified in ChartSettings as | |
482 * int array [width,height]. | |
483 * | |
484 * @return an int array with [width,height]. | |
485 */ | |
486 protected int[] getExportDimension() { | |
487 ChartSettings chartSettings = getChartSettings(); | |
488 if (chartSettings == null) { | |
489 return new int[] { 600, 400 }; | |
490 } | |
491 | |
492 ExportSection export = chartSettings.getExportSection(); | |
493 Integer width = export.getWidth(); | |
494 Integer height = export.getHeight(); | |
495 | |
496 if (width != null && height != null) { | |
497 return new int[] { width, height }; | |
498 } | |
499 | |
500 return new int[] { 600, 400 }; | |
210 } | 501 } |
211 | 502 |
212 | 503 |
213 /** | 504 /** |
214 * Returns the Y-Axis label of a chart at position <i>pos</i>. | 505 * Returns the Y-Axis label of a chart at position <i>pos</i>. |
233 | 524 |
234 return getDefaultYAxisLabel(pos); | 525 return getDefaultYAxisLabel(pos); |
235 } | 526 } |
236 | 527 |
237 | 528 |
529 /** | |
530 * This helper mehtod is used to extract the current locale from instance | |
531 * vairable <i>context</i>. | |
532 * | |
533 * @return the current locale. | |
534 */ | |
238 protected Locale getLocale() { | 535 protected Locale getLocale() { |
239 CallMeta meta = context.getMeta(); | 536 CallMeta meta = context.getMeta(); |
240 PreferredLocale[] prefs = meta.getLanguages(); | 537 PreferredLocale[] prefs = meta.getLanguages(); |
241 | 538 |
242 int len = prefs != null ? prefs.length : 0; | 539 int len = prefs != null ? prefs.length : 0; |
311 | 608 |
312 return size[0] > 0 && size[1] > 0 ? size : null; | 609 return size[0] > 0 && size[1] > 0 ? size : null; |
313 } | 610 } |
314 | 611 |
315 | 612 |
613 /** | |
614 * This method returns the format specified in the <i>request</i> document | |
615 * or <i>DEFAULT_CHART_FORMAT</i> if no format is specified in | |
616 * <i>request</i>. | |
617 * | |
618 * @return the format used to export this chart. | |
619 */ | |
316 protected String getFormat() { | 620 protected String getFormat() { |
317 String format = (String) XMLUtils.xpath( | 621 String format = (String) XMLUtils.xpath( |
318 request, | 622 request, |
319 XPATH_CHART_FORMAT, | 623 XPATH_CHART_FORMAT, |
320 XPathConstants.STRING, | 624 XPathConstants.STRING, |
421 protected int[] getDefaultSize() { | 725 protected int[] getDefaultSize() { |
422 return new int[] { DEFAULT_CHART_WIDTH, DEFAULT_CHART_HEIGHT }; | 726 return new int[] { DEFAULT_CHART_WIDTH, DEFAULT_CHART_HEIGHT }; |
423 } | 727 } |
424 | 728 |
425 | 729 |
426 public abstract void doOut( | |
427 ArtifactAndFacet bundle, | |
428 Document attr, | |
429 boolean visible); | |
430 | |
431 public abstract void generate() throws IOException; | |
432 | |
433 | |
434 /** | 730 /** |
435 * Returns an instance of <i>EmptySettings</i> currently! | 731 * Returns an instance of <i>EmptySettings</i> currently! |
436 * | 732 * |
437 * @return an instance of <i>EmptySettings</i>. | 733 * @return an instance of <i>EmptySettings</i>. |
438 */ | 734 */ |
467 protected NumberAxis createNumberAxis(int idx, String label) { | 763 protected NumberAxis createNumberAxis(int idx, String label) { |
468 YAxisWalker walker = getYAxisWalker(); | 764 YAxisWalker walker = getYAxisWalker(); |
469 | 765 |
470 return new IdentifiableNumberAxis(walker.getId(idx), label); | 766 return new IdentifiableNumberAxis(walker.getId(idx), label); |
471 } | 767 } |
768 | |
769 | |
770 protected void preparePDFContext(CallContext context) { | |
771 int[] dimension = getExportDimension(); | |
772 | |
773 context.putContextValue("chart.width", dimension[0]); | |
774 context.putContextValue("chart.height", dimension[1]); | |
775 context.putContextValue("chart.marginLeft", 5f); | |
776 context.putContextValue("chart.marginRight", 5f); | |
777 context.putContextValue("chart.marginTop", 5f); | |
778 context.putContextValue("chart.marginBottom", 5f); | |
779 context.putContextValue( | |
780 "chart.page.format", | |
781 ChartExportHelper.DEFAULT_PAGE_SIZE); | |
782 } | |
783 | |
784 | |
785 protected void prepareSVGContext(CallContext context) { | |
786 int[] dimension = getExportDimension(); | |
787 | |
788 context.putContextValue("chart.width", dimension[0]); | |
789 context.putContextValue("chart.height", dimension[1]); | |
790 context.putContextValue( | |
791 "chart.encoding", | |
792 ChartExportHelper.DEFAULT_ENCODING); | |
793 } | |
472 } | 794 } |
473 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 795 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |