Mercurial > dive4elements > river
view flys-artifacts/src/main/java/de/intevation/flys/exports/DurationCurveGenerator.java @ 682:591249171c32
#77 The curves in a duration chart will have names now.
flys-artifacts/trunk@2115 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 14 Jun 2011 15:34:27 +0000 |
parents | bc1e4878d7e3 |
children | eab5e5089d77 |
line wrap: on
line source
package de.intevation.flys.exports; import java.awt.Color; import org.w3c.dom.Document; import org.apache.log4j.Logger; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.chart.title.TextTitle; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; import de.intevation.artifacts.Artifact; import de.intevation.flys.model.River; import de.intevation.flys.artifacts.WINFOArtifact; import de.intevation.flys.artifacts.model.WQDay; import de.intevation.flys.artifacts.resources.Resources; /** * An OutGenerator that generates duration curves. * * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> */ public class DurationCurveGenerator extends XYChartGenerator { public static final String I18N_DURATION_W = "chart.duration.curve.curve.w"; public static final String I18N_DURATION_Q = "chart.duration.curve.curve.q"; private static Logger logger = Logger.getLogger(DurationCurveGenerator.class); /** The storage for the W series to be drawn in this chart.*/ protected XYSeriesCollection w; /** The storage for the Q series to be drawn in this chart.*/ protected XYSeriesCollection q; public static final String DURATION_CURVE_W = "duration_curve.w"; public static final String DURATION_CURVE_Q = "duration_curve.q"; public static final String I18N_CHART_TITLE = "chart.duration.curve.title"; public static final String I18N_CHART_SUBTITLE = "chart.duration.curve.subtitle"; public static final String I18N_XAXIS_LABEL = "chart.duration.curve.xaxis.label"; public static final String I18N_YAXIS_LABEL = "chart.duration.curve.yaxis.label"; public static final String I18N_CHART_TITLE_DEFAULT = "Dauerlinie"; public static final String I18N_XAXIS_LABEL_DEFAULT = "Unterschreitungsdauer [Tage]"; public static final String I18N_YAXIS_LABEL_DEFAULT = "W [NN + m]"; public DurationCurveGenerator() { super(); this.w = new XYSeriesCollection(); this.q = new XYSeriesCollection(); } protected String getChartTitle() { return msg(I18N_CHART_TITLE, I18N_CHART_TITLE_DEFAULT); } @Override protected void addSubtitles(JFreeChart chart) { double[] dist = getRange(); Object[] args = new Object[] { getRiverName(), dist[0] }; String subtitle = msg(I18N_CHART_SUBTITLE, "", args); chart.addSubtitle(new TextTitle(subtitle)); } protected String getXAxisLabel() { return msg(I18N_XAXIS_LABEL, I18N_XAXIS_LABEL_DEFAULT); } protected String getYAxisLabel() { return msg(I18N_YAXIS_LABEL, I18N_YAXIS_LABEL_DEFAULT); } public void addDatasets(JFreeChart chart) { XYPlot plot = (XYPlot) chart.getPlot(); plot.setDataset(0, w); plot.setDataset(1, q); } protected void adjustPlot(XYPlot plot) { super.adjustPlot(plot); // TODO REMOVE THIS CODE, IF WE HAVE INTRODUCED THEMES! XYLineAndShapeRenderer rw = (XYLineAndShapeRenderer) plot.getRendererForDataset(w); XYLineAndShapeRenderer rq = null; try { rq = (XYLineAndShapeRenderer) rw.clone(); } catch (Exception e) { logger.error(e, e); } int wNum = w.getSeriesCount(); int qNum = q.getSeriesCount(); for (int i = 0; i < wNum; i++) { rw.setSeriesPaint(i, Color.BLUE); } for (int i = 0; i < qNum; i++) { rq.setSeriesPaint(i, Color.GREEN); } plot.setRenderer(0, rw); plot.setRenderer(1, rq); } protected void adjustAxes(XYPlot plot) { super.adjustAxes(plot); NumberAxis qAxis = new NumberAxis("Q [m\u00b3/s]"); plot.setRangeAxis(1, qAxis); plot.mapDatasetToRangeAxis(1, 1); } public void doOut(Artifact artifact, String facet, Document attr) { logger.debug("DurationCurveGenerator.doOut: " + facet); if (facet == null || facet.length() == 0) { logger.error("No facet given. Cannot create dataset."); return; } WINFOArtifact winfo = (WINFOArtifact) artifact; River river = winfo.getRiver(); if (facet.equals(DURATION_CURVE_W)) { doWOut(getDurationCurveData(artifact), river.getName()); } else if (facet.equals(DURATION_CURVE_Q)) { doQOut(getDurationCurveData(artifact), river.getName()); } else { logger.warn("Unknown facet name: " + facet); return; } } /** * Creates the series for a duration curve's W facet. * * @param wqdays The WQDay store that contains the Ws. * @param river The name of the river. */ protected void doWOut(WQDay wqdays, String river) { logger.debug("DurationCurveGenerator.doWOut"); // TODO find the correct series name XYSeries series = new XYSeries( getSeriesName(river, DURATION_CURVE_W)); int size = wqdays.size(); for (int i = 0; i < size; i++) { int day = wqdays.getDay(i); double w = wqdays.getW(i); series.add((double) day, w); } this.w.addSeries(series); } /** * Creates the series for a duration curve's Q facet. * * @param wqdays The WQDay store that contains the Qs. * @param river The name of the river. */ protected void doQOut(WQDay wqdays, String river) { logger.debug("DurationCurveGenerator.doQOut"); // TODO find the correct series name XYSeries series = new XYSeries( getSeriesName(river, DURATION_CURVE_Q)); int size = wqdays.size(); for (int i = 0; i < size; i++) { int day = wqdays.getDay(i); double q = wqdays.getQ(i); series.add((double) day, q); } this.q.addSeries(series); } /** * Returns the computed data for a duration curve based on the artifact's * computation method. * * @param artifact The WINFO artifact. * * @return the computed data for a duration curve's W and Q facet. */ protected WQDay getDurationCurveData(Artifact artifact) { WINFOArtifact winfoArtifact = (WINFOArtifact) artifact; return winfoArtifact.getDurationCurveData(); } protected String getSeriesName(String river, String type) { Object[] args = new Object[] { river }; if (type == null || type.length() == 0) { logger.warn("No duration curve type given."); return "n/a"; } else if (type.equals(DURATION_CURVE_W)) { return Resources.getMsg( context.getMeta(), I18N_DURATION_W, "W", args); } else if (type.equals(DURATION_CURVE_Q)) { return Resources.getMsg( context.getMeta(), I18N_DURATION_Q, "W", args); } logger.warn("Could not determine chart curve type: " + type); return type; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :