Mercurial > dive4elements > river
changeset 369:2ce7b473620e
Implemented the chart creation of a longitudinal section chart - W and Q facets.
flys-artifacts/trunk@1778 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Fri, 29 Apr 2011 10:13:24 +0000 |
parents | 3e66a5705c39 |
children | 4a8d934e745f |
files | flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/exports/DischargeCurveGenerator.java flys-artifacts/src/main/java/de/intevation/flys/exports/LongitudinalSectionGenerator.java flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java |
diffstat | 4 files changed, 265 insertions(+), 45 deletions(-) [+] |
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog Fri Apr 29 10:02:59 2011 +0000 +++ b/flys-artifacts/ChangeLog Fri Apr 29 10:13:24 2011 +0000 @@ -1,3 +1,14 @@ +2011-04-29 Ingo Weinzierl <ingo@intevation.de> + + * src/main/java/de/intevation/flys/exports/XYChartGenerator.java: New. An + abstract base class for ChartGenerators that create XY charts. + + * src/main/java/de/intevation/flys/exports/DischargeCurveGenerator.java: + Sourced the generate() method out to the XYChartGenerator. + + * src/main/java/de/intevation/flys/exports/LongitudinalSectionGenerator.java: + Implemented the methods to add W and Q facets to the chart. + 2011-04-29 Ingo Weinzierl <ingo@intevation.de> * src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java: Improved
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/DischargeCurveGenerator.java Fri Apr 29 10:02:59 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/DischargeCurveGenerator.java Fri Apr 29 10:13:24 2011 +0000 @@ -1,7 +1,5 @@ package de.intevation.flys.exports; -import java.awt.Color; -import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -11,10 +9,7 @@ import org.w3c.dom.Document; import org.jfree.data.xy.DefaultXYDataset; - -import org.jfree.chart.ChartFactory; -import org.jfree.chart.JFreeChart; -import org.jfree.chart.plot.PlotOrientation; +import org.jfree.data.xy.XYDataset; import de.intevation.artifacts.Artifact; @@ -23,7 +18,6 @@ import de.intevation.flys.artifacts.FLYSArtifact; import de.intevation.flys.artifacts.model.DischargeTables; import de.intevation.flys.artifacts.model.GaugesFactory; -import de.intevation.flys.exports.ChartExportHelper; /** @@ -31,7 +25,7 @@ * * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> */ -public class DischargeCurveGenerator extends ChartGenerator { +public class DischargeCurveGenerator extends XYChartGenerator { /** The logger used in this generator.*/ private static Logger logger = @@ -49,6 +43,26 @@ } + protected String getChartTitle() { + return "Abflusskurven"; + } + + + protected String getXAxisLabel() { + return "Q[m³/s]"; + } + + + protected String getYAxisLabel() { + return "W[cm]"; + } + + + protected XYDataset getXYDataset() { + return dataset; + } + + public void doOut(Artifact artifact, String facet, Document attr) { logger.debug("DischargeCurveGenerator.doOut: " + facet); @@ -98,32 +112,6 @@ double[][] values = gaugeValues.get(gauge); dataset.addSeries(gauge, values); } - - } - - - public void generate() - throws IOException - { - logger.debug("DischargeCurveGenerator.generate"); - - JFreeChart chart = ChartFactory.createXYLineChart( - "Abflusskurven", - "Q", "W", - dataset, - PlotOrientation.VERTICAL, - true, - false, - false); - - chart.setBackgroundPaint(Color.WHITE); - chart.getPlot().setBackgroundPaint(Color.WHITE); - - ChartExportHelper.exportImage( - out, - chart, - "png", - 600, 400); } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/LongitudinalSectionGenerator.java Fri Apr 29 10:02:59 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/LongitudinalSectionGenerator.java Fri Apr 29 10:13:24 2011 +0000 @@ -1,10 +1,10 @@ package de.intevation.flys.exports; -import java.io.IOException; - import org.apache.log4j.Logger; -import org.jfree.data.xy.DefaultXYDataset; +import org.jfree.data.xy.XYDataset; +import org.jfree.data.xy.XYSeries; +import org.jfree.data.xy.XYSeriesCollection; import org.w3c.dom.Document; @@ -19,39 +19,162 @@ * * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> */ -public class LongitudinalSectionGenerator extends ChartGenerator { +public class LongitudinalSectionGenerator extends XYChartGenerator { /** The logger that is used in this generator.*/ private static Logger logger = Logger.getLogger(LongitudinalSectionGenerator.class); - protected DefaultXYDataset dataset; + public static final String LONGITUDINAL_SECTION_W = + "longitudinal_section.w"; + + public static final String LONGITUDINAL_SECTION_Q = + "longitudinal_section.q"; + + + /** The storage for the series to be drawn in this chart.*/ + protected XYSeriesCollection dataset; public LongitudinalSectionGenerator() { super(); - this.dataset = new DefaultXYDataset(); + this.dataset = new XYSeriesCollection(); + } + + + protected String getChartTitle() { + // TODO i18n + return "Wasserstand für Gewässer"; + } + + + protected String getXAxisLabel() { + return "km"; + } + + + protected String getYAxisLabel() { + return "W [NN + m]"; + } + + + protected XYDataset getXYDataset() { + return dataset; } public void doOut(Artifact artifact, String facet, Document attr) { logger.debug("LongitudinalSectionGenerator.doOut: " + facet); + if (facet == null) { + logger.error("No facet name for doOut(). No output generated!"); + return; + } + + if (facet.equals(LONGITUDINAL_SECTION_W)) { + doWOut(getWaterlevelData(artifact)); + } + else if (facet.equals(LONGITUDINAL_SECTION_Q)) { + doQOut(getWaterlevelData(artifact)); + } + else { + logger.warn("Unknown facet name: " + facet); + return; + } + } + + + /** + * Returns the waterlevel data computed by the WINFOArtifact. + * + * @param artifact The WINFOArtifact. + * + * @return the computed waterlevel data. + */ + protected WQKms[] getWaterlevelData(Artifact artifact) { WINFOArtifact winfoArtifact = (WINFOArtifact) artifact; WQKms[] wqkms = winfoArtifact.getWaterlevelData(); logger.debug("Got " + wqkms.length + " WQKms objects."); + + return wqkms; } - public void generate() - throws IOException - { - logger.debug("LongitudinalSectionGenerator.generate"); + /** + * Process the output for W facets in a longitudinal section curve. + * + * @param wqkms An array of WQKms values. + */ + protected void doWOut(WQKms[] wqkms) { + logger.debug("LongitudinalSectionGenerator.doWOut"); - // TODO Implement me! + int idx = 0; + for (WQKms tmp: wqkms) { + XYSeries series = new XYSeries(getSeriesName(tmp, "w", idx++)); + + double[] target = new double[3]; + int size = tmp.size(); + + if (logger.isDebugEnabled()) { + logger.debug("Generate series: " + series.getKey()); + + logger.debug("Start km: " + tmp.getKms(0)); + logger.debug("End km: " + tmp.getKms(size-1)); + logger.debug("Values : " + size); + } + + for (int i = 0; i < size; i++) { + target = tmp.get(i, target); + + logger.debug("++ W Tuple: " + target[2] + " -> " + target[0]); + series.add(target[2], target[0]); + } + + dataset.addSeries(series); + } + } + + + /** + * Process the output for Q facets in a longitudinal section curve. + * + * @param wqkms An array of WQKms values. + */ + protected void doQOut(WQKms[] wqkms) { + logger.debug("LongitudinalSectionGenerator.doQOut"); + + int idx = 0; + for (WQKms tmp: wqkms) { + XYSeries series = new XYSeries(getSeriesName(tmp, "Q", idx++)); + + double[] target = new double[3]; + int size = tmp.size(); + + if (logger.isDebugEnabled()) { + logger.debug("Generate series: " + series.getKey()); + + logger.debug("Start km: " + tmp.getKms(0)); + logger.debug("End km: " + tmp.getKms(size-1)); + logger.debug("Values : " + size); + } + + for (int i = 0; i < size; i++) { + target = tmp.get(i, target); + + logger.debug("++ Q Tuple: " + target[2] + " -> " + target[1]); + series.add(target[2], target[1]); + } + + dataset.addSeries(series); + } + } + + + protected String getSeriesName(WQKms wqkms, String prefix, int idx) { + return prefix + "-" + idx; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java Fri Apr 29 10:13:24 2011 +0000 @@ -0,0 +1,98 @@ +package de.intevation.flys.exports; + +import java.awt.Color; + +import java.io.IOException; + +import org.apache.log4j.Logger; + +import org.jfree.chart.ChartFactory; +import org.jfree.chart.JFreeChart; +import org.jfree.chart.axis.NumberAxis; +import org.jfree.chart.plot.PlotOrientation; +import org.jfree.chart.plot.XYPlot; +import org.jfree.data.xy.XYDataset; + +import de.intevation.flys.exports.ChartExportHelper; + + +/** + * An abstract base class for creating XY charts. + * + * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> + */ +public abstract class XYChartGenerator extends ChartGenerator { + + /** The logger that is used in this generator.*/ + private static Logger logger = Logger.getLogger(ChartGenerator.class); + + + /** + * Returns the title of a chart. + * + * @return the title of a chart. + */ + protected abstract String getChartTitle(); + + /** + * Returns the X-Axis label of a chart. + * + * @return the X-Axis label of a chart. + */ + protected abstract String getXAxisLabel(); + + /** + * Returns the Y-Axis label of a chart. + * + * @return the Y-Axis label of a chart. + */ + protected abstract String getYAxisLabel(); + + /** + * Returns the XYDataset used to add curves to the chart. + * + * @return the XYDataset used to add curves to the chart. + */ + protected abstract XYDataset getXYDataset(); + + + public void generate() + throws IOException + { + logger.debug("XYChartGenerator.generate"); + + JFreeChart chart = ChartFactory.createXYLineChart( + getChartTitle(), + getXAxisLabel(), + getYAxisLabel(), + getXYDataset(), + PlotOrientation.VERTICAL, + true, + false, + false); + + chart.setBackgroundPaint(Color.WHITE); + chart.getPlot().setBackgroundPaint(Color.WHITE); + + adjustAxes((XYPlot) chart.getPlot()); + + ChartExportHelper.exportImage( + out, + chart, + "png", + 600, 400); + } + + + /** + * Adjusts the axes of a plot. + * + * @param plot The XYPlot of the chart. + */ + protected void adjustAxes(XYPlot plot) { + NumberAxis yAxis = (NumberAxis) plot.getRangeAxis(); + + yAxis.setAutoRangeIncludesZero(false); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :