# HG changeset patch # User Ingo Weinzierl # Date 1328191240 0 # Node ID c2b15d9c0f4375df6aa13ae1c5c1910725b4a940 # Parent ee5310134463654c5bf06bc9977bc9cfb52e768b Refactoring: moved more base code from XYChartGenerator into its parent class ChartGenerator flys-artifacts/trunk@3883 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r ee5310134463 -r c2b15d9c0f43 flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Thu Feb 02 13:23:55 2012 +0000 +++ b/flys-artifacts/ChangeLog Thu Feb 02 14:00:40 2012 +0000 @@ -1,3 +1,13 @@ +2012-02-02 Ingo Weinzierl + + * src/main/java/de/intevation/flys/exports/TimeseriesChartGenerator.java: + Removed useless import. + + * src/main/java/de/intevation/flys/exports/XYChartGenerator.java, + src/main/java/de/intevation/flys/exports/ChartGenerator.java: More + refactoring: moved more base code from XYChartGenerator to its parent + class ChartGenerator. + 2012-02-02 Felix Wolfsteller * src/main/java/de/intevation/flys/artifacts/states/LocationSelect.java: diff -r ee5310134463 -r c2b15d9c0f43 flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java --- a/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java Thu Feb 02 13:23:55 2012 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java Thu Feb 02 14:00:40 2012 +0000 @@ -1,8 +1,13 @@ package de.intevation.flys.exports; import java.awt.Color; +import java.awt.Font; + import java.io.IOException; import java.io.OutputStream; + +import java.util.ArrayList; +import java.util.List; import java.util.Locale; import javax.xml.xpath.XPathConstants; @@ -13,6 +18,7 @@ import org.w3c.dom.Element; import org.jfree.chart.JFreeChart; +import org.jfree.chart.LegendItem; import org.jfree.chart.axis.NumberAxis; import org.jfree.data.Range; @@ -32,6 +38,7 @@ import de.intevation.flys.artifacts.FLYSArtifact; import de.intevation.flys.artifacts.resources.Resources; import de.intevation.flys.utils.FLYSUtils; +import de.intevation.flys.utils.ThemeAccess; /** @@ -227,6 +234,168 @@ /** + * Returns an instance of ChartSettings with a chart specific section + * but with no axes settings. + * + * @return an instance of ChartSettings. + */ + @Override + public Settings getSettings() { + if (this.settings != null) { + return this.settings; + } + + ChartSettings settings = new ChartSettings(); + + ChartSection chartSection = buildChartSection(); + LegendSection legendSection = buildLegendSection(); + ExportSection exportSection = buildExportSection(); + + settings.setChartSection(chartSection); + settings.setLegendSection(legendSection); + settings.setExportSection(exportSection); + + List axisSections = buildAxisSections(); + for (AxisSection axisSection: axisSections) { + settings.addAxisSection(axisSection); + } + + return settings; + } + + + /** + * Creates a new ChartSection. + * + * @return a new ChartSection. + */ + protected ChartSection buildChartSection() { + ChartSection chartSection = new ChartSection(); + chartSection.setTitle(getChartTitle()); + chartSection.setSubtitle(getChartSubtitle()); + chartSection.setDisplayGird(isGridVisible()); + return chartSection; + } + + + /** + * Creates a new LegendSection. + * + * @return a new LegendSection. + */ + protected LegendSection buildLegendSection() { + LegendSection legendSection = new LegendSection(); + legendSection.setVisibility(isLegendVisible()); + legendSection.setFontSize(getLegendFontSize()); + return legendSection; + } + + + /** + * Creates a new ExportSection with default values WIDTH=600 + * and HEIGHT=400. + * + * @return a new ExportSection. + */ + protected ExportSection buildExportSection() { + ExportSection exportSection = new ExportSection(); + exportSection.setWidth(600); + exportSection.setHeight(400); + return exportSection; + } + + + /** + * Creates a list of Sections that contains all axes of the chart (including + * X and Y axes). + * + * @return a list of Sections for each axis in this chart. + */ + protected List buildAxisSections() { + List axisSections = new ArrayList(); + + axisSections.addAll(buildXAxisSections()); + axisSections.addAll(buildYAxisSections()); + + return axisSections; + } + + + /** + * Creates a new Section for chart's X axis. + * + * @return a List that contains a Section for the X axis. + */ + protected List buildXAxisSections() { + List axisSections = new ArrayList(); + + String identifier = "X"; + + AxisSection axisSection = new AxisSection(); + axisSection.setIdentifier(identifier); + axisSection.setLabel(getXAxisLabel()); + axisSection.setFontSize(14); + axisSection.setFixed(false); + + // XXX We are able to find better default ranges that [0,0], but the Y + // axes currently have no better ranges set. + axisSection.setUpperRange(0d); + axisSection.setLowerRange(0d); + + axisSections.add(axisSection); + + return axisSections; + } + + + /** + * Creates a list of Section for the chart's Y axes. This method makes use + * of getYAxisWalker to be able to access all Y axes defined in + * subclasses. + * + * @return a list of Y axis sections. + */ + protected List buildYAxisSections() { + List axisSections = new ArrayList(); + + YAxisWalker walker = getYAxisWalker(); + for (int i = 0, n = walker.length(); i < n; i++) { + AxisSection ySection = new AxisSection(); + ySection.setIdentifier(walker.getId(i)); + ySection.setLabel(getYAxisLabel(i)); + ySection.setFontSize(14); + ySection.setFixed(false); + + // XXX We are able to find better default ranges that [0,0], the + // only problem is, that we do NOT have a better range than [0,0] + // for each axis, because the initial chart will not have a dataset + // for each axis set! + ySection.setUpperRange(0d); + ySection.setLowerRange(0d); + + axisSections.add(ySection); + } + + return axisSections; + } + + + /** + * Returns the settings as ChartSettings. + * + * @return the settings as ChartSettings or null, if + * settings is not an instance of ChartSettings. + */ + public ChartSettings getChartSettings() { + if (settings instanceof ChartSettings) { + return (ChartSettings) settings; + } + + return null; + } + + + /** * Returns the chart title provided by settings. * * @param settings A ChartSettings object. @@ -527,6 +696,41 @@ /** + * This method searches for a specific axis in the settings if + * settings is set. If the axis was found, this method returns the + * specified axis range if the axis range is fixed. Otherwise, this method + * returns null. + * + * @param axisId The identifier of an axis. + * + * @return the specified axis range from settings if the axis is + * fixed, otherwise null. + */ + public Range getRangeForAxisFromSettings(String axisId) { + ChartSettings chartSettings = getChartSettings(); + if (chartSettings == null) { + return null; + } + + AxisSection as = chartSettings.getAxisSection(axisId); + Boolean fixed = as.isFixed(); + + if (fixed != null && fixed) { + Double upper = as.getUpperRange(); + Double lower = as.getLowerRange(); + + if (upper != null && lower != null) { + return lower < upper + ? new Range(lower, upper) + : new Range(upper, lower); + } + } + + return null; + } + + + /** * This helper mehtod is used to extract the current locale from instance * vairable context. * @@ -728,31 +932,6 @@ /** - * Returns an instance of EmptySettings currently! - * - * @return an instance of EmptySettings. - */ - public Settings getSettings() { - return settings != null ? settings : new EmptySettings(); - } - - - /** - * Returns the settings as ChartSettings. - * - * @return the settings as ChartSettings or null, if - * settings is not an instance of ChartSettings. - */ - public ChartSettings getChartSettings() { - if (settings instanceof ChartSettings) { - return (ChartSettings) settings; - } - - return null; - } - - - /** * Creates a new instance of IdentifiableNumberAxis. * * @param idx The index of the new axis. @@ -767,6 +946,42 @@ } + /** + * Creates a new LegendItem with name and font provided by + * createLegendLabelFont(). + * + * @param theme The theme of the chart line. + * @param The displayed name of the item. + * + * @return a new LegendItem instance. + */ + public LegendItem createLegendItem(Document theme, String name) { + // OPTIMIZE Pass font, parsed Theme items. + ThemeAccess themeAccess = new ThemeAccess(theme); + + Color color = themeAccess.parseLineColorField(); + LegendItem legendItem = new LegendItem(name, color); + + legendItem.setLabelFont(createLegendLabelFont()); + return legendItem; + } + + + /** + * Creates Font (Family and size) to use when creating Legend Items. The + * font size depends in the return value of getLegendFontSize(). + * + * @return a new Font instance with DEFAULT_FONT_NAME. + */ + protected Font createLegendLabelFont() { + return new Font( + DEFAULT_FONT_NAME, + Font.PLAIN, + getLegendFontSize() + ); + } + + protected void preparePDFContext(CallContext context) { int[] dimension = getExportDimension(); diff -r ee5310134463 -r c2b15d9c0f43 flys-artifacts/src/main/java/de/intevation/flys/exports/TimeseriesChartGenerator.java --- a/flys-artifacts/src/main/java/de/intevation/flys/exports/TimeseriesChartGenerator.java Thu Feb 02 13:23:55 2012 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/TimeseriesChartGenerator.java Thu Feb 02 14:00:40 2012 +0000 @@ -1,6 +1,5 @@ package de.intevation.flys.exports; -import java.io.IOException; import org.apache.log4j.Logger; diff -r ee5310134463 -r c2b15d9c0f43 flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java --- a/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java Thu Feb 02 13:23:55 2012 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java Thu Feb 02 14:00:40 2012 +0000 @@ -11,7 +11,6 @@ import java.awt.image.BufferedImage; -import java.io.IOException; import java.text.NumberFormat; @@ -46,13 +45,10 @@ import org.jfree.ui.RectangleInsets; import org.jfree.ui.TextAnchor; -import de.intevation.artifacts.CallContext; import de.intevation.artifactdatabase.state.Facet; -import de.intevation.artifactdatabase.state.Settings; -import de.intevation.flys.exports.ChartExportHelper; import de.intevation.flys.jfree.EnhancedLineAndShapeRenderer; import de.intevation.flys.jfree.FLYSAnnotation; import de.intevation.flys.jfree.StableXYDifferenceRenderer; @@ -442,16 +438,6 @@ } - /** Creates Font (Family and size) to use when creating Legend Items. */ - protected Font createLegendLabelFont() { - return new Font( - DEFAULT_FONT_NAME, - Font.PLAIN, - getLegendFontSize() - ); - } - - /** * If no data is visible, draw at least empty axis. */ @@ -632,50 +618,6 @@ } - /** - * This method searches for a specific axis in the settings if - * settings is set. If the axis was found, this method returns the - * specified axis range if the axis range is fixed. Otherwise, this method - * returns null. - * - * @param axisId The identifier of an axis. - * - * @return the specified axis range from settings if the axis is - * fixed, otherwise null. - */ - public Range getRangeForAxisFromSettings(String axisId) { - ChartSettings chartSettings = getChartSettings(); - if (chartSettings == null) { - return null; - } - - AxisSection as = chartSettings.getAxisSection(axisId); - Boolean fixed = as.isFixed(); - - if (fixed != null && fixed) { - Double upper = as.getUpperRange(); - Double lower = as.getLowerRange(); - - if (upper != null && lower != null) { - return lower < upper - ? new Range(lower, upper) - : new Range(upper, lower); - } - } - - return null; - } - - public LegendItem createLegendItem(Document theme, String name) { - // OPTIMIZE Pass font, parsed Theme items. - ThemeAccess themeAccess = new ThemeAccess(theme); - Color color = themeAccess.parseLineColorField(); - LegendItem li = new LegendItem(name, color); - li.setLabelFont(createLegendLabelFont()); - return li; - } - - /** Get color for hyk zones by their type (which is the name). */ public Paint colorForHYKZone(String zoneName) { if (zoneName.startsWith("R")) { @@ -950,12 +892,6 @@ } - /** Override to handle subtitle adding. */ - protected void addSubtitles(JFreeChart chart) { - // override this method in subclasses that need subtitles - } - - /** * This method walks over all axes (domain and range) of plot and * calls localizeDomainAxis() for domain axes or localizeRangeAxis() for @@ -1154,152 +1090,6 @@ /** - * Returns an instance of ChartSettings with a chart specific section - * but with no axes settings. - * - * @return an instance of ChartSettings. - */ - public Settings getSettings() { - if (this.settings != null) { - return this.settings; - } - - ChartSettings settings = new ChartSettings(); - - ChartSection chartSection = buildChartSection(); - LegendSection legendSection = buildLegendSection(); - ExportSection exportSection = buildExportSection(); - - settings.setChartSection(chartSection); - settings.setLegendSection(legendSection); - settings.setExportSection(exportSection); - - List axisSections = buildAxisSections(); - for (AxisSection axisSection: axisSections) { - settings.addAxisSection(axisSection); - } - - return settings; - } - - - /** - * Creates a new ChartSection. - * - * @return a new ChartSection. - */ - protected ChartSection buildChartSection() { - ChartSection chartSection = new ChartSection(); - chartSection.setTitle(getChartTitle()); - chartSection.setSubtitle(getChartSubtitle()); - chartSection.setDisplayGird(isGridVisible()); - return chartSection; - } - - - /** - * Creates a new LegendSection. - * - * @return a new LegendSection. - */ - protected LegendSection buildLegendSection() { - LegendSection legendSection = new LegendSection(); - legendSection.setVisibility(isLegendVisible()); - legendSection.setFontSize(getLegendFontSize()); - return legendSection; - } - - - /** - * Creates a new ExportSection with default values WIDTH=600 - * and HEIGHT=400. - * - * @return a new ExportSection. - */ - protected ExportSection buildExportSection() { - ExportSection exportSection = new ExportSection(); - exportSection.setWidth(600); - exportSection.setHeight(400); - return exportSection; - } - - - /** - * Creates a list of Sections that contains all axes of the chart (including - * X and Y axes). - * - * @return a list of Sections for each axis in this chart. - */ - protected List buildAxisSections() { - List axisSections = new ArrayList(); - - axisSections.addAll(buildXAxisSections()); - axisSections.addAll(buildYAxisSections()); - - return axisSections; - } - - - /** - * Creates a new Section for chart's X axis. - * - * @return a List that contains a Section for the X axis. - */ - protected List buildXAxisSections() { - List axisSections = new ArrayList(); - - String identifier = "X"; - - AxisSection axisSection = new AxisSection(); - axisSection.setIdentifier(identifier); - axisSection.setLabel(getXAxisLabel()); - axisSection.setFontSize(14); - axisSection.setFixed(false); - - // XXX We are able to find better default ranges that [0,0], but the Y - // axes currently have no better ranges set. - axisSection.setUpperRange(0d); - axisSection.setLowerRange(0d); - - axisSections.add(axisSection); - - return axisSections; - } - - - /** - * Creates a list of Section for the chart's Y axes. This method makes use - * of getYAxisWalker to be able to access all Y axes defined in - * subclasses. - * - * @return a list of Y axis sections. - */ - protected List buildYAxisSections() { - List axisSections = new ArrayList(); - - YAxisWalker walker = getYAxisWalker(); - for (int i = 0, n = walker.length(); i < n; i++) { - AxisSection ySection = new AxisSection(); - ySection.setIdentifier(walker.getId(i)); - ySection.setLabel(getYAxisLabel(i)); - ySection.setFontSize(14); - ySection.setFixed(false); - - // XXX We are able to find better default ranges that [0,0], the - // only problem is, that we do NOT have a better range than [0,0] - // for each axis, because the initial chart will not have a dataset - // for each axis set! - ySection.setUpperRange(0d); - ySection.setLowerRange(0d); - - axisSections.add(ySection); - } - - return axisSections; - } - - - /** * Do Points out. */ protected void doPoints(