ingo@359: package de.intevation.flys.exports; ingo@359: ingo@1690: import java.awt.Font; ingo@1690: ingo@359: import org.apache.log4j.Logger; ingo@359: ingo@375: import org.jfree.chart.JFreeChart; ingo@375: import org.jfree.chart.axis.NumberAxis; ingo@422: import org.jfree.chart.axis.ValueAxis; ingo@375: import org.jfree.chart.plot.XYPlot; ingo@414: import org.jfree.chart.title.TextTitle; ingo@720: import org.jfree.data.Range; ingo@369: import org.jfree.data.xy.XYSeries; ingo@364: ingo@359: import org.w3c.dom.Document; ingo@359: ingo@359: import de.intevation.artifacts.Artifact; ingo@359: ingo@695: import de.intevation.artifactdatabase.state.Facet; ingo@695: ingo@422: import de.intevation.flys.artifacts.FLYSArtifact; sascha@706: ingo@696: import de.intevation.flys.artifacts.model.FacetTypes; ingo@364: import de.intevation.flys.artifacts.model.WQKms; ingo@364: ingo@1677: import de.intevation.flys.jfree.FLYSAnnotation; sascha@1688: ingo@1667: import de.intevation.flys.utils.FLYSUtils; sascha@1688: import de.intevation.flys.utils.DataUtil; ingo@359: felix@1035: felix@1035: /** ingo@359: * An OutGenerator that generates discharge curves. ingo@359: * ingo@359: * @author Ingo Weinzierl ingo@359: */ ingo@696: public class LongitudinalSectionGenerator ingo@696: extends XYChartGenerator ingo@696: implements FacetTypes ingo@696: { felix@1037: /** The logger that is used in this generator. */ ingo@359: private static Logger logger = ingo@359: Logger.getLogger(LongitudinalSectionGenerator.class); ingo@359: felix@1700: /** Key to look up internationalized String for annotations label. */ felix@1041: public static final String I18N_ANNOTATIONS_LABEL = felix@1041: "chart.longitudinal.annotations.label"; felix@1041: felix@1700: /** Whether or not the plot is inverted (left-right). */ sascha@745: protected boolean inverted; sascha@745: felix@1041: ingo@359: public LongitudinalSectionGenerator() { ingo@359: super(); ingo@369: } ingo@369: ingo@369: sascha@745: public boolean isInverted() { sascha@745: return inverted; sascha@745: } sascha@745: felix@1700: sascha@745: public void setInverted(boolean inverted) { sascha@745: this.inverted = inverted; sascha@745: } ingo@369: felix@1700: felix@1700: /** felix@1700: * Get internationalized title for chart. felix@1700: */ felix@1700: public String getChartTitle() { felix@1700: return msg("chart.longitudinal.section.title", "W-L\u00e4ngsschnitt"); felix@1700: } felix@1700: felix@1700: felix@1700: /** felix@1700: * Gets key to look up internationalized String for the charts subtitle. felix@1700: * @return key to look up translated subtitle. felix@1700: */ felix@1700: protected String getChartSubtitleKey() { felix@1700: return "chart.longitudinal.section.subtitle"; felix@1700: } felix@1700: felix@1700: felix@1700: /** felix@1700: * Add (internationalized) subtitle to chart. felix@1700: * @see getChartSubtitleKey felix@1700: */ ingo@414: @Override ingo@414: protected void addSubtitles(JFreeChart chart) { felix@1700: double[] dist = getRange(); ingo@414: Object[] args = new Object[] { ingo@414: getRiverName(), ingo@414: dist[0], ingo@414: dist[1] ingo@414: }; ingo@414: felix@1700: String subtitle = msg(getChartSubtitleKey(), "", args); ingo@414: chart.addSubtitle(new TextTitle(subtitle)); ingo@414: } ingo@414: ingo@414: felix@1700: /** felix@1700: * Get internationalized label for the x axis. felix@1700: */ ingo@369: protected String getXAxisLabel() { ingo@1667: FLYSArtifact flys = (FLYSArtifact) master; ingo@1667: ingo@1667: return msg( felix@1700: "chart.longitudinal.section.xaxis.label", felix@1700: "km", ingo@1667: new Object[] { FLYSUtils.getRiver(flys).getName() }); ingo@369: } ingo@369: ingo@369: felix@1700: /** felix@1700: * Get internationalized label for the y axis. felix@1700: */ ingo@369: protected String getYAxisLabel() { ingo@1667: FLYSArtifact flys = (FLYSArtifact) master; ingo@1667: ingo@1673: String unit = FLYSUtils.getRiver(flys).getWstUnit().getName(); ingo@1667: ingo@1667: return msg( felix@1700: "chart.longitudinal.section.yaxis.label", felix@1700: "W [NN + m]", ingo@1667: new Object[] { unit }); ingo@369: } ingo@369: ingo@369: felix@1700: /** felix@1700: * Get default value for the second Y-Axis' label (if no translation was felix@1700: * found). felix@1700: */ felix@1700: protected String get2YAxisDefaultLabel() { felix@1700: return "Q [m\u00b3/s]"; felix@1700: } felix@1700: felix@1700: felix@1700: /** felix@1700: * Get key for internationalization of the second Y-Axis' label. felix@1700: */ felix@1700: protected String get2YAxisLabelKey() { felix@1700: return "chart.longitudinal.section.yaxis.second.label"; felix@1700: } felix@1700: felix@1700: felix@1700: /** felix@1700: * Adjust the axis to meet LongitudinalSection diagram demands. felix@1700: * (e.g. add second Y-axis with internationalized label, trigger felix@1700: * inversion). felix@1700: * @param see get2YAxisLabelKey, get2YAxisDefaultLabel felix@1700: */ felix@1700: @Override ingo@375: protected void adjustAxes(XYPlot plot) { ingo@375: super.adjustAxes(plot); ingo@375: ingo@408: NumberAxis qAxis = new NumberAxis( felix@1700: msg(get2YAxisLabelKey(), get2YAxisDefaultLabel())); ingo@375: ingo@666: plot.setRangeAxis(1, qAxis); ingo@422: ingo@1690: Font font = plot.getRangeAxis(0).getLabelFont(); ingo@1690: qAxis.setLabelFont(font); ingo@1690: ingo@422: invertXAxis(plot.getDomainAxis()); ingo@422: } ingo@422: ingo@422: ingo@422: /** felix@1653: * Create a range that includes 0 (for the Q axis). felix@1653: * @param range range with which to look up upper bound. felix@1653: * @return range with 0 included. felix@1653: */ felix@1653: protected Range createSecondAxisRange(Range range) { felix@1653: return new Range(0d, range.getUpperBound()); felix@1653: } felix@1653: felix@1653: felix@1653: /** felix@1653: * This method overrides the XYChartGenerators zoomY method to be able to felix@1653: * modify the range of the Q axis (here, it shall include 0). ingo@720: */ ingo@720: @Override ingo@720: protected boolean zoomY(XYPlot plot, ValueAxis axis, Range range, Range x) { ingo@734: if (plot.getRangeAxisIndex(axis) == 1) { felix@1653: // We want the Q axis to start at 0 if no zooming has been done. felix@1653: range = createSecondAxisRange(range); ingo@720: } ingo@720: ingo@734: return super.zoomY(plot, axis, range, x); ingo@720: } ingo@720: ingo@720: ingo@720: /** ingo@422: * This method inverts the x-axis based on the kilometer information of the ingo@422: * selected river. If the head of the river is at kilometer 0, the axis is ingo@422: * not inverted, otherwise it is. ingo@422: * ingo@422: * @param xaxis The domain axis. ingo@422: */ ingo@422: protected void invertXAxis(ValueAxis xaxis) { ingo@422: sascha@745: if (inverted) { ingo@1692: logger.debug("X-Axis.setInverted(true)"); ingo@422: xaxis.setInverted(true); ingo@422: } ingo@359: } ingo@359: ingo@359: ingo@1684: public void doOut( ingo@1684: Artifact artifact, ingo@1684: Facet facet, ingo@1684: Document attr, ingo@1684: boolean visible ingo@1684: ) { ingo@695: String name = facet.getName(); ingo@359: ingo@695: logger.debug("LongitudinalSectionGenerator.doOut: " + name); ingo@695: ingo@695: if (name == null) { ingo@369: logger.error("No facet name for doOut(). No output generated!"); ingo@369: return; ingo@369: } ingo@369: ingo@696: FLYSArtifact flys = (FLYSArtifact) artifact; ingo@696: Facet f = flys.getNativeFacet(facet); ingo@696: ingo@696: if (f == null) { ingo@696: return; ingo@369: } ingo@696: ingo@696: if (name.equals(LONGITUDINAL_W)) { ingo@1684: doWOut((WQKms) f.getData(artifact, context), attr, visible); ingo@696: } ingo@696: else if (name.equals(LONGITUDINAL_Q)) { ingo@1684: doQOut((WQKms) f.getData(artifact, context), attr, visible); ingo@369: } felix@1028: else if (name.equals(LONGITUDINAL_ANNOTATION)) { ingo@1684: doAnnotationsOut(f.getData(artifact, context), attr, visible); felix@1028: } ingo@369: else { ingo@695: logger.warn("Unknown facet name: " + name); ingo@369: return; ingo@369: } ingo@369: } ingo@369: ingo@369: felix@1037: /** felix@1037: * Register annotations available for the diagram. felix@1037: * felix@1037: * @param o list of annotations (data of facet). felix@1041: * @param theme yet ignored. felix@1037: */ ingo@1684: protected void doAnnotationsOut(Object o, Document theme, boolean visible) { felix@1028: logger.debug("LongitudinalSectionGenerator.doAnnotationsOut"); felix@1041: ingo@1677: // Add all annotations in list o to our annotation pool. ingo@1677: FLYSAnnotation fa = (FLYSAnnotation) o; ingo@1684: addAnnotations(fa, visible); felix@1028: } felix@1028: felix@1028: ingo@369: /** ingo@369: * Process the output for W facets in a longitudinal section curve. ingo@369: * ingo@369: * @param wqkms An array of WQKms values. ingo@924: * @param theme The theme that contains styling information. ingo@369: */ ingo@1684: protected void doWOut(WQKms wqkms, Document theme, boolean visible) { ingo@369: logger.debug("LongitudinalSectionGenerator.doWOut"); ingo@359: ingo@924: XYSeries series = new StyledXYSeries(getSeriesName(wqkms, "W"), theme); ingo@369: sascha@719: int size = wqkms.size(); ingo@696: ingo@696: for (int i = 0; i < size; i++) { sascha@1697: series.add(wqkms.getKm(i), wqkms.getW(i), false); ingo@369: } ingo@696: ingo@1684: addFirstAxisSeries(series, visible); sascha@745: ingo@1692: if (needInvertAxis(wqkms)) { sascha@745: setInverted(true); sascha@745: } ingo@369: } ingo@369: ingo@369: ingo@369: /** ingo@369: * Process the output for Q facets in a longitudinal section curve. ingo@369: * ingo@369: * @param wqkms An array of WQKms values. ingo@924: * @param theme The theme that contains styling information. ingo@369: */ ingo@1684: protected void doQOut(WQKms wqkms, Document theme, boolean visible) { ingo@369: logger.debug("LongitudinalSectionGenerator.doQOut"); ingo@369: ingo@924: XYSeries series = new StyledXYSeries(getSeriesName(wqkms, "Q"), theme); ingo@369: sascha@719: int size = wqkms.size(); ingo@369: ingo@696: for (int i = 0; i < size; i++) { sascha@1697: series.add(wqkms.getKm(i), wqkms.getQ(i), false); ingo@369: } ingo@696: ingo@1684: addSecondAxisSeries(series, visible); sascha@745: ingo@1692: if (needInvertAxis(wqkms)) { ingo@1692: setInverted(true); ingo@1692: } ingo@1692: } ingo@1692: ingo@1692: ingo@1692: /** ingo@1692: * This method determines - taking JFreeCharts auto x value ordering into ingo@1692: * account - if the x axis need to be inverted. Waterlines in these charts ingo@1692: * should decrease. ingo@1692: * ingo@1692: * @param wqkms The data object that stores the x and y values used for this ingo@1692: * chart. ingo@1692: */ ingo@1692: public boolean needInvertAxis(WQKms wqkms) { ingo@1692: boolean wsUp = wqkms.guessWaterIncreasing(); ingo@1692: boolean kmUp = DataUtil.guessWaterIncreasing(wqkms.allKms()); ingo@1692: boolean inv = (wsUp && kmUp) || (!wsUp && !kmUp); ingo@1692: ingo@1692: int size = wqkms.size(); ingo@1692: ingo@1692: if (logger.isDebugEnabled()) { ingo@1692: logger.debug("Values : " + size); ingo@1692: if (size > 0) { ingo@1692: logger.debug("Start km: " + wqkms.getKm(0)); ingo@1692: logger.debug("End km: " + wqkms.getKm(size-1)); ingo@1692: } ingo@1692: logger.debug("wsUp: " + wsUp); ingo@1692: logger.debug("kmUp: " + kmUp); ingo@1692: logger.debug("inv: " + inv); ingo@1692: } ingo@1692: ingo@1692: return inv; ingo@369: } ingo@369: ingo@369: felix@1041: /** felix@1041: * Get name of series (displayed in legend). felix@1041: * @return name of the series. felix@1041: */ ingo@448: protected String getSeriesName(WQKms wqkms, String mode) { ingo@448: String name = wqkms.getName(); ingo@448: String prefix = name != null && name.indexOf(mode) >= 0 ? null : mode; ingo@448: ingo@448: return prefix != null && prefix.length() > 0 ingo@448: ? prefix + "(" + name +")" ingo@448: : name; ingo@359: } ingo@359: } ingo@359: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :