comparison flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java @ 2236:c2b15d9c0f43

Refactoring: moved more base code from XYChartGenerator into its parent class ChartGenerator flys-artifacts/trunk@3883 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 02 Feb 2012 14:00:40 +0000
parents 46ec09c7f578
children 23c7c51df772
comparison
equal deleted inserted replaced
2235:ee5310134463 2236:c2b15d9c0f43
1 package de.intevation.flys.exports; 1 package de.intevation.flys.exports;
2 2
3 import java.awt.Color; 3 import java.awt.Color;
4 import java.awt.Font;
5
4 import java.io.IOException; 6 import java.io.IOException;
5 import java.io.OutputStream; 7 import java.io.OutputStream;
8
9 import java.util.ArrayList;
10 import java.util.List;
6 import java.util.Locale; 11 import java.util.Locale;
7 12
8 import javax.xml.xpath.XPathConstants; 13 import javax.xml.xpath.XPathConstants;
9 14
10 import org.apache.log4j.Logger; 15 import org.apache.log4j.Logger;
11 16
12 import org.w3c.dom.Document; 17 import org.w3c.dom.Document;
13 import org.w3c.dom.Element; 18 import org.w3c.dom.Element;
14 19
15 import org.jfree.chart.JFreeChart; 20 import org.jfree.chart.JFreeChart;
21 import org.jfree.chart.LegendItem;
16 import org.jfree.chart.axis.NumberAxis; 22 import org.jfree.chart.axis.NumberAxis;
17 import org.jfree.data.Range; 23 import org.jfree.data.Range;
18 24
19 import de.intevation.artifacts.Artifact; 25 import de.intevation.artifacts.Artifact;
20 import de.intevation.artifacts.CallContext; 26 import de.intevation.artifacts.CallContext;
30 import de.intevation.flys.model.River; 36 import de.intevation.flys.model.River;
31 37
32 import de.intevation.flys.artifacts.FLYSArtifact; 38 import de.intevation.flys.artifacts.FLYSArtifact;
33 import de.intevation.flys.artifacts.resources.Resources; 39 import de.intevation.flys.artifacts.resources.Resources;
34 import de.intevation.flys.utils.FLYSUtils; 40 import de.intevation.flys.utils.FLYSUtils;
41 import de.intevation.flys.utils.ThemeAccess;
35 42
36 43
37 /** 44 /**
38 * The base class for chart creation. It should provide some basic things that 45 * The base class for chart creation. It should provide some basic things that
39 * equal in all chart types. 46 * equal in all chart types.
225 this.settings = settings; 232 this.settings = settings;
226 } 233 }
227 234
228 235
229 /** 236 /**
237 * Returns an instance of <i>ChartSettings</i> with a chart specific section
238 * but with no axes settings.
239 *
240 * @return an instance of <i>ChartSettings</i>.
241 */
242 @Override
243 public Settings getSettings() {
244 if (this.settings != null) {
245 return this.settings;
246 }
247
248 ChartSettings settings = new ChartSettings();
249
250 ChartSection chartSection = buildChartSection();
251 LegendSection legendSection = buildLegendSection();
252 ExportSection exportSection = buildExportSection();
253
254 settings.setChartSection(chartSection);
255 settings.setLegendSection(legendSection);
256 settings.setExportSection(exportSection);
257
258 List<AxisSection> axisSections = buildAxisSections();
259 for (AxisSection axisSection: axisSections) {
260 settings.addAxisSection(axisSection);
261 }
262
263 return settings;
264 }
265
266
267 /**
268 * Creates a new <i>ChartSection</i>.
269 *
270 * @return a new <i>ChartSection</i>.
271 */
272 protected ChartSection buildChartSection() {
273 ChartSection chartSection = new ChartSection();
274 chartSection.setTitle(getChartTitle());
275 chartSection.setSubtitle(getChartSubtitle());
276 chartSection.setDisplayGird(isGridVisible());
277 return chartSection;
278 }
279
280
281 /**
282 * Creates a new <i>LegendSection</i>.
283 *
284 * @return a new <i>LegendSection</i>.
285 */
286 protected LegendSection buildLegendSection() {
287 LegendSection legendSection = new LegendSection();
288 legendSection.setVisibility(isLegendVisible());
289 legendSection.setFontSize(getLegendFontSize());
290 return legendSection;
291 }
292
293
294 /**
295 * Creates a new <i>ExportSection</i> with default values <b>WIDTH=600</b>
296 * and <b>HEIGHT=400</b>.
297 *
298 * @return a new <i>ExportSection</i>.
299 */
300 protected ExportSection buildExportSection() {
301 ExportSection exportSection = new ExportSection();
302 exportSection.setWidth(600);
303 exportSection.setHeight(400);
304 return exportSection;
305 }
306
307
308 /**
309 * Creates a list of Sections that contains all axes of the chart (including
310 * X and Y axes).
311 *
312 * @return a list of Sections for each axis in this chart.
313 */
314 protected List<AxisSection> buildAxisSections() {
315 List<AxisSection> axisSections = new ArrayList<AxisSection>();
316
317 axisSections.addAll(buildXAxisSections());
318 axisSections.addAll(buildYAxisSections());
319
320 return axisSections;
321 }
322
323
324 /**
325 * Creates a new Section for chart's X axis.
326 *
327 * @return a List that contains a Section for the X axis.
328 */
329 protected List<AxisSection> buildXAxisSections() {
330 List<AxisSection> axisSections = new ArrayList<AxisSection>();
331
332 String identifier = "X";
333
334 AxisSection axisSection = new AxisSection();
335 axisSection.setIdentifier(identifier);
336 axisSection.setLabel(getXAxisLabel());
337 axisSection.setFontSize(14);
338 axisSection.setFixed(false);
339
340 // XXX We are able to find better default ranges that [0,0], but the Y
341 // axes currently have no better ranges set.
342 axisSection.setUpperRange(0d);
343 axisSection.setLowerRange(0d);
344
345 axisSections.add(axisSection);
346
347 return axisSections;
348 }
349
350
351 /**
352 * Creates a list of Section for the chart's Y axes. This method makes use
353 * of <i>getYAxisWalker</i> to be able to access all Y axes defined in
354 * subclasses.
355 *
356 * @return a list of Y axis sections.
357 */
358 protected List<AxisSection> buildYAxisSections() {
359 List<AxisSection> axisSections = new ArrayList<AxisSection>();
360
361 YAxisWalker walker = getYAxisWalker();
362 for (int i = 0, n = walker.length(); i < n; i++) {
363 AxisSection ySection = new AxisSection();
364 ySection.setIdentifier(walker.getId(i));
365 ySection.setLabel(getYAxisLabel(i));
366 ySection.setFontSize(14);
367 ySection.setFixed(false);
368
369 // XXX We are able to find better default ranges that [0,0], the
370 // only problem is, that we do NOT have a better range than [0,0]
371 // for each axis, because the initial chart will not have a dataset
372 // for each axis set!
373 ySection.setUpperRange(0d);
374 ySection.setLowerRange(0d);
375
376 axisSections.add(ySection);
377 }
378
379 return axisSections;
380 }
381
382
383 /**
384 * Returns the <i>settings</i> as <i>ChartSettings</i>.
385 *
386 * @return the <i>settings</i> as <i>ChartSettings</i> or null, if
387 * <i>settings</i> is not an instance of <i>ChartSettings</i>.
388 */
389 public ChartSettings getChartSettings() {
390 if (settings instanceof ChartSettings) {
391 return (ChartSettings) settings;
392 }
393
394 return null;
395 }
396
397
398 /**
230 * Returns the chart title provided by <i>settings</i>. 399 * Returns the chart title provided by <i>settings</i>.
231 * 400 *
232 * @param settings A ChartSettings object. 401 * @param settings A ChartSettings object.
233 * 402 *
234 * @return the title provided by <i>settings</i> or null if no 403 * @return the title provided by <i>settings</i> or null if no
521 return label; 690 return label;
522 } 691 }
523 } 692 }
524 693
525 return getDefaultYAxisLabel(pos); 694 return getDefaultYAxisLabel(pos);
695 }
696
697
698 /**
699 * This method searches for a specific axis in the <i>settings</i> if
700 * <i>settings</i> is set. If the axis was found, this method returns the
701 * specified axis range if the axis range is fixed. Otherwise, this method
702 * returns null.
703 *
704 * @param axisId The identifier of an axis.
705 *
706 * @return the specified axis range from <i>settings</i> if the axis is
707 * fixed, otherwise null.
708 */
709 public Range getRangeForAxisFromSettings(String axisId) {
710 ChartSettings chartSettings = getChartSettings();
711 if (chartSettings == null) {
712 return null;
713 }
714
715 AxisSection as = chartSettings.getAxisSection(axisId);
716 Boolean fixed = as.isFixed();
717
718 if (fixed != null && fixed) {
719 Double upper = as.getUpperRange();
720 Double lower = as.getLowerRange();
721
722 if (upper != null && lower != null) {
723 return lower < upper
724 ? new Range(lower, upper)
725 : new Range(upper, lower);
726 }
727 }
728
729 return null;
526 } 730 }
527 731
528 732
529 /** 733 /**
530 * This helper mehtod is used to extract the current locale from instance 734 * This helper mehtod is used to extract the current locale from instance
726 return new int[] { DEFAULT_CHART_WIDTH, DEFAULT_CHART_HEIGHT }; 930 return new int[] { DEFAULT_CHART_WIDTH, DEFAULT_CHART_HEIGHT };
727 } 931 }
728 932
729 933
730 /** 934 /**
731 * Returns an instance of <i>EmptySettings</i> currently!
732 *
733 * @return an instance of <i>EmptySettings</i>.
734 */
735 public Settings getSettings() {
736 return settings != null ? settings : new EmptySettings();
737 }
738
739
740 /**
741 * Returns the <i>settings</i> as <i>ChartSettings</i>.
742 *
743 * @return the <i>settings</i> as <i>ChartSettings</i> or null, if
744 * <i>settings</i> is not an instance of <i>ChartSettings</i>.
745 */
746 public ChartSettings getChartSettings() {
747 if (settings instanceof ChartSettings) {
748 return (ChartSettings) settings;
749 }
750
751 return null;
752 }
753
754
755 /**
756 * Creates a new instance of <i>IdentifiableNumberAxis</i>. 935 * Creates a new instance of <i>IdentifiableNumberAxis</i>.
757 * 936 *
758 * @param idx The index of the new axis. 937 * @param idx The index of the new axis.
759 * @param label The label of the new axis. 938 * @param label The label of the new axis.
760 * 939 *
762 */ 941 */
763 protected NumberAxis createNumberAxis(int idx, String label) { 942 protected NumberAxis createNumberAxis(int idx, String label) {
764 YAxisWalker walker = getYAxisWalker(); 943 YAxisWalker walker = getYAxisWalker();
765 944
766 return new IdentifiableNumberAxis(walker.getId(idx), label); 945 return new IdentifiableNumberAxis(walker.getId(idx), label);
946 }
947
948
949 /**
950 * Creates a new LegendItem with <i>name</i> and font provided by
951 * <i>createLegendLabelFont()</i>.
952 *
953 * @param theme The theme of the chart line.
954 * @param The displayed name of the item.
955 *
956 * @return a new LegendItem instance.
957 */
958 public LegendItem createLegendItem(Document theme, String name) {
959 // OPTIMIZE Pass font, parsed Theme items.
960 ThemeAccess themeAccess = new ThemeAccess(theme);
961
962 Color color = themeAccess.parseLineColorField();
963 LegendItem legendItem = new LegendItem(name, color);
964
965 legendItem.setLabelFont(createLegendLabelFont());
966 return legendItem;
967 }
968
969
970 /**
971 * Creates Font (Family and size) to use when creating Legend Items. The
972 * font size depends in the return value of <i>getLegendFontSize()</i>.
973 *
974 * @return a new Font instance with <i>DEFAULT_FONT_NAME</i>.
975 */
976 protected Font createLegendLabelFont() {
977 return new Font(
978 DEFAULT_FONT_NAME,
979 Font.PLAIN,
980 getLegendFontSize()
981 );
767 } 982 }
768 983
769 984
770 protected void preparePDFContext(CallContext context) { 985 protected void preparePDFContext(CallContext context) {
771 int[] dimension = getExportDimension(); 986 int[] dimension = getExportDimension();

http://dive4elements.wald.intevation.org