Mercurial > dive4elements > river
view flys-artifacts/src/main/java/de/intevation/flys/exports/LongitudinalSectionGenerator.java @ 1713:6d9184c745dd
Bugfix: #226 Set the upper bound of duration curve charts to 364.
flys-artifacts/trunk@2985 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Mon, 17 Oct 2011 11:04:53 +0000 |
parents | 7e19449d7826 |
children | 62983d0ced51 |
line wrap: on
line source
package de.intevation.flys.exports; import java.awt.Font; import org.apache.log4j.Logger; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.title.TextTitle; import org.jfree.data.Range; import org.jfree.data.xy.XYSeries; import org.w3c.dom.Document; import de.intevation.artifacts.Artifact; import de.intevation.artifactdatabase.state.Facet; import de.intevation.flys.artifacts.FLYSArtifact; import de.intevation.flys.artifacts.model.FacetTypes; import de.intevation.flys.artifacts.model.WQKms; import de.intevation.flys.jfree.FLYSAnnotation; import de.intevation.flys.utils.FLYSUtils; import de.intevation.flys.utils.DataUtil; /** * An OutGenerator that generates discharge curves. * * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> */ public class LongitudinalSectionGenerator extends XYChartGenerator implements FacetTypes { /** The logger that is used in this generator. */ private static Logger logger = Logger.getLogger(LongitudinalSectionGenerator.class); /** Key to look up internationalized String for annotations label. */ public static final String I18N_ANNOTATIONS_LABEL = "chart.longitudinal.annotations.label"; /** * Key to look up internationalized String for LongitudinalSection diagrams * titles. */ public static final String I18N_CHART_TITLE = "chart.longitudinal.section.title"; /** * Key to look up internationalized String for LongitudinalSection diagrams * subtitles. */ public static final String I18N_CHART_SUBTITLE = "chart.longitudinal.section.subtitle"; public static final String I18N_XAXIS_LABEL = "chart.longitudinal.section.xaxis.label"; public static final String I18N_YAXIS_LABEL = "chart.longitudinal.section.yaxis.label"; public static final String I18N_2YAXIS_LABEL = "chart.longitudinal.section.yaxis.second.label"; public static final String I18N_CHART_TITLE_DEFAULT = "W-L\u00e4ngsschnitt"; public static final String I18N_XAXIS_LABEL_DEFAULT = "km"; public static final String I18N_YAXIS_LABEL_DEFAULT = "W [NN + m]"; public static final String I18N_2YAXIS_LABEL_DEFAULT = "Q [m\u00b3/s]"; /** Whether or not the plot is inverted (left-right). */ protected boolean inverted; public LongitudinalSectionGenerator() { super(); } public boolean isInverted() { return inverted; } public void setInverted(boolean inverted) { this.inverted = inverted; } /** * Get internationalized title for chart. */ public String getChartTitle() { return msg(I18N_CHART_TITLE, I18N_CHART_TITLE_DEFAULT); } /** * Gets key to look up internationalized String for the charts subtitle. * @return key to look up translated subtitle. */ protected String getChartSubtitleKey() { return I18N_CHART_SUBTITLE; } /** * Add (internationalized) subtitle to chart. * @see getChartSubtitleKey */ @Override protected void addSubtitles(JFreeChart chart) { double[] dist = getRange(); Object[] args = new Object[] { getRiverName(), dist[0], dist[1] }; String subtitle = msg(getChartSubtitleKey(), "", args); chart.addSubtitle(new TextTitle(subtitle)); } /** * Get internationalized label for the x axis. */ protected String getXAxisLabel() { FLYSArtifact flys = (FLYSArtifact) master; return msg( I18N_XAXIS_LABEL, I18N_XAXIS_LABEL_DEFAULT, new Object[] { FLYSUtils.getRiver(flys).getName() }); } /** * Get internationalized label for the y axis. */ protected String getYAxisLabel() { FLYSArtifact flys = (FLYSArtifact) master; String unit = FLYSUtils.getRiver(flys).getWstUnit().getName(); return msg( I18N_YAXIS_LABEL, I18N_YAXIS_LABEL_DEFAULT, new Object[] { unit }); } /** * Get default value for the second Y-Axis' label (if no translation was * found). */ protected String get2YAxisDefaultLabel() { return I18N_2YAXIS_LABEL_DEFAULT; } /** * Get key for internationalization of the second Y-Axis' label. */ protected String get2YAxisLabelKey() { return I18N_2YAXIS_LABEL; } /** * Adjust the axis to meet LongitudinalSection diagram demands. * (e.g. add second Y-axis with internationalized label, trigger * inversion). * @param see get2YAxisLabelKey, get2YAxisDefaultLabel */ @Override protected void adjustAxes(XYPlot plot) { super.adjustAxes(plot); NumberAxis qAxis = new NumberAxis( msg(get2YAxisLabelKey(), get2YAxisDefaultLabel())); plot.setRangeAxis(1, qAxis); Font font = plot.getRangeAxis(0).getLabelFont(); qAxis.setLabelFont(font); invertXAxis(plot.getDomainAxis()); } /** * Create a range that includes 0 (for the Q axis). * @param range range with which to look up upper bound. * @return range with 0 included. */ protected Range createSecondAxisRange(Range range) { return new Range(0d, range.getUpperBound()); } /** * This method overrides the XYChartGenerators zoomY method to be able to * modify the range of the Q axis (here, it shall include 0). */ @Override protected boolean zoomY(XYPlot plot, ValueAxis axis, Range range, Range x) { if (plot.getRangeAxisIndex(axis) == 1) { // We want the Q axis to start at 0 if no zooming has been done. range = createSecondAxisRange(range); } return super.zoomY(plot, axis, range, x); } /** * This method inverts the x-axis based on the kilometer information of the * selected river. If the head of the river is at kilometer 0, the axis is * not inverted, otherwise it is. * * @param xaxis The domain axis. */ protected void invertXAxis(ValueAxis xaxis) { if (inverted) { logger.debug("X-Axis.setInverted(true)"); xaxis.setInverted(true); } } public void doOut( Artifact artifact, Facet facet, Document attr, boolean visible ) { String name = facet.getName(); logger.debug("LongitudinalSectionGenerator.doOut: " + name); if (name == null) { logger.error("No facet name for doOut(). No output generated!"); return; } FLYSArtifact flys = (FLYSArtifact) artifact; Facet f = flys.getNativeFacet(facet); if (f == null) { return; } if (name.equals(LONGITUDINAL_W)) { doWOut((WQKms) f.getData(artifact, context), facet, attr, visible); } else if (name.equals(LONGITUDINAL_Q)) { doQOut((WQKms) f.getData(artifact, context), facet, attr, visible); } else if (name.equals(LONGITUDINAL_ANNOTATION)) { doAnnotationsOut(f.getData(artifact, context), facet, attr,visible); } else { logger.warn("Unknown facet name: " + name); return; } } /** * Register annotations available for the diagram. * * @param o list of annotations (data of facet). * @param facet The facet. This facet does NOT support any data objects. Use * FLYSArtifact.getNativeFacet() instead to retrieve a Facet which supports * data. * @param theme yet ignored. * @param visible The visibility of the annotations. */ protected void doAnnotationsOut( Object o, Facet facet, Document theme, boolean visible ) { logger.debug("LongitudinalSectionGenerator.doAnnotationsOut"); // Add all annotations in list o to our annotation pool. FLYSAnnotation fa = (FLYSAnnotation) o; fa.setLabel(facet.getDescription()); addAnnotations(fa, visible); } /** * Process the output for W facets in a longitudinal section curve. * * @param wqkms An array of WQKms values. * @param facet The facet. This facet does NOT support any data objects. Use * FLYSArtifact.getNativeFacet() instead to retrieve a Facet which supports * data. * @param theme The theme that contains styling information. * @param visible The visibility of the curve. */ protected void doWOut( WQKms wqkms, Facet facet, Document theme, boolean visible ) { logger.debug("LongitudinalSectionGenerator.doWOut"); XYSeries series = new StyledXYSeries(facet.getDescription(), theme); int size = wqkms.size(); for (int i = 0; i < size; i++) { series.add(wqkms.getKm(i), wqkms.getW(i), false); } addFirstAxisSeries(series, visible); if (needInvertAxis(wqkms)) { setInverted(true); } } /** * Process the output for Q facets in a longitudinal section curve. * * @param wqkms An array of WQKms values. * @param facet The facet. This facet does NOT support any data objects. Use * FLYSArtifact.getNativeFacet() instead to retrieve a Facet which supports * data. * @param theme The theme that contains styling information. * @param visible The visibility of the curve. */ protected void doQOut( WQKms wqkms, Facet facet, Document theme, boolean visible ) { logger.debug("LongitudinalSectionGenerator.doQOut"); XYSeries series = new StyledXYSeries(facet.getDescription(), theme); int size = wqkms.size(); for (int i = 0; i < size; i++) { series.add(wqkms.getKm(i), wqkms.getQ(i), false); } addSecondAxisSeries(series, visible); if (needInvertAxis(wqkms)) { setInverted(true); } } /** * This method determines - taking JFreeCharts auto x value ordering into * account - if the x axis need to be inverted. Waterlines in these charts * should decrease. * * @param wqkms The data object that stores the x and y values used for this * chart. */ public boolean needInvertAxis(WQKms wqkms) { boolean wsUp = wqkms.guessWaterIncreasing(); boolean kmUp = DataUtil.guessWaterIncreasing(wqkms.allKms()); boolean inv = (wsUp && kmUp) || (!wsUp && !kmUp); int size = wqkms.size(); if (logger.isDebugEnabled()) { logger.debug("Values : " + size); if (size > 0) { logger.debug("Start km: " + wqkms.getKm(0)); logger.debug("End km: " + wqkms.getKm(size-1)); } logger.debug("wsUp: " + wsUp); logger.debug("kmUp: " + kmUp); logger.debug("inv: " + inv); } return inv; } /** * Get name of series (displayed in legend). * @return name of the series. */ protected String getSeriesName(WQKms wqkms, String mode) { String name = wqkms.getName(); String prefix = name != null && name.indexOf(mode) >= 0 ? null : mode; return prefix != null && prefix.length() > 0 ? prefix + "(" + name +")" : name; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :